开发者

Order of Maven+Surefire+Testng - BeforeClass, BeforeTest, Test, Test, AfterTest, AfterClass

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
   pub开发者_运维百科lic 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");
    }


Correct. @BeforeTest/@AfterTest wrap a tag, not a test method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜