Java 从main调用参数化JUnit测试
我有如下所示的测试类,它使用Java 从main调用参数化JUnit测试,java,junit,appium,Java,Junit,Appium,我有如下所示的测试类,它使用input函数中的硬编码参数来初始化测试: @RunWith(Parameterized.class) public class Test1 extends JUnitXMLReporter { private String name, id, platform, version; private Test1 test; public Test1(String name, String id, String platform, String
input
函数中的硬编码参数来初始化测试:
@RunWith(Parameterized.class)
public class Test1 extends JUnitXMLReporter {
private String name, id, platform, version;
private Test1 test;
public Test1(String name, String id, String platform, String version){
//super();
this.name = name;
this.id = id;
this.platform = platform;
this.version = version;
}
@Test
//Login as test user | start menu | logout
public void testLoginLogout() {
[...]
}
@Before
public void initialize() {
test = new Test1(name, id, platform, version);
}
@Parameterized.Parameters
public static String[][] input() {
return new String[][]{{"Nexus 5X API 24", "emulator-5554", "Android", "7.0"},
{"Nexus 5X API 26", "emulator-5556", "Android", "8.0"}};
}
}
我知道我可以调用这个类,所以Result=JUnitCore.runClasses(新的ParallelComputer(false,true),Test1.class)
为硬编码参数并行运行@Test。现在我想改变它,所以现在硬编码的信息是从文件中读取的,最好是从main传递
所以main最好读取XML文件以获取信息,将其放入数组并传递给测试类。如何实现这一点?在@Nikolas的帮助下,我最终使用了globals
System.setProperty(“name”)和System.getProperty(“name”)
。例如:
testClass
//class name & annotation → @RunWith(Parameterized.class)
private String name, id, platform, version;
private testClass test;
public testClass(String name, String id, String platform, String version){
//super();
this.name = name;
this.id = id;
this.platform = platform;
this.version = version;
}
@Test
//whatever test
@Before
public void initialize() {
test = new testClass(name, id, platform, version);
}
@Parameterized.Parameters( name = "{2}, {0}")
public static String[][] input() {
return differentClass.input();
}
differentitclass.input()
所述线程的可能重复并没有解释如何将读取参数从main传递到测试类,而是解释了如何可能地轻松读取文件。非常感谢。
//function reading file and returning correct type, here String[][]
//most important is↓
//File file = new File(System.getProperty("devices"));