JUnit5 类级别参数化测试

问题描述 投票:0回答:4

是否可以使用JUnit5的参数化新功能来运行测试类来接收测试参数,而不是在方法级别执行?

使用 JUnit 4,可以使用诸如

@RunWith(Parameterized::class)
之类的运行程序加上继承来将参数数组传递给子类,但我不确定是否可以使用新的 JUnit 5 api 实现等效的效果。

java kotlin junit5 parameterized-unit-test
4个回答
19
投票

简短回答
无法按照 JUnit 4 的风格使用 JUnit 5 参数化 类创建

幸运的是,分离测试逻辑和测试输入数据(参数)的初衷可以以不同的方式实现。


JUnit 5 有自己的进行参数化测试的方法,当然,它与 JUnit 4 不同。新方法不允许在类级别使用参数化装置,即通过其每个测试方法。 因此,每个参数化测试方法都应该使用参数链接进行显式注释。

JUnit 5提供了大量的参数源类型,可以在文档指南

中找到

就您而言,从

Junit 4
@Parameters 迁移的最简单方法是使用
@MethodSource
@ArgumentsSource
org.junit.jupiter.params.provider.*

J单元4

@RunWith(Parameterized.class)
public class MyTestWithJunit4 {
    @Parameters
    public static Collection<Object[]> data() {
      return Arrays.asList(new Object[][] {     
               { 0, 0, 0 },
               { 1, 2, 3 }, 
               { 5, 3, 8 } 
      });
    }

    int first;
    int second;
    int sum;

    public MyTestWithJunit4(int first, int second, int sum) {
      this.first = first;
      this.second = second;
      this.sum = sum;
    }

    @Test
    public void test() {
      assertEquals(sum, first + second));
    }
}

JUnit 5(带有

@MethodSource

class MyTestWithJunit5 {

  @DisplayName("Test with @MethodSource")
  @ParameterizedTest(name = "{index}: ({0} + {1}) => {2})")
  @MethodSource("localParameters")
  void test(int first, int second, int sum) {
    assertEquals(sum, first + second);
  }

  static Stream<Arguments> localParameters() {
    return Stream.of(
        Arguments.of(0, 0, 0),
        Arguments.of(1, 2, 3),
        Arguments.of(5, 3, 8)
    );
  }
}

JUnit 5(带有

@ArgumentsSource

class MyTestWithJunit5 {
  @DisplayName("Test with @ArgumentsSource")
  @ParameterizedTest(name = "{index}: ({0} + {1}) => {2})")
  @ArgumentsSource(Params.class)
  void test(int first, int second, int sum) {
    assertEquals(sum, first + second);
  }

  static class Params implements ArgumentsProvider {
    @Override
    public Stream<? extends Arguments> provideArguments(ExtensionContext context) {
      return Stream.of(
          Arguments.of(0, 0, 0),
          Arguments.of(1, 2, 3),
          Arguments.of(5, 3, 8)
      );
    }
  }
}

考虑到

@MethodSource
中的方法和
@ArgumentsSource
中的类可以在任何地方描述,而不仅仅是在测试方法所在的同一个类中。另外
@MethodSource
允许提供多种源方法,因为它的
value
String[]

一些评论和比较

JUnit 4 中,我们只能有一个提供参数的工厂方法,并且测试应该围绕这些参数构建。 相反,JUnit 5 在绑定参数方面提供了更多的抽象和灵活性,并将测试逻辑与其参数解耦,这是次要的。 这允许独立于参数源构建测试,并在需要时轻松更改它们。

依赖性要求

参数化测试功能不包含在核心中

junit-jupiter-engine
, 但位于单独的依赖项中
junit-jupiter-params


4
投票

创建指定参数化的元注释,并将其应用到测试方法:

public class MyTest {
    @ParameterizedTest(name = "{0}")
    @MethodSource("allImplementations")
    @Retention(RetentionPolicy.RUNTIME)
    private @interface TestAllImplementations {
    }

    static Stream<Arguments> allImplementations() {
        return Stream.of(
             Arguments.of("Serial", new SerialWidget()),
             Arguments.of("Concurrent", new ConcurrentWidget())
        );
    }

    @TestAllImplementations
    void myFirstTest(String name, Widget implementation) {
        /* ... */
    }

    @TestAllImplementations
    void mySecondTest(String name, Widget implementation) {
        /* ... */
    }
}

0
投票
@ParameterizedTest()
@MethodSource("dataProvider")
void testWithDataProvider(String str, List<JSONObject> list) {
    System.out.println();

}

static Stream<Arguments> dataProvider() {
    String json = """
            {
                "test":1 
            }
                    """;
    return Stream.of(
            arguments("abc", Arrays.asList(new JSONObject(json))),
            arguments("lemon", Arrays.asList(new JSONObject(json)))
    );

我通过这种方式绕过了这个复杂性


0
投票

我能找到的最好的解决方案是使旧的测试变得抽象。 然后创建一个新类,其中包含每个变体的嵌套测试,例如。

public class NewParametricTest {
  @Nested
  public class NewParametricTestVariation1Test
    extends AbstractOriginalToBeParameticizedTest {
    public NewParametricTestVariation1Test() {
      super("Some parameter", 1);
    }
  }

  // add others here
}
© www.soinside.com 2019 - 2024. All rights reserved.