TestNG.xml参数配置-如何控制部分执行@test方法

来源:互联网 发布:淘宝描述代码 编辑:程序博客网 时间:2024/06/08 13:29

测试的过程中,我们经常遇到只想执行脚本中某一部分的测试用例,如果每次都通过注释掉不需要执行的那部分脚本的方式,一个是容易出错,二是比较花费时间和精力,这时就可以通过testng.xml中的一些参数配置来设置。

1)利用groups参数

测试脚本为:

public class test{      @Test(groups = { "A", "B" })      public void testMethod1() {          System.err.println("groups = {A,B}");      }        @Test(groups = { "A" })      public void testMethod2() {          System.err.println("groups = {A}");      }    @Test(groups = { "B" })      public void testMethod3() {          System.err.println("groups = {B}");      } } 

xml文件为:

1.<?xml version="1.0" encoding="UTF-8"?>2.<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >3.<suite name="suite1">4. <groups>  5.            <run>  6.                <include name="A" />  7.                <exclude name="B" />  8.            </run>  9.        </groups>  11.<test name="test" preserve-order="true">12.<parameter name="url" value="http://123.57.56.45:7778/initLogin" />13.<parameter name="username" value="999111" />14.<parameter name="password" value="111111" />15.<classes >16.<class name="com.testng.test"/ >18.</classes>19.</test>20.</suite>

执行结果:只会执行testMethod2

(2)利用methods中的include和exclude 

如果在methods中标识了@test的方法,也可以在method中通过include和exclude来控制需要执行哪些方法

测试脚本为:

public class test{      @Test    public void testMethod1() {          System.out.println("testMethod1");      }        @Test    public void testMethod2() {          System.out.println("testMethod2");      }
@Test    public void testMethod3() {          System.out.println("testMethod3");      } } 

xml文件为:

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" ><suite name="suite1"><test name="test" preserve-order="true"><parameter name="url" value="http://123.57.56.45:7778/initLogin" /><parameter name="username" value="999111" /><parameter name="password" value="111111" /><classes ><class name="com.testng.test" ><methods><include name="testMethod1" /><exclude name="testMethod2" /><include name="testMethod3" /></methods></class></classes></test></suite>

执行结果:先执行testMethod1,再执行testMethod3,这里也可以起到一个控制@test方法执行的作用,不需要在脚本中再添加priority参数。

如果以上methods中的三个都为include,则执行顺序为testMethod1testMethod2,testMethod3.

3 0
原创粉丝点击