博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
TestNG的DataProvider和Factory使用
阅读量:6070 次
发布时间:2019-06-20

本文共 8282 字,大约阅读时间需要 27 分钟。

hot3.png

  1. 创建Test Case
import org.testng.annotations.Test;public class TableCheckTest {    private String key;    private String tableName;    private String timeStamp;    public TableCheckTest(String key, String tableName, String timeStamp) {        this.key = key;        this.tableName = tableName;        this.timeStamp = timeStamp;    }    @Test(groups = {"nifi"}, enabled = true)    public void test1() {        System.out.println("test case 1:" + key);    }    @Test(enabled = true, dependsOnMethods = {"test1"})    public void test2() {        System.out.println("test case 2:" + key);    }    @Test(enabled = true, dependsOnMethods = {"test2"})    public void test3() {        System.out.println("test case 3:" + key);    }    @Test(enabled = true, dependsOnMethods = {"test3"})    public void test4() {        System.out.println("test case 4:" + key);    }    @Test(enabled = true, dependsOnMethods = {"test4"})    public void test5() {        System.out.println("test case 5:" + key);    }}

 2. 使用Factory和DataProvider annotation给测试用例提供数据

import org.testng.annotations.DataProvider;import org.testng.annotations.Factory;public class TableCheck {    @Factory(dataProvider = "kafkaInfo")    public Object[] createInstance(String key, String tableName, String timeStamp) {        return new Object[] { new TableCheckTest(key, tableName, timeStamp)};    }    @DataProvider(name="kafkaInfo")    public Object[][] table()  {        String[][] array = new String[][] {                {"2e380c20-858d-45e4-961d-1aac7682ac77", "DF_CAL_COMP_COMMENT_AF_A", "1522749597922"},                {"32d834cd-6628-4dea-bffc-29be00b73178", "DF_CAL_FORM_FEEDBACK_AF_A", "1522749598125"},                {"55c84640-aec1-46b8-88e2-990ddd567fe5", "DF_CAL_FEEDBACK_AF_A", "1522749598016"},        };        return array;    }}

3. 创建主入口测试类

import org.testng.TestNG;import org.testng.xml.XmlClass;import org.testng.xml.XmlSuite;import org.testng.xml.XmlTest;import java.util.ArrayList;import java.util.List;public class MainClass {    public static void main(String[] args) {        XmlSuite suite = new XmlSuite();        suite.setName("MyTestSuite");        List
classes = new ArrayList
(); classes.add(new XmlClass("TableCheck")); XmlTest test = new XmlTest(suite); test.setName("MyTests"); test.setXmlClasses(classes); List
suites = new ArrayList
(); suites.add(suite); TestNG testNG = new TestNG(); testNG.setXmlSuites(suites); testNG.setVerbose(2); testNG.run(); }}

4. 运行MainClass

C:\AutoBBM\sapjvm_8\bin\java "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.3.1\lib\idea_rt.jar=56069:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.3.1\bin" -Dfile.encoding=UTF-8 -classpath C:\AutoBBM\sapjvm_8\jre\lib\charsets.jar;C:\AutoBBM\sapjvm_8\jre\lib\ext\access-bridge-64.jar;C:\AutoBBM\sapjvm_8\jre\lib\ext\cldrdata.jar;C:\AutoBBM\sapjvm_8\jre\lib\ext\dnsns.jar;C:\AutoBBM\sapjvm_8\jre\lib\ext\jaccess.jar;C:\AutoBBM\sapjvm_8\jre\lib\ext\localedata.jar;C:\AutoBBM\sapjvm_8\jre\lib\ext\nashorn.jar;C:\AutoBBM\sapjvm_8\jre\lib\ext\sunec.jar;C:\AutoBBM\sapjvm_8\jre\lib\ext\sunjce_provider.jar;C:\AutoBBM\sapjvm_8\jre\lib\ext\sunmscapi.jar;C:\AutoBBM\sapjvm_8\jre\lib\ext\sunpkcs11.jar;C:\AutoBBM\sapjvm_8\jre\lib\ext\zipfs.jar;C:\AutoBBM\sapjvm_8\jre\lib\jce.jar;C:\AutoBBM\sapjvm_8\jre\lib\jsse.jar;C:\AutoBBM\sapjvm_8\jre\lib\management-agent.jar;C:\AutoBBM\sapjvm_8\jre\lib\resources.jar;C:\AutoBBM\sapjvm_8\jre\lib\rt.jar;C:\Users\I318869\Documents\workplace\test\out\test\classes;C:\Users\I318869\Documents\workplace\test\out\production\classes;C:\Users\I318869\.gradle\caches\modules-2\files-2.1\org.testng\testng\6.14.2\10c93c2c0d165e895a7582dfd8b165f108658db5\testng-6.14.2.jar;C:\Users\I318869\.gradle\caches\modules-2\files-2.1\com.beust\jcommander\1.72\6375e521c1e11d6563d4f25a07ce124ccf8cd171\jcommander-1.72.jar;C:\Users\I318869\.gradle\caches\modules-2\files-2.1\org.apache-extras.beanshell\bsh\2.0b6\fb418f9b33a0b951e9a2978b4b6ee93b2707e72f\bsh-2.0b6.jar MainClasstest case 1:32d834cd-6628-4dea-bffc-29be00b73178test case 1:2e380c20-858d-45e4-961d-1aac7682ac77test case 1:55c84640-aec1-46b8-88e2-990ddd567fe5test case 2:32d834cd-6628-4dea-bffc-29be00b73178test case 2:2e380c20-858d-45e4-961d-1aac7682ac77test case 2:55c84640-aec1-46b8-88e2-990ddd567fe5test case 3:32d834cd-6628-4dea-bffc-29be00b73178test case 3:2e380c20-858d-45e4-961d-1aac7682ac77test case 3:55c84640-aec1-46b8-88e2-990ddd567fe5test case 4:32d834cd-6628-4dea-bffc-29be00b73178test case 4:2e380c20-858d-45e4-961d-1aac7682ac77test case 4:55c84640-aec1-46b8-88e2-990ddd567fe5test case 5:32d834cd-6628-4dea-bffc-29be00b73178test case 5:2e380c20-858d-45e4-961d-1aac7682ac77test case 5:55c84640-aec1-46b8-88e2-990ddd567fe5PASSED: test1PASSED: test1PASSED: test1PASSED: test2PASSED: test2PASSED: test2PASSED: test3PASSED: test3PASSED: test3PASSED: test4PASSED: test4PASSED: test4PASSED: test5PASSED: test5PASSED: test5===============================================    MyTests    Tests run: 15, Failures: 0, Skips: 0==============================================================================================MyTestSuiteTotal tests run: 15, Failures: 0, Skips: 0===============================================Process finished with exit code 0

5. 如果想要用第一条数据跑完所有test method再用第二条数据跑test method, 可在MainClass中设置

test.setGroupByInstances(true);

再次运行测试MainClass,得到结果

C:\AutoBBM\sapjvm_8\bin\java "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.3.1\lib\idea_rt.jar=56115:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2017.3.1\bin" -Dfile.encoding=UTF-8 -classpath C:\AutoBBM\sapjvm_8\jre\lib\charsets.jar;C:\AutoBBM\sapjvm_8\jre\lib\ext\access-bridge-64.jar;C:\AutoBBM\sapjvm_8\jre\lib\ext\cldrdata.jar;C:\AutoBBM\sapjvm_8\jre\lib\ext\dnsns.jar;C:\AutoBBM\sapjvm_8\jre\lib\ext\jaccess.jar;C:\AutoBBM\sapjvm_8\jre\lib\ext\localedata.jar;C:\AutoBBM\sapjvm_8\jre\lib\ext\nashorn.jar;C:\AutoBBM\sapjvm_8\jre\lib\ext\sunec.jar;C:\AutoBBM\sapjvm_8\jre\lib\ext\sunjce_provider.jar;C:\AutoBBM\sapjvm_8\jre\lib\ext\sunmscapi.jar;C:\AutoBBM\sapjvm_8\jre\lib\ext\sunpkcs11.jar;C:\AutoBBM\sapjvm_8\jre\lib\ext\zipfs.jar;C:\AutoBBM\sapjvm_8\jre\lib\jce.jar;C:\AutoBBM\sapjvm_8\jre\lib\jsse.jar;C:\AutoBBM\sapjvm_8\jre\lib\management-agent.jar;C:\AutoBBM\sapjvm_8\jre\lib\resources.jar;C:\AutoBBM\sapjvm_8\jre\lib\rt.jar;C:\Users\I318869\Documents\workplace\test\out\test\classes;C:\Users\I318869\Documents\workplace\test\out\production\classes;C:\Users\I318869\.gradle\caches\modules-2\files-2.1\org.testng\testng\6.14.2\10c93c2c0d165e895a7582dfd8b165f108658db5\testng-6.14.2.jar;C:\Users\I318869\.gradle\caches\modules-2\files-2.1\com.beust\jcommander\1.72\6375e521c1e11d6563d4f25a07ce124ccf8cd171\jcommander-1.72.jar;C:\Users\I318869\.gradle\caches\modules-2\files-2.1\org.apache-extras.beanshell\bsh\2.0b6\fb418f9b33a0b951e9a2978b4b6ee93b2707e72f\bsh-2.0b6.jar MainClasstest case 1:55c84640-aec1-46b8-88e2-990ddd567fe5test case 2:55c84640-aec1-46b8-88e2-990ddd567fe5test case 3:55c84640-aec1-46b8-88e2-990ddd567fe5test case 4:55c84640-aec1-46b8-88e2-990ddd567fe5test case 5:55c84640-aec1-46b8-88e2-990ddd567fe5test case 1:2e380c20-858d-45e4-961d-1aac7682ac77test case 2:2e380c20-858d-45e4-961d-1aac7682ac77test case 3:2e380c20-858d-45e4-961d-1aac7682ac77test case 4:2e380c20-858d-45e4-961d-1aac7682ac77test case 5:2e380c20-858d-45e4-961d-1aac7682ac77test case 1:32d834cd-6628-4dea-bffc-29be00b73178test case 2:32d834cd-6628-4dea-bffc-29be00b73178test case 3:32d834cd-6628-4dea-bffc-29be00b73178test case 4:32d834cd-6628-4dea-bffc-29be00b73178test case 5:32d834cd-6628-4dea-bffc-29be00b73178PASSED: test1PASSED: test2PASSED: test3PASSED: test4PASSED: test5PASSED: test1PASSED: test2PASSED: test3PASSED: test4PASSED: test5PASSED: test1PASSED: test2PASSED: test3PASSED: test4PASSED: test5===============================================    MyTests    Tests run: 15, Failures: 0, Skips: 0==============================================================================================MyTestSuiteTotal tests run: 15, Failures: 0, Skips: 0===============================================

 

转载于:https://my.oschina.net/lyle001/blog/1789689

你可能感兴趣的文章
边缘控制平面Ambassador全解读
查看>>
Windows Phone 7 利用计时器DispatcherTimer创建时钟
查看>>
程序员最喜爱的12个Android应用开发框架二(转)
查看>>
vim学习与理解
查看>>
DIRECTSHOW在VS2005中PVOID64问题和配置问题
查看>>
MapReduce的模式,算法以及用例
查看>>
《Advanced Linux Programming》读书笔记(1)
查看>>
zabbix agent item
查看>>
一步一步学习SignalR进行实时通信_7_非代理
查看>>
AOL重组为两大业务部门 全球裁员500人
查看>>
字符设备与块设备的区别
查看>>
为什么我弃用GNOME转向KDE(2)
查看>>
Redis学习记录初篇
查看>>
爬虫案例若干-爬取CSDN博文,糗事百科段子以及淘宝的图片
查看>>
Web实时通信技术
查看>>
第三章 计算机及服务器硬件组成结合企业运维场景 总结
查看>>
IntelliJ IDEA解决Tomcal启动报错
查看>>
默认虚拟主机设置
查看>>
七周五次课(1月26日)
查看>>
Linux系统一些系统查看指令
查看>>