运行子目录中的所有测试文件

时间:2018-08-07 10:08:11

标签: java junit junit4 junit-runner

我有一个带有以下tests软件包的项目:

tests
 |- part1
 |- part2
 |- part3
 |- Tests.java

part1part2等是包含带有“ .txt”扩展名的测试数据文件的目录。这样的目录可能有很多,其中包含许多测试文件。

我希望Test.java文件以某种方式成为参数化的junit测试,以使它能够通过并在所有这些子目录中运行所有测试,并向我显示每个目录的结果。例如。像这样:

part1 3/5 pass
 |- test1 PASS
 |- test2 PASS
 |-  .....

我尝试为Test.java编写此文件,但不确定是否完全正确...:-(

@RunWith(Parameterized.class)
public class Tests {

    private final String testName;

    public Tests(String testName) {
        this.testName = testName;
    }

    @Parameters(name = "{0}")
    public static Collection<Object[]> data() {
        ArrayList<Object[]> testcases = new ArrayList<Object[]>();
        for (File f : new File("src/tests").listFiles()) {
            if (f.isFile()) {
                String name = f.getName();
                if (name.endsWith(".txt")) {
                    // Get rid of ".txt" extension
                    String testName = name.substring(0, name.length() - 4);
                    testcases.add(new Object[] { testName });
                }
            }
        }       
        return testcases;
    }

    @Test
    public void valid() throws IOException {
        runTest(this.testName);
    }

    private void runTest(String testname) throws IOException {
        new TestRunner().run(new File("src/tests/"+testname));
    }   
}

请帮助我按子目录分组...谢谢

0 个答案:

没有答案