TestNG 组配置和基本示例
可以在 testng.xml 的 Suite 和/或 Test 元素下配置组。标记为包含在 tesng.xml 中的所有组将被视为执行,排除的组将被忽略。如果 @Test 方法有多个组,并且如果在 testng.xml 中排除任何单个组,那么 @Test 方法将不会运行。
以下是运行组在 Test 级别的典型 testng.xml 配置:
<suite name="Suite World">
<test name="Test Name">
<groups>
<run>
<include name="functest" />
<exclude name="regtest" />
</run>
</groups>
<classes>
<class name="example.group.GroupTest"/>
</classes>
</test>
</suite>
这个测试类看起来如下:
package example.group;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
public class GroupTest {
@BeforeClass
public void deSetup(){
//do configuration stuff here
}
@Test(groups = { "functest", "regtest" })
public void testMethod1() {
}
@Test(groups = {"functest", "regtest"} )
public void testMethod2() {
}
@Test(groups = { "functest" })
public void testMethod3() {
}
@AfterClass
public void cleanUp(){
//do resource release and cleanup stuff here
}
}
在运行这个
GroupTestTestNG类时,只会执行testMethod3()。
说明:
<include name="functest" />functest组的所有测试方法如果不被任何其他组排除,则有资格参加。<exclude name="regtest" />没有regtest组的测试方法有资格参加匹配。testMethod1()和testMethod2()在regtest组,所以他们不会跑。testMethod3()在regtest组,所以它会运行。