我想以编程方式使用TestNG运行Test Suite。
我创建了测试结构:XmlSuite - > XmlTest - > TestClass(扩展XmlClass)
我做了三件事:
但是在测试执行中发生的事情是当它进入@Test方法时,对象没有我之前设置的东西。
代码
public class TestRunner {
public static void main(String[] args) {
XmlSuite suite = new XmlSuite();
suite.setName("TmpSuite");
XmlTest test = new XmlTest(suite);
test.setName("TmpTest");
// Add parameter to XmlTest
test.addParameter("testParam1", "clock");
TestClass testClass = new TestClass();
// Add parameter to XmlClass
Map<String, String> testClassParameters = new HashMap<>();
testClassParameters.put("classParam1", "valueOfClassParam1");
testClass.setParameters(testClassParameters);
// Set day variable
testClass.setDay("Friday");
testClass.setClass(TestClass.class);
test.setXmlClasses(Arrays.asList(testClass)) ;
TestNG tng = new TestNG();
tng.setXmlSuites(Arrays.asList(suite));
tng.run();
}
}
public class TestClass extends XmlClass {
private String day;
@Test
public void test1() {
System.out.println("Day: " + day);
System.out.println("LocalParameters: " + getLocalParameters());
System.out.println("AllParameters: " + getAllParameters());
}
public void setDay(String day) {
this.day = day;
}
}
结果:
Day: null
LocalParameters: {}
------------> Exception
答案 0 :(得分:1)
我认为你期望做的是:
public class TestRunner {
public static void main(String[] args) {
// Add parameter to XmlClass
Map<String, String> testClassParameters = new HashMap<>();
testClassParameters.put("classParam1", "valueOfClassParam1");
testClassParameters.put("day", "Friday");
TestClass testClass = new TestClass();
testClass.setParameters(testClassParameters);
testClass.setClass(TestClass.class);
XmlSuite suite = new XmlSuite();
suite.setName("TmpSuite");
XmlTest test = new XmlTest(suite);
test.setName("TmpTest");
// Add parameter to XmlTest
test.addParameter("testParam1", "clock");
test.setXmlClasses(Arrays.asList(testClass)) ;
TestNG tng = new TestNG();
tng.setXmlSuites(Arrays.asList(suite));
tng.run();
}
}
public class TestClass {
@Parameters({ "day", "testParam1", "classParam1" })
@Test
public void test1(String day, String testParam1, String classParam1) {
System.out.println("Day: " + day);
System.out.println("testParam1: " + testParam1);
System.out.println("classParam1: " + classParam1);
}
}