gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 23:09:24 26 4
gpt4 key购买 nike

我期望以下输出:

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

最佳答案

愚蠢的我。将 After/BeforeTest 与 After/BeforeMethod 混淆。

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

关于maven - Maven+Surefire+Testng的顺序 - BeforeClass, BeforeTest, Test, Test, AfterTest, AfterClass,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4286801/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com