从main调用参数化的JUnit测试

时间:2018-08-09 14:27:08

标签: 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 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 result = JUnitCore.runClasses(new ParallelComputer(false, true), Test1.class);可以为硬编码参数并行运行@Test。现在我要更改它,以便现在从文件中读取硬编码的信息,最好从main传递 因此,最好main读取XML文件以获取信息,将其放入数组中并传递给Test类。我该如何实现?

1 个答案:

答案 0 :(得分:0)

在@Nikolas的帮助下,这些注释指向读取文件过程,我终于使用了全局变量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();
}

differentClass.input()

//function reading file and returning correct type, here String[][]
//most important is↓
//File file = new File(System.getProperty("devices"));