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組,所以它會執行。