TA的每日心情 | 无聊 2024-11-5 10:03 |
---|
签到天数: 77 天 连续签到: 1 天 [LV.6]测试旅长
|
前面写过一个无参的数据提供者方法,还有三种有参数据提供者方法。
一、Method表示当前测试的方法
- <p>Dataprovidertest.java代码</p><p>
- </p><p>package com.tzf.testng;</p><p> </p><p>import java.lang.reflect.Method;</p><p>import org.testng.annotations.DataProvider;</p><p>import org.testng.annotations.Test;</p><p> </p><p>public class DataProviderTest {</p><p> </p><p> @DataProvider</p><p> public Object[][] providerMethod(Method method){</p><p> Object[][] result = null;</p><p> if(method.getName().equals("testmethod1")){</p><p> result = new Object[][]{new Object[]{1}};</p><p> }else if(method.getName().equals("testmethod2")){</p><p> result = new Object[][]{new Object[]{2}};</p><p> }else{</p><p> result = new Object[][]{new Object[]{3}};</p><p> }</p><p> return result;</p><p> }</p><p> </p><p> @Test(dataProvider="providerMethod")</p><p> public void testmethod1(int param){</p><p> System.out.println("method1 received:"+param);</p><p> }</p><p> </p><p> @Test(dataProvider="providerMethod")</p><p> public void testmethod2(int param){</p><p> System.out.println("method2 received:"+param);</p><p> }</p><p> </p><p> @Test(dataProvider="providerMethod")</p><p> public void testmethod3(int param){</p><p> System.out.println("method3 received:"+param);</p><p> }</p><p>}</p><p> 注意这里我的Dataprovider的name值没有指定,默认用的是方法名。 Testng.xml代码</p><p>
- </p><p>复制代码</p><p><?xml version="1.0" encoding="UTF-8"?></p><p><suite name="Suite" parallel="false"></p><p> <test name="DataProviderTest"></p><p> <classes></p><p> <class name="com.tzf.testng.DataProviderTest" /></p><p> </classes></p><p> </test></p><p></suite></p><p>复制代码</p>
复制代码
运行结果代码
RemoteTestNG starting
method1 received:1
method2 received:2
method3 received:3
RemoteTestNG finishing: 145 ms
二、ITestContent当前测试的上下文信息
Dataprovidertest2.java代码
复制代码
- <p>package com.tzf.testng;</p><p>
- </p><p>import java.util.Random;</p><p>
- </p><p>import org.testng.ITestContext;</p><p>import org.testng.annotations.DataProvider;</p><p>import org.testng.annotations.Test;</p><p>
- </p><p>public class DataProviderTest2 {</p><p> </p><p> @DataProvider</p><p> public Object[][] providerMethod(ITestContext context){</p><p> String[] groups = context.getIncludedGroups();</p><p> int size = 2;</p><p> for(String group : groups){</p><p> if(group.equals("function-test")){</p><p> size = 10 ;</p><p> break;</p><p> }</p><p> }</p><p> Object[][] result = new Object[size][];</p><p> Random r = new Random();</p><p> for(int i=0;i<size;i++){</p><p> result[i] = new Object[]{new Integer(r.nextInt())};</p><p> }</p><p> return result;</p><p> }</p><p> </p><p> @Test(dataProvider="providerMethod",groups={"function-test","unit-test"})</p><p> public void testmethod1(Integer n){</p><p> System.out.println("method1 received:"+n);</p><p> }</p><p> </p><p>}</p><p>复制代码</p><p>Testng.xml代码</p><p>
- </p><p>复制代码</p><p><?xml version="1.0" encoding="UTF-8"?></p><p><suite name="Suite" parallel="false"></p><p> <test name="DataProviderTest2"></p><p> <groups></p><p> <run></p><p> <include name="function-test"/></p><p> <exclude name="unit-test"/></p><p> </run></p><p> </groups></p><p> <classes></p><p> <class name="com.tzf.testng.DataProviderTest2" group="function-test"/></p><p> </classes></p><p> </test></p><p></suite></p><p>复制代码</p><p>运行结果代码</p><p>
- </p><p>复制代码</p><p>RemoteTestNG starting</p><p>method1 received:-1987654923</p><p>method1 received:338477914</p><p>method1 received:1910366338</p><p>method1 received:107205369</p><p>method1 received:-1018164959</p><p>method1 received:-2060129321</p><p>method1 received:-803464435</p><p>method1 received:-814156781</p><p>method1 received:-1075504680</p><p>method1 received:-774924512</p><p>RemoteTestNG finishing: 151 ms</p>
复制代码
|
|