Maven + Surefire + Testng的顺序 - BeforeClass,BeforeTest,Test,Test,AfterTest,AfterClass

时间:2010-11-26 16:04:07

标签: maven testng surefire

我期待以下输出:

Running TestSuite
[DEBUG] beforeClass
[DEBUG] beforeTest
[DEBUG] test
[DEBUG] afterTest
[DEBUG] beforeTest
[DEBUG] test
[DEBUG] afterTest
[DEBUG] afterClass

但相反,这确实发生了。注意2个问题:BeforeClass在 BeforeTest之后运行。其次,Before / AfterTest运行一次

Running TestSuite
[DEBUG] beforeTest
[DEBUG] beforeClass
[DEBUG] test
[DEBUG] test
[DEBUG] afterClass
[DEBUG] afterTest

这是代码。

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");
   }
}

对于任何对工具版本感兴趣的人: java macosx / 1.6.0_22,mvn:2.2.1,surefire 2.6,testng 5.14.2

2 个答案:

答案 0 :(得分:5)

愚蠢的我。用After / BeforeMethod混淆After / BeforeTest。

[DEBUG] beforeClass
[DEBUG] beforeMethod
[DEBUG] test
[DEBUG] afterMethod
[DEBUG] beforeMethod
[DEBUG] test
[DEBUG] afterMethod
[DEBUG] afterClass

替换After / BeforeTest生成正确的exec排序。

    @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");
    }

答案 1 :(得分:4)

正确。 @ BeforeTest / @ AfterTest包装标签,而不是测试方法。