- 创建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"); Listclasses = 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===============================================