首页 > 解决方案 > 测试多个文件并查看每个文件的结果的最佳方法或 method() 是什么?

问题描述

我正在使用 Intellij IDEA 2019 和 junit 读取一堆文件,处理它们,看看我是否得到了正确的结果。这是我的代码:

@Test
public void testSolution() {
    Solver s;
    Board b;
    int counter = 0;
    final File folder = new File("C:\\Users\\IdeaProjects\\EightPuzzle\\src\\ModifiedTests");

    for (final File fileEntry : folder.listFiles()) {
        System.out.println("processing file: " + fileEntry.getName());
        In in = new In(fileEntry.getAbsolutePath());
        int n = in.readInt();
        int moves = in.readInt();
        int[][] tiles = new int[n][n];

        for (int i = 0; i < n; i++)
            for (int j = 0; j < n; j++)
                tiles[i][j] = in.readInt();

        b = new Board(tiles);
        s = new Solver(b);
        assertEquals(s.moves(), moves);

        counter++;
        if (counter > 10) break;
    }
}

这是一个示例文件:

3
24
6   5   3 
4   1   7 
0   2   8 

有什么方法可以查看哪些文件已被处理,结果如何?我一直在尝试使用 assertEquals 方法中的消息字段,但我不知道访问结果的方法。我可能需要不同的方法?

标签: javatestingintellij-ideajunit

解决方案


如果我理解你的问题是正确的,也许参数化测试是你的解决方案,你的目录中的文件是参数。请检查:

https://junit.org/junit4/javadoc/4.12/org/junit/runners/Parameterized.html


推荐阅读