运行TestNG以编程方式需要循环自动创建多个测试 [英] Running TestNG programmatically need loop to create multiple tests automatically
问题描述
我想知道有人能给我正确的方向吗?不确定是否正确使用Map或HashMap进行循环。我知道它有效,如果我做一个参数,但我喜欢做1-100主机参数,当我以编程方式创建我的XML。你能告诉我我需要做什么来创建一个循环,所以我可以创建多个测试与参数与主机值1-100。我的代码如下:
I was wondering if someone can give me the right direction for this. Not sure if I'm using Map or HashMap correctly for loop. I know it works if I do one parameters but I like to do 1-100 host parameters when I create my XML programmatically. Can you show me what I need to do for creating a loop so I can create multiple test with parameter with host value 1-100. My code is as below:
package firsttestngpackage;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.TestNG;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;
public class Test1 {
WebDriver driver;
WebDriverWait wait;
private void testRunner(Map<String, String> testngParams) {
TestNG testNG = new TestNG();
XmlSuite suite = getXmlSuite();
XmlTest test = getXmlTest(suite);
test.setParameters(testngParams);
List<XmlClass> classes = getXmlClasses();
test.setXmlClasses(classes);
List<XmlTest> tests = new ArrayList<XmlTest>();
tests.add(test);
suite.setTests(tests);
List<XmlSuite> suites = new ArrayList<XmlSuite>();
suites.add(suite);
testNG.setXmlSuites(suites);
testNG.run();
}
private XmlSuite getXmlSuite() {
XmlSuite suite = new XmlSuite();
suite.setName("Test Suite");
return suite;
}
private XmlTest getXmlTest(XmlSuite suite) {
XmlTest test = new XmlTest(suite);
test.setName("test_with_firefox");
return test;
}
private List<XmlClass> getXmlClasses() {
List<XmlClass> classez = new ArrayList<XmlClass>();
classez.add(new XmlClass("firsttestngpackage.Test5"));
return classez;
}
public static void main(String args[]) {
Test1 program = new Test1();
Map<String, String> params = new HashMap<String, String>();
//Need THIS CONFIGURATION LOOP TO WORK
//NEED HELP HERE!!!!!!!!!!!!!!
for (int hostnum = 1; hostnum <= 100; hostnum++){
params.put("host", hostnum);
}
//THIS CONFIGURATION WORKS, BUT NO LOOP
//params.put("host", "10");
program.testRunner(params);
}
}
这是预期的XML,但是是100次
This is the Expected XML but 100 times
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="TestSuite" thread-count="3" >
<test name="1">
<parameter name="host" value="1" />
<classes>
<class name="firsttestngpackage.Test5">
</class>
</classes>
</test>
<test name="2">
<parameter name="host" value="2" />
<classes>
<class name="firsttestngpackage.Test5">
</class>
</classes>
</test>
<test name="3">
<parameter name="host" value="3" />
<classes>
<class name="firsttestngpackage.Test5">
</class>
</classes>
</test>
</suite>
我收到语法错误
-Syntax错误,将Dimension插入完整的ReferenceType
I am getting syntax error
-Syntax error, insert "Dimension" to complete ReferenceType
推荐答案
尝试以下代码:
public static void main(String[] args) {
/**
* Get the number of hosts (for example from the arguments,
* or from a file, or from System.getenv, etc...)
*/
Integer numberOfHosts = this.getNumberOfHosts();
// Creating a new Suite
XmlSuite suite = new XmlSuite();
for (Integer i = 1; i <= numberOfHosts; i++) {
// Creating a new Test
XmlTest test = new XmlTest(suite);
// Set Test name
test.setName("test-number-" + i);
// New list for the parameters
Map<String, String> testParams = new HashMap<String, String>();
// Add parameter to the list
testParams.put("host", String.valueOf(i));
// Add parameters to test
test.setParameters(testParams);
// New list for the classes
List<XmlClass> classes = new ArrayList<XmlClass>();
// Putting the classes to the list
classes.add(new XmlClass("firsttestngpackage.Test5"));
// Add classes to test
test.setClasses(classes);
}
// New list for the Suites
List<XmlSuite> suites = new ArrayList<XmlSuite>();
// Add suite to the list
suites.add(suite);
// Creating the xml
TestListenerAdapter tla = new TestListenerAdapter();
TestNG tng = new TestNG();
tng.setXmlSuites(suites);
tng.addListener(tla);
tng.run();
}
这篇关于运行TestNG以编程方式需要循环自动创建多个测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!