I expected the following output:
Running TestSuite
[DEBUG] beforeClass
[DEBUG] beforeTest
[DEBUG] test
[DEBUG] afterTest
[DEBUG] beforeTest
[DEBUG] test
[DEBUG] afterTest
[DEBUG] afterClass
But instead, this actually happens. Notice 2 problems: BeforeClass runs after BeforeTest. Second, Before/AfterTest runs once.
Running TestSuite
[DEBUG] beforeTest
[DEBUG] beforeClass
[DEBUG] test
[DEBUG] test
[DEBUG] afterClass
[DEBUG] afterTest
Here's the code.
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class TestNgAnnoTest {
   @BeforeClass
   public void beforeClass(){
      System.out.println("beforeClass");
   }
   @BeforeTest
   public void beforeTest(){
      System.out.println("beforeTest");
   }
   @Test
   public void test1(){
      System.out.println("test");
   }
   @Test
   public void test2(){
      System.out.println("test");
   }
   @AfterTest
   public void afterTest(){
      System.out.println("afterTest");
   }
   @AfterClass
   public void afterClass(){
      System.out.println("afterClass");
   }
}
For anyone curious about tool versions: java macosx/1.6.0_22, mvn: 2.2.1, surefire 2.6, testng 5.14.2
Stupid me. Confused After/BeforeTest with After/BeforeMethod.
[DEBUG] beforeClass
[DEBUG] beforeMethod
[DEBUG] test
[DEBUG] afterMethod
[DEBUG] beforeMethod
[DEBUG] test
[DEBUG] afterMethod
[DEBUG] afterClass
Replacing After/BeforeTest produced the correct exec ordering.
    @BeforeClass
    public static void beforeClass(){
        log.debug("beforeClass");
    }
    @BeforeMethod
    public void beforeMethod(){
        log.debug("beforeMethod");
    }
    @Test
    public void test1(){
        log.debug("test");
    }
    @Test
    public void test2(){
        log.debug("test");
    }
    @AfterMethod
    public void afterMethod(){
        log.debug("afterMethod");
    }
    @AfterClass
    public void afterClass(){
        log.debug("afterClass");
    }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With