原文: https://howtodoinjava.com/junit/how-to-write-parameterized-testcases-with-junit-4/

在本 JUnit 教程中,学习创建和执行 junit 参数化测试。 参数化测试是正常测试,它使用不同的测试参数反复执行。 它可以帮助开发人员在使用不同输入类型执行相同测试以测试函数健壮性,以及可能的函数边界上面节省时间。

1. JUnit Maven 依赖项

下面是 maven 依赖项,我们应该在测试示例代码之前添加到您的 maven 项目中。

  1. <!-- Junit -->
  2. <dependency>
  3. <groupId>junit</groupId>
  4. <artifactId>junit</artifactId>
  5. <version>4.11</version>
  6. <scope>test</scope>
  7. </dependency>
  8. <dependency>
  9. <groupId>junit</groupId>
  10. <artifactId>junit-dep</artifactId>
  11. <version>4.11</version>
  12. <scope>test</scope>
  13. </dependency>

2. 具有构造器参数的 JUnit 参数化测试

参数化测试使用@RunWith注解以及@Parameters注解来馈送输入。

2.1 要测试的类

下面是测试类,我们将为其编写测试用例。

  1. package corejava.test.junit;
  2. public final class MathUtils
  3. {
  4. //Return square of a function
  5. public static int square(final int number) {
  6. return number * number;
  7. }
  8. }

2.2 参数化测试

让我们为上述数学工具类编写参数化测试。

  1. package corejava.test.junit;
  2. import java.util.Arrays;
  3. import org.junit.Assert;
  4. import org.junit.Test;
  5. import org.junit.runner.RunWith;
  6. import org.junit.runners.Parameterized;
  7. import org.junit.runners.Parameterized.Parameters;
  8. @RunWith(Parameterized.class)
  9. public class JunitTestsWithParameters {
  10. // @Parameters annotation marks this method as parameters provider
  11. @Parameters(name = "Run #Square of : {0}^2={1}")
  12. public static Iterable<Object []> data()
  13. {
  14. return Arrays.asList(new Object[][] { { 1, 1 },
  15. { 2, 4 },
  16. { 3, 19 },
  17. { 4, 16 },
  18. { 5, 25 } });
  19. }
  20. // Our two parameters
  21. private final int input;
  22. private final int resultExpected;
  23. // Constructor is initialized with one set of parameters every time
  24. public JunitTestsWithParameters(final int input, final int result)
  25. {
  26. this.input = input;
  27. this.resultExpected = result;
  28. }
  29. @Test
  30. public void testUserMapping() {
  31. // You can use here assert also
  32. Assert.assertEquals(resultExpected, MathUtils.square(input));
  33. }
  34. }

请注意:

  1. 我们必须按照给定的方式声明参数。
  2. 参数传递给类的构造器以设置变量,因此可以在测试用例中使用。
  3. 参数类的返回类型为“List[]”,要使用的数据类型已限于字符串或原始值

现在检查程序输出。

JUnit 参数化测试示例 - 图1

测试执行结果

3. 带有字段注入的 JUnit 参数化测试

为了传递参数进行测试,我们可以通过字段注入传递参数,而不是通过构造器传递参数。 在这种方法中,我们声明确切的字段数作为输入参数。 每个字段一个参数。

让我们通过场注入重新测试我们的MathUtils类。 请注意,我们如何用@Parameter注解的字段替换构造器。

  1. import org.junit.Assert;
  2. import org.junit.Test;
  3. import org.junit.runner.RunWith;
  4. import org.junit.runners.Parameterized;
  5. import org.junit.runners.Parameterized.Parameter;
  6. import org.junit.runners.Parameterized.Parameters;
  7. @RunWith(Parameterized.class)
  8. public class JunitTestsWithFieldInjection {
  9. @Parameters(name = "Run #Square of : {0}^2={1}")
  10. public static Iterable<Object[]> data() {
  11. return Arrays.asList(new Object[][] { { 1, 1 }, { 2, 4 }, { 3, 9 }, { 4, 16 }, { 5, 25 } });
  12. }
  13. @Parameter(value = 0)
  14. public int input;
  15. @Parameter(value = 1)
  16. public int resultExpected;
  17. @Test
  18. public void testSquare()
  19. {
  20. Assert.assertEquals(resultExpected, MathUtils.square(input));
  21. }
  22. }

3.1 单个字段注入

如果仅要注入一个字段,则无需在@Parameter注解中放置value属性。 默认值始终为“value = 0”。

  1. import org.junit.Assert;
  2. import org.junit.Test;
  3. import org.junit.runner.RunWith;
  4. import org.junit.runners.Parameterized;
  5. import org.junit.runners.Parameterized.Parameter;
  6. import org.junit.runners.Parameterized.Parameters;
  7. @RunWith(Parameterized.class)
  8. public class JunitTestsWithParameters {
  9. @Parameters(name = "Argument number {0} is positive")
  10. public static Iterable<Object[]> data() {
  11. return Arrays.asList(new Object[][] { { 0 }, { 1 }, { 2 }, { 3 }, { 4 } });
  12. }
  13. @Parameter
  14. public int input;
  15. @Test
  16. public void testPositiveNumber()
  17. {
  18. Assert.assertEquals(true, input >= 0);
  19. }
  20. }

在本文中,我们学习了如何创建参数化测试,并使用不同的参数集运行测试的多次迭代。 它有助于测试带有参数的方法。

学习愉快!