1. 单个测试用例文件
新建TestDBConnection.java文件
import org.testng.annotations.*; public class TestDBConnection { @Test public void runOtherTest1() { System.out.println("@Test - runOtherTest1"); } @Test public void runOtherTest2() { System.out.println("@Test - runOtherTest2"); } @Test(groups = "haha") public void runOtherTest3() { System.out.println("@Test - runOtherTest3"); } @BeforeClass public void beforeClass() { System.out.println("@Test - BeforeClass"); } @BeforeGroups(groups = "haha") public void beforeGroups() { System.out.println("@Test - BeforeGroups"); } @BeforeMethod public void beforeMethod() { System.out.println("@Test - BeforeMethod"); } @AfterMethod public void afterMethod() { System.out.println("@Test - AfterMethod"); } @AfterGroups(groups = "haha") public void afterGroups() { System.out.println("@Test - AfterGroups"); } @AfterClass public void afterClass() { System.out.println("@Test - AfterClass"); } }
配置并运行
点击运行,结果如下:
[TestNG] Running: /Users/sunmin/Library/Caches/IdeaIC2017.3/temp-testng-customsuite.xml @Test - BeforeClass @Test - BeforeMethod @Test - runOtherTest1 @Test - AfterMethod @Test - BeforeMethod @Test - runOtherTest2 @Test - AfterMethod @Test - BeforeGroups @Test - BeforeMethod @Test - runOtherTest3 @Test - AfterMethod @Test - AfterGroups @Test - AfterClass =============================================== Default Suite Total tests run: 3, Failures: 0, Skips: 0 =============================================== Process finished with exit code 0
2. Test Suit
新建TestDBConfig.java文件,内容如下:
import org.testng.annotations.AfterSuite; import org.testng.annotations.AfterTest; import org.testng.annotations.BeforeSuite; import org.testng.annotations.BeforeTest; public class TestDBConfig { @BeforeSuite() public void beforeSuite() { System.out.println("@BeforeSuite"); } @AfterSuite() public void afterSuite() { System.out.println("@AfterSuite"); } @BeforeTest() public void beforeTest() { System.out.println("@BeforeTest"); } @AfterTest() public void afterTest() { System.out.println("@AfterTest"); } }
新建testng.xml文件,位置放在pom.xml同级,内容如下。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" > <!-- @BeforeSuite --> <suite name="TestAll"> <!-- @BeforeTest --> <test name="case1"> <classes> <class name="TestDBConfig" /> </classes> </test> <!-- @AfterTest --> <!-- @BeforeTest --> <test name="case2"> <classes> <class name="TestDBConnection" /> </classes> </test> <!-- @AfterTest -->
</suite> <!-- @AfterSuite -->
配置及运行
Test kind需选择Suite,Suite的配置文件选择我们自己的testng.xml文件。点击运行,结果如下:为啥@AfterTest会出现在这个地方?
[TestNG] Running: /Users/sunmin/Downloads/TestHelloWorld/testng.xml @BeforeSuite @BeforeTest @AfterTest @Test - BeforeClass @Test - BeforeMethod @Test - runOtherTest1 @Test - AfterMethod @Test - BeforeMethod @Test - runOtherTest2 @Test - AfterMethod @Test - BeforeGroups @Test - BeforeMethod @Test - runOtherTest3 @Test - AfterMethod @Test - AfterGroups @Test - AfterClass @AfterSuite =============================================== TestAll Total tests run: 3, Failures: 0, Skips: 0 =============================================== Process finished with exit code 0