🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
# JUnit 参数化测试示例 > 原文: [https://howtodoinjava.com/junit/how-to-write-parameterized-testcases-with-junit-4/](https://howtodoinjava.com/junit/how-to-write-parameterized-testcases-with-junit-4/) 在本 [JUnit 教程](https://howtodoinjava.com/junit-5-tutorial/)中,学习创建和执行 junit 参数化测试。 参数化测试是正常测试,它使用不同的测试参数反复执行。 它可以帮助开发人员在使用不同输入类型执行相同测试以测试函数健壮性,以及可能的函数边界上面节省时间。 ## 1\. JUnit Maven 依赖项 下面是 **maven 依赖项**,我们应该在测试示例代码之前添加到您的 maven 项目中。 ```java <!-- Junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit-dep</artifactId> <version>4.11</version> <scope>test</scope> </dependency> ``` ## 2\. 具有构造器参数的 JUnit 参数化测试 参数化测试使用`@RunWith`注解以及`@Parameters`注解来馈送输入。 #### 2.1 要测试的类 下面是测试类,我们将为其编写测试用例。 ```java package corejava.test.junit; public final class MathUtils { //Return square of a function public static int square(final int number) { return number * number; } } ``` #### 2.2 参数化测试 让我们为上述数学工具类编写参数化测试。 ```java package corejava.test.junit; import java.util.Arrays; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; @RunWith(Parameterized.class) public class JunitTestsWithParameters { // @Parameters annotation marks this method as parameters provider @Parameters(name = "Run #Square of : {0}^2={1}") public static Iterable<Object []> data() { return Arrays.asList(new Object[][] { { 1, 1 }, { 2, 4 }, { 3, 19 }, { 4, 16 }, { 5, 25 } }); } // Our two parameters private final int input; private final int resultExpected; // Constructor is initialized with one set of parameters every time public JunitTestsWithParameters(final int input, final int result) { this.input = input; this.resultExpected = result; } @Test public void testUserMapping() { // You can use here assert also Assert.assertEquals(resultExpected, MathUtils.square(input)); } } ``` 请注意: 1. 我们必须按照给定的方式声明参数。 2. 参数传递给类的构造器以设置变量,因此可以在测试用例中使用。 3. 参数类的返回类型为“`List[]`”,要使用的数据类型已限于字符串或原始值 现在检查程序输出。 ![JUnit4 test execution](https://img.kancloud.cn/e8/01/e801b2a32126534223c6a2bdfbd4983e_1042x178.png "JUnit4 test execution") 测试执行结果 ## 3\. 带有字段注入的 JUnit 参数化测试 为了传递参数进行测试,我们可以通过字段注入传递参数,而不是通过构造器传递参数。 在这种方法中,我们声明确切的字段数作为输入参数。 每个字段一个参数。 让我们通过场注入重新测试我们的`MathUtils`类。 请注意,我们如何用`@Parameter`注解的字段替换构造器。 ```java import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameter; import org.junit.runners.Parameterized.Parameters; @RunWith(Parameterized.class) public class JunitTestsWithFieldInjection { @Parameters(name = "Run #Square of : {0}^2={1}") public static Iterable<Object[]> data() { return Arrays.asList(new Object[][] { { 1, 1 }, { 2, 4 }, { 3, 9 }, { 4, 16 }, { 5, 25 } }); } @Parameter(value = 0) public int input; @Parameter(value = 1) public int resultExpected; @Test public void testSquare() { Assert.assertEquals(resultExpected, MathUtils.square(input)); } } ``` #### 3.1 单个字段注入 如果仅要注入一个字段,则无需在`@Parameter`注解中放置`value`属性。 默认值始终为“`value = 0`”。 ```java import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameter; import org.junit.runners.Parameterized.Parameters; @RunWith(Parameterized.class) public class JunitTestsWithParameters { @Parameters(name = "Argument number {0} is positive") public static Iterable<Object[]> data() { return Arrays.asList(new Object[][] { { 0 }, { 1 }, { 2 }, { 3 }, { 4 } }); } @Parameter public int input; @Test public void testPositiveNumber() { Assert.assertEquals(true, input >= 0); } } ``` 在本文中,我们学习了如何创建参数化测试,并使用不同的参数集运行测试的多次迭代。 它有助于测试带有参数的方法。 学习愉快!