TestNG的组测试和组中组测试

在编写测试的过程中,我们经常遇到只想执行个别或者某一部分/某一类型的测试用例,这时我们可以使用TestNG的分组测试方法

分组测试在配置时,TestNG执行的原则是:只保留最小集合进行执行

看代码:

  1. /** 
  2.  *  
  3.  * <p> 
  4.  * Title: TestngGroups 
  5.  * </p> 
  6.  *  
  7.  * <p> 
  8.  * 对应配置文件testng-groups.xml  
  9.  * Description:使用groups进行分组测试,include和exclude的原则是保留最小集合, 
  10.  * </p> 
  11.  *  
  12.  * <p> 
  13.  * Company: 
  14.  * </p> 
  15.  *  
  16.  * @author : Dragon 
  17.  *  
  18.  * @date : 2014年10月13日 
  19.  */  
  20. public class TestngGroups {  
  21.     @Test(groups = { "functest""checkintest" })  
  22.     public void testMethod1() {  
  23.         System.err.println("groups = { functest, checkintest }");  
  24.     }  
  25.   
  26.     @Test(groups = { "functest""checkintest" })  
  27.     public void testMethod2() {  
  28.         System.err.println("groups = { functest, checkintest }");  
  29.     }  
  30.   
  31.     @Test(groups = { "functest" })  
  32.     public void testMethod3() {  
  33.         System.err.println("groups = { functest }");  
  34.     }  
  35.   
  36.     @Test(groups = { "checkintest" })  
  37.     public void testMethod4() {  
  38.         System.err.println("groups = { checkintest }");  
  39.     }  
  40.   
  41. }  

配置文件:testng-groups.xml
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">  
  3. <suite name="framework_testng">  
  4.     <test verbose="2" name="TestGroups">  
  5.         <groups>  
  6.             <run>  
  7.                 <include name="functest" />  
  8.                 <exclude name="checkintest" />  
  9.             </run>  
  10.         </groups>  
  11.   
  12.         <classes>  
  13.             <class name="com.dragon.testng.annotation.TestngGroups" />  
  14.         </classes>  
  15.     </test>  
  16. </suite>  

执行结果:
  1. groups = { functest }  
  2. PASSED: testMethod3  
  3.   
  4. ===============================================  
  5.     TestGroups  
  6.     Tests run: 1, Failures: 0, Skips: 0  
  7. ===============================================  
  8.   
  9.   
  10. ===============================================  
  11. framework_testng  
  12. Total tests run: 1, Failures: 0, Skips: 0  
  13. ===============================================  

当我们的测试用例累积了很多以后,我们可能不需要测试之前的分组,只要测试刚刚写好的分组,这时候testng提供了一种新的配置方式,来实现这一功能,让测试人员只修改配置文件就完成测试

注意:多个group测试时,xml文件dom顺序必须是'<groups>'标签必须在'<test>'标签内, 否则会 有空指针异常

  1. /** 
  2.  *  
  3.  * <p> 
  4.  * Title: TestngGroupsOfGroups 
  5.  * </p> 
  6.  *  
  7.  * <p> 
  8.  * 参考配置文件:testng-groupsOfGroups.xml 
  9.  * Description:使用<define>标签将测试方法在组内再次进行分组并以name属性进行区分, 
  10.  * <run>通过define标签的name进行调用,以后修改测试直接修改run调用的名称即可 
  11.  *  
  12.  * 注:<b>多个group测试时,xml文件dom顺序必须是'<groups>'标签必须在'<test>'标签内, 否则会 有空指针异常 
  13.  * </p> 
  14.  *  
  15.  * <p> 
  16.  * Company: 
  17.  * </p> 
  18.  *  
  19.  * @author : Dragon 
  20.  *  
  21.  * @date : 2014年10月13日 
  22.  */  
  23. public class TestngGroupsOfGroups {  
  24.   
  25.     @Test(groups = { "windows.xp" })  
  26.     public void testMethod5() {  
  27.         System.err.println("(groups = { windows.xp })");  
  28.     }  
  29.   
  30.     @Test(groups = { "windows.7" })  
  31.     public void testMethod6() {  
  32.         System.err.println("(groups = { windows.7 })");  
  33.     }  
  34.   
  35.     @Test(groups = { "windows.8" })  
  36.     public void testMethod7() {  
  37.         System.err.println("(groups = { windows.8 })");  
  38.     }  
  39. }  
配置文件:testng-groupOfGroups.xml
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">  
  3. <suite name="framework_testng">  
  4.     <test verbose="2" name="TestGroupsOfGroups">  
  5.         <groups>  
  6.             <define name="windows.xp">  
  7.                 <include name="windows.xp" />  
  8.             </define>  
  9.             <define name="windows.7">  
  10.                 <include name="windows.7" />  
  11.             </define>  
  12.             <define name="all">  
  13.                 <include name="windows.*" />  
  14.             </define>  
  15.             <run>  
  16.                 <include name="all" />  
  17.                 <exclude name="windows.7" />  
  18.             </run>  
  19.         </groups>  
  20.         <classes>  
  21.             <class name="com.dragon.testng.annotation.TestngGroupsOfGroups" />  
  22.         </classes>  
  23.     </test>  
  24. </suite>  

测试结果:(注意:此时 被运行的测试分组将在run标签内进行配置,include和exclude时,是根据Define标签的name来决定)
  1. (groups = { windows.xp })  
  2. (groups = { windows.8 })  
  3. PASSED: testMethod5  
  4. PASSED: testMethod7  
  5.   
  6. ===============================================  
  7.     TestGroupsOfGroups  
  8.     Tests run: 2, Failures: 0, Skips: 0  
  9. =============================================== 


testNG参考资料:http://www.yiibai.com/html/testng/2013/0915300.html

                                 http://www.cnblogs.com/TankXiao/p/3888070.html

posted @ 2016-06-03 15:05  午夜飘雪789  阅读(797)  评论(0编辑  收藏  举报