由于大量的测试用例,用例本身测试重点不一样。在Testng.xml配置文件进入了group概念,这样用例按照组的方式执行,便于重点考察哪些功能点。 下面就说说实践。
1. 在testng-groups.xml文件中,加入要执行的group
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="TC_Operation_Suite" parallel="none" preserve-order="true">
<test name="swiftcoder2_0.TC_Operation">
<groups>
<run>
<include name ="test.workflow" /> //这里配置除@Test annotation 以外,用专门的标注 @Configuration
指定类中的其他特定方法
<include name ="come" />
// 组名come,运行,
<exclude name ="go" /> // 组名go,不运行
</run>
</groups>
<classes>
<class name = "ToBeTestedClass name" />
//在哪个类中执行以上groups
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
注意点:
a. groups必须在test标签里面。(参考文档 :http://www.cnblogs.com/choosewang/articles/3079983.htmlXML文件写法)
b. include name必须在代码中真正存在
2. ToBeTestedClass.java代码
package example1; import org.testng.annotations.*; public class ToBeTestedClass { @BeforeTest @Configuration(beforeTestClass = true,groups = {"test.workflow"}) public void setUp() {System.out.println("----Before Test--------");}
@AfterTest @Configuration(afterTestClass= true, groups = {"test.workflow"}) public void TearDown(){ System.out.println("----After Test--------"); } @Test(groups = { "come" }) public void comeTest() { System.out.println("Hello World"); throw new Error(); } @Test(groups = { "go" }) public void goTest() { System.out.println("Bye Bye"); } @Test(groups = { "come" }) public void comeTestAgain() { System.out.println("Hello World Again"); }
说明点:@BeforeMethod @Configuration(beforeTestMethod= true, groups = {"test.workflow"}) public void BeforeMethod() throws InterruptedException{ System.out.println("----Before Method--------"); } @AfterMethod @Configuration(afterTestMethod= true, groups = {"test.workflow"}) public void AfterMethod() throws InterruptedException{ System.out.println("----After Method--------"); }}
1. @Test注释含有@group注释属于哪一组,如果在testng_group.xml配置include中,则表示运行此组名的所有@Test,反之,exclude表示不执行
2.如果您使用组,那么配置方法也必须属于某个组。详见代码和配置文件背景颜色为紫色的。如果初始条件不配置group的话,这些初始情况是不会执行的
(http://www.ibm.com/developerworks/cn/java/j-testng/
TestNG 使 Java 单元测试轻而易举(查看配置方法内容))
3. 了解@Configuration annotation的使用(http://www.ibm.com/developerworks/cn/java/j-test-ng/Eclipse 3.1 中使用TestNG:基于注释的单元测试框架
清单3 configuration中的属性)
其他参考文档:
http://www.ibm.com/developerworks/cn/java/j-lo-testng/
使用 TestNG 的新特性管理实际项目中的大量单元测试
http://testng.org/doc/documentation-main.html#test-groups
Testng官网文档
3. 执行Testng_groups.xml文件:
类似
[TestNG] Running:
E:\Documents\QA.Management\automation\swiftcoder2_0\testng-groups.xml
Hello World
Hello World Again!
===============================================
TC_Operation_Suite
Total tests run: 2, Failures: 0, Skips: 0
===============================================