TestNG执行测试用例的顺序:直接上代码,执行顺序一目了然
代码语言:javascript复制import org.testng.annotations.*;
public class TestDemo {
@BeforeSuite
public void beforeSuite() {
System.out.println("BeforeSuite");
}
@AfterSuite
public void AfterSuite() {
System.out.println("AfterSuite");
}
@BeforeClass
public void beforeclass() {
System.out.println("BeforeClass");
}
@AfterClass
public void afterClass() {
System.out.println("AfterClass");
}
@BeforeTest
public void beforeTest() {
System.out.println("BeforeTest");
}
@AfterTest
public void AfterTest() {
System.out.println("AfterTest");
}
@DataProvider
public Object[][] dp() {
Object[][] data = {{"testCase-", 1}, {"testCase-", 2}, {"testCase-", 3}};
return data;
}
@BeforeMethod
public void before() {
System.out.println("BeforeMethod");
}
@AfterMethod
public void after() {
System.out.println("AfterMethod");
System.out.println("------------------------------------");
}
@Test(dataProvider="dp")
public void t1(String casename,int index) {
System.out.println(casename index);
}
}
执行结果:
代码语言:javascript复制/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/bin/java -ea -Didea.test.cyclic.buffer.size=1048576 -Dfile.encoding=UTF-8 -classpath /Applications/IntelliJ IDEA.app/Contents/lib/idea_rt.jar:/Applications/IntelliJ IDEA.app/Contents/plugins/testng/lib/testng-rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/jre/lib/charsets.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/jre/lib/deploy.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/jre/lib/ext/cldrdata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/jre/lib/ext/dnsns.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/jre/lib/ext/jaccess.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/jre/lib/ext/jfxrt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/jre/lib/ext/localedata.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/jre/lib/ext/nashorn.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/jre/lib/ext/sunec.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/jre/lib/ext/sunjce_provider.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/jre/lib/ext/sunpkcs11.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/jre/lib/ext/zipfs.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/jre/lib/javaws.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/jre/lib/jce.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/jre/lib/jfr.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/jre/lib/jfxswt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/jre/lib/jsse.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/jre/lib/management-agent.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/jre/lib/plugin.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/jre/lib/resources.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/jre/lib/rt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/lib/ant-javafx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/lib/dt.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/lib/javafx-mx.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/lib/jconsole.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/lib/packager.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/lib/sa-jdi.jar:/Library/Java/JavaVirtualMachines/jdk1.8.0_151.jdk/Contents/Home/lib/tools.jar:/Users/imac/Desktop/chat/AutoTest/target/classes:/Users/imac/.m2/repository/org/testng/testng/6.9.10/testng-6.9.10.jar:/Users/imac/.m2/repository/com/beust/jcommander/1.48/jcommander-1.48.jar:/Users/imac/.m2/repository/org/beanshell/bsh/2.0b4/bsh-2.0b4.jar com.intellij.rt.testng.RemoteTestNGStarter -usedefaultlisteners false -socket50881 @w@/private/var/folders/8n/kmxfvb_17jngjq053xkzt2jr0000gn/T/idea_working_dirs_testng.tmp -temp /private/var/folders/8n/kmxfvb_17jngjq053xkzt2jr0000gn/T/idea_testng.tmp
[TestNGContentHandler] [WARN] It is strongly recommended to add "<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >" at the top of your file, otherwise TestNG may fail or not work as expected.
[TestNG] Running:
/Users/imac/Library/Caches/JetBrains/IntelliJIdea2023.1/temp-testng-customsuite.xml
BeforeSuite
BeforeTest
BeforeClass
BeforeMethod
testCase-1
AfterMethod
------------------------------------
BeforeMethod
testCase-2
AfterMethod
------------------------------------
BeforeMethod
testCase-3
AfterMethod
------------------------------------
AfterClass
AfterTest
AfterSuite
===============================================
Default Suite
Total tests run: 3, Failures: 0, Skips: 0
===============================================
结论:执行顺序为 BeforeSuite-BeforeTest-BeforeClass-BeforeMethod-testCase
AfterMethod-AfterClass-AfterTest-AfterSuite