使用 groups 属性
package com.lc.tesgFenZu; import org.testng.annotations.AfterGroups; import org.testng.annotations.BeforeGroups; import org.testng.annotations.Test; public class TestNG14 { @BeforeGroups("database") //在测试组database测试之前运行 public void setupDB() { System.out.println("我还是测试方法setuDB;在测试组之前运行"); } @AfterGroups("database") //在测试组database测试之后运行 public void cleanDB() { System.out.println("我还是测试方法cleanDB;在测试组之后运行"); } @Test(groups = "selenium-test") //该类属于测试组 selenium-test public void runSelenium() { System.out.println("我是测试类的 方法:runSelenium "); } @Test(groups = "selenium-test") //该类属于测试组 selenium-test public void runSelenium1() { System.out.println("我是测试类的 方法:runSelenium1 "); } @Test(groups = "database") //该类属于测试组 database public void testConnectOracle() { System.out.println("我是测试类的方法:testConnectOracle"); } @Test(groups = "database") //该类属于测试组 database public void testConnectOracle1() { System.out.println("我是测试类的方法:testConnectOracle1"); } }
运行效果
测试方法也可以同时属于多个分组
例如:@Test(groups = {"database","selenium-test"})
package com.lc.tesgFenZu; import org.testng.annotations.AfterGroups; import org.testng.annotations.BeforeGroups; import org.testng.annotations.Test; public class TestNG15 { @BeforeGroups("database") public void setupDB() { System.out.println("我还是测试方法setuDB;在测试组之前运行"); } @AfterGroups("database") public void cleanDB() { System.out.println("我还是测试方法cleanDB;在测试组之后运行"); } @Test(groups = {"database","selenium-test"}) public void runSelenium() { System.out.println("我是测试类的 方法:runSelenium "); } @Test(groups = {"database","selenium-test"}) public void runSelenium1() { System.out.println("我是测试类的 方法:runSelenium1 "); } @Test(groups = "database") public void testConnectOracle() { System.out.println("我是测试类的方法:testConnectOracle"); } @Test(groups = "database") public void testConnectOracle1() { System.out.println("我是测试类的方法:testConnectOracle1"); } }
运行结果