循环遍历数组,每个元素一个 JUnit 测试

发布于 2024-10-03 10:20:03 字数 2600 浏览 0 评论 0原文

我有一个 JUnit 4 测试,它循环访问一组测试数据:

public @Test void testAll() {

    final Object[][] sets = new Object[][] {
            // SET                              TYPE VALUE

            // --- valid sets

            // groups
            x(s(A,1, B,1, C,1),                 G),
            x(s(A,4, B,4, D,4),                 G),
            x(s(A,8, B,8, D,8, C,8),            G),
            x(s(J,J, B,4, D,4),                 G,  4*3),
            x(s(A,9, J,J, D,9),                 G,  9*3),
            x(s(A,2, B,2, C,2),                 G),
            x(s(A,4, B,4, J,J),                 G,  4*3),
            x(s(A,4, B,4, C,4, D,4),            G),

            // runs
            x(s(A,1, A,2, A,3),                 R),
            x(s(B,8, B,9, B,10),                R),
            x(s(J,J, C,2, C,3),                 R,  6),
            x(s(D,8, D,9, J,J, D,11),           R,  38),
            x(s(D,8, D,9, J,J, J,J),            R,  38),

            // sames
            x(s(A,1, A,1),                      S),
            x(s(B,4, B,4, B,4),                 S),
            x(s(C,8, C,8),                      S),
            x(s(D,3, D,3),                      S),

            // doubt-cases, assume group (TODO: verify this is correct)
            x(s(J,J, J,J, D,4),                 G,  4*3),
            x(s(A,7, J,J, J,J),                 G,  7*3),
            x(s(J,J, D,9, J,J),                 G,  9*3),
            x(s(J,J, J,J, J,J),                 G,  1),

            // --- invalid sets
            x(s(B,1, A,2, A,3),                 I), // not same colour
            x(s(D,11, D,12, J,J, J,J),          I), // last joker is 14
            x(s(B,1, B,1, A,1),                 I), // duplicate B1
            x(s(A,1, A,2, A,3, A,5),            I), // gap A4
            x(s(J,J, A,1, J,J, B,1, C,1),       I), // one J replaces D1, then nothing left to replace
            x(s(A,1, A,2),                      I), // short
            x(s(B,1),                           I), // shorter
            x(s(A,5, A,6),                      I), // short
    };

    for (Object[] o : sets) {

        TileSet s = (TileSet) o[0];
        Type t = (Type) o[1];
        int v = (Integer) o[2];

        System.out.println(s);

        assertEquals(t, s.getType());
        assertEquals(v, s.getValue());

        // test isValid, though it's Too Simple To Break(R)
        if (t == Type.INVALID) assertFalse(s.isValid());
        else assertTrue(s.isValid());
    }

}

因为它全部在一个测试方法中,所以一旦数组中的一个元素失败,整个测试就会停止。有没有办法解决这个问题,而不为每个测试项目制定方法? 也许有一些反思?

I have a JUnit 4 test that loops through an array of test data:

public @Test void testAll() {

    final Object[][] sets = new Object[][] {
            // SET                              TYPE VALUE

            // --- valid sets

            // groups
            x(s(A,1, B,1, C,1),                 G),
            x(s(A,4, B,4, D,4),                 G),
            x(s(A,8, B,8, D,8, C,8),            G),
            x(s(J,J, B,4, D,4),                 G,  4*3),
            x(s(A,9, J,J, D,9),                 G,  9*3),
            x(s(A,2, B,2, C,2),                 G),
            x(s(A,4, B,4, J,J),                 G,  4*3),
            x(s(A,4, B,4, C,4, D,4),            G),

            // runs
            x(s(A,1, A,2, A,3),                 R),
            x(s(B,8, B,9, B,10),                R),
            x(s(J,J, C,2, C,3),                 R,  6),
            x(s(D,8, D,9, J,J, D,11),           R,  38),
            x(s(D,8, D,9, J,J, J,J),            R,  38),

            // sames
            x(s(A,1, A,1),                      S),
            x(s(B,4, B,4, B,4),                 S),
            x(s(C,8, C,8),                      S),
            x(s(D,3, D,3),                      S),

            // doubt-cases, assume group (TODO: verify this is correct)
            x(s(J,J, J,J, D,4),                 G,  4*3),
            x(s(A,7, J,J, J,J),                 G,  7*3),
            x(s(J,J, D,9, J,J),                 G,  9*3),
            x(s(J,J, J,J, J,J),                 G,  1),

            // --- invalid sets
            x(s(B,1, A,2, A,3),                 I), // not same colour
            x(s(D,11, D,12, J,J, J,J),          I), // last joker is 14
            x(s(B,1, B,1, A,1),                 I), // duplicate B1
            x(s(A,1, A,2, A,3, A,5),            I), // gap A4
            x(s(J,J, A,1, J,J, B,1, C,1),       I), // one J replaces D1, then nothing left to replace
            x(s(A,1, A,2),                      I), // short
            x(s(B,1),                           I), // shorter
            x(s(A,5, A,6),                      I), // short
    };

    for (Object[] o : sets) {

        TileSet s = (TileSet) o[0];
        Type t = (Type) o[1];
        int v = (Integer) o[2];

        System.out.println(s);

        assertEquals(t, s.getType());
        assertEquals(v, s.getValue());

        // test isValid, though it's Too Simple To Break(R)
        if (t == Type.INVALID) assertFalse(s.isValid());
        else assertTrue(s.isValid());
    }

}

Because it's all in one test method, the whole test stops as soon as one element in the array fails. Is there a way around that, without making a method for each test item?
Maybe something with reflection?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

隱形的亼 2024-10-10 10:20:03

捕获 AssertionError 并将捕获的错误添加到错误列表中,最后检查列表是否为空,如果不是,则引发复合 AssertionError

catch AssertionError and add the caught error to the errors list, at the end check the list to be empty raise a compound AssertionError if not.

梦忆晨望 2024-10-10 10:20:03

我认为,通过更好的语法,JUnit 5 中的参数化测试变得更加容易。您可以在此处阅读有关内容。其中一个选项是声明一个方法来提供要测试的参数列表,如下所示:

// Given
private static Stream<Arguments> shouldReturnExpectedResultWhenGivenRightInput() {
   return Stream.of(
       Arguments.of(3, "Fizz"),
       Arguments.of(5, "Buzz"),
       Arguments.of(8, "8"),
       Arguments.of(15, "FizzBuzz"),
       Arguments.of(19, "19"),
       Arguments.of(30, "FizzBuzz"),
       Arguments.of(40, "Buzz"),
       Arguments.of(45, "FizzBuzz")
   );
}
@ParameterizedTest
@MethodSource
void shouldReturnExpectedResultWhenGivenRightInput(Integer input, String expected) {
   // When
   val result = library.processInput(input); 
   // Then
   assertEquals(expected, result);
}

// Given
private static Stream<Arguments> shouldNotReturnExpectedResultWhenGivenWrongInput() {
    return Stream.of(
        Arguments.of(3, "Buzz"),
        Arguments.of(5, "Fizz"),
        Arguments.of(8, "Buzz"),
        Arguments.of(15, "15"),
        Arguments.of(19, "Fizz")
    );
}
@ParameterizedTest
@MethodSource
void shouldNotReturnExpectedResultWhenGivenWrongInput(Integer input, String unexpected) {
    // When
    val result = library.processInput(input); 
    // Then
    assertNotEquals(unexpected, result);
}

Parameterized Tests in JUnit 5 have got easier with a better syntax, I think. You can read about here. One of the options is to declare a method to provide the list of arguments to test, such as below:

// Given
private static Stream<Arguments> shouldReturnExpectedResultWhenGivenRightInput() {
   return Stream.of(
       Arguments.of(3, "Fizz"),
       Arguments.of(5, "Buzz"),
       Arguments.of(8, "8"),
       Arguments.of(15, "FizzBuzz"),
       Arguments.of(19, "19"),
       Arguments.of(30, "FizzBuzz"),
       Arguments.of(40, "Buzz"),
       Arguments.of(45, "FizzBuzz")
   );
}
@ParameterizedTest
@MethodSource
void shouldReturnExpectedResultWhenGivenRightInput(Integer input, String expected) {
   // When
   val result = library.processInput(input); 
   // Then
   assertEquals(expected, result);
}

// Given
private static Stream<Arguments> shouldNotReturnExpectedResultWhenGivenWrongInput() {
    return Stream.of(
        Arguments.of(3, "Buzz"),
        Arguments.of(5, "Fizz"),
        Arguments.of(8, "Buzz"),
        Arguments.of(15, "15"),
        Arguments.of(19, "Fizz")
    );
}
@ParameterizedTest
@MethodSource
void shouldNotReturnExpectedResultWhenGivenWrongInput(Integer input, String unexpected) {
    // When
    val result = library.processInput(input); 
    // Then
    assertNotEquals(unexpected, result);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文