从Junit5开始,对参数化测试支持进行了大幅度的改进和提升。下面我们就一起来详细看看Junit5参数化测试的方法。
部署和依赖
和Junit4相比,Junit5框架更多在向测试平台演进。其核心组成也从以前的一个Junit的jar包更换成由多个模块组成。本文所需要依赖模块如下:
- junit-jupiter-engine: Junit的核心测试引擎
- junit-jupiter-params: 编写参数化测试所需要的依赖包
- junit-platform-launcher: 从IDE(InteliJ/Eclipses)等运行时所需要的启动器
另外,为了从Maven命令行工具中运行Juint,还需要junit-platform-surefire-provider包的依赖。
maven的pom.xml文件中添加如下来进行安装
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.2.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.platform</groupId>
<artifactId>junit-platform-launcher</artifactId>
<version>1.2.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22</version>
</plugin>
</plugins>
</build>
Junit5 参数源详解
value source
value source是最简单的参数源,通过注解可以直接指定携带的运行参数。
- String values: @ValueSource(strings = {“foo”, “bar”, “baz”})
- Double values: @ValueSource(doubles = {1.5D, 2.2D, 3.0D})
- Long values: @ValueSource(longs = {2L, 4L, 8L})
- Integer values: @ValueSource(ints = {2, 4, 8})
示例代码如下:
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
public class ValueSourcesExampleTest {
@ParameterizedTest
@ValueSource(ints = {2, 4, 8})
void testNumberShouldBeEven(int num) {
assertEquals(0, num % 2);
}
@ParameterizedTest
@ValueSource(strings = {"Radar", "Rotor", "Tenet", "Madam", "Racecar"})
void testStringShouldBePalindrome(String word) {
assertEquals(isPalindrome(word), true);
}
@ParameterizedTest
@ValueSource(doubles = {2.D, 4.D, 8.D})
void testDoubleNumberBeEven(double num) {
assertEquals(0, num % 2);
}
boolean isPalindrome(String word) {
return word.toLowerCase().equals(new StringBuffer(word.toLowerCase()).reverse().toString());
}
}
输出
[INFO] -------------------------------------------------------
[INFO] T E S T S
[INFO] -------------------------------------------------------
[INFO] Running qiucao.learning.ParaTest
[INFO] Tests run: 11, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.155 s - in qiucao.learning.ParaTest
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 11, Failures: 0, Errors: 0, Skipped: 0
Enum Source
枚举参数源,允许我们通过将参数值由给定Enum枚举类型传入。并可以通过制定约束条件或正则匹配来筛选传入参数
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.EnumSet;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import org.junit.jupiter.params.provider.EnumSource.Mode;
public class EnumSourcesExampleTest {
@ParameterizedTest(name = "[{index}] TimeUnit: {arguments}")
@EnumSource(TimeUnit.class)
void testTimeUnitMinimumNanos(TimeUnit unit) {
assertTrue(unit.toMillis(2000000L) > 1);
}
@ParameterizedTest
@EnumSource(value = TimeUnit.class, names = {"SECONDS", "MINUTES"})
void testTimeUnitJustSecondsAndMinutes(TimeUnit unit) {
assertTrue(EnumSet.of(TimeUnit.SECONDS, TimeUnit.MINUTES).contains(unit));
assertFalse(EnumSet
.of(TimeUnit.DAYS, TimeUnit.HOURS, TimeUnit.MILLISECONDS, TimeUnit.NANOSECONDS,
TimeUnit.MICROSECONDS).contains(unit));
}
@ParameterizedTest
@EnumSource(value = TimeUnit.class, mode = Mode.EXCLUDE, names = {"SECONDS", "MINUTES"})
void testTimeUnitExcludingSecondsAndMinutes(TimeUnit unit) {
assertFalse(EnumSet.of(TimeUnit.SECONDS, TimeUnit.MINUTES).contains(unit));
assertTrue(EnumSet
.of(TimeUnit.DAYS, TimeUnit.HOURS, TimeUnit.MILLISECONDS, TimeUnit.NANOSECONDS,
TimeUnit.MICROSECONDS).contains(unit));
}
@ParameterizedTest
@EnumSource(value = TimeUnit.class, mode = Mode