1
votes

I am using testng+webdriver for automated testing. And have a problem that @AfterClass annotation doesn't work as I expected.

I have following test class:

    public class WorkspaceTest{
        @BeforeClass
        public void init(){
            //Initialization steps
        }

        @Test
        public void testMethod1{...}

        @Test
        public void testMethod2{...}

        @Test(enabled=false)
        public void testMethod3{...}

        @AfterClass(alwaysRun=true)
        public void tearDown{
            //finalizing steps 
        }

}

If all test methods are enabled - tearDown method works fine, but if one of tests is disabled - I even don't reach break point in tearDown method.

Is it expected behavior for @AfterClass annotation? or I do something wrong?

Testng version: 6.1.1
Webdriver 2.5.0
Java 1.6.0_26
2
I ran into the same bug when I ran the TestNG test in IntelliJ, but it called the @AfterClass correctly (even when one method was enabled=false) when I ran the test via Gradle.checketts

2 Answers

1
votes

It works for me:

tearDown
PASSED: testMethod1
PASSED: testMethod2

===============================================
    Test1
    Tests run: 2, Failures: 0, Skips: 0
===============================================

Here is the source I used:

public class A {
      @BeforeClass
      public void init(){
          //Initialization steps
      }

      @Test
      public void testMethod1() {}

      @Test
      public void testMethod2() {}

      @Test(enabled=false)
      public void testMethod3() {}

      @AfterClass(alwaysRun=true)
      public void tearDown() {
        System.out.println("tearDown");
      }

}
-2
votes

If you using standard junit @AfterClass should be annotated on public static void method, for example:

@AfterClass
public static void logout() {}