博客
关于我
JUnit5学习之六:参数化测试(Parameterized Tests)基础
阅读量:410 次
发布时间:2019-03-06

本文共 6276 字,大约阅读时间需要 20 分钟。

JUnit5学习系列:参数化测试(Parameterized Tests)基础知识

欢迎访问我的GitHub

关于《JUnit5学习》系列

《JUnit5学习》系列旨在通过实战提升SpringBoot环境下的单元测试技能,共计八篇文章,链接如下:

[系列文章链接待补充]

本篇概览

本文是《JUnit5学习》系列的第六篇,主要介绍参数化测试(Parameterized Tests)的基础知识,包括以下内容:

  • 极速体验
  • 版本依赖
  • ValueSource数据源
  • null、空字符串数据源
  • 枚举数据源
  • 方法数据源
  • Csv格式数据源
  • Csv文件数据源

源码下载

源码下载

项目主页:[链接待补充]

git仓库地址(HTTPS):[链接待补充]

git仓库地址(SSH):git@github.com:zq2599/blog_demos.git

极速体验

在父工程junitpractice中新建名为parameterized的子工程,其pom.xml内容如下:

4.0.0
com.bolingcavalry
junitpractice
1.0-SNAPSHOT
../pom.xml
com.bolingcavalry
parameterized
0.0.1-SNAPSHOT
parameterized
Demo project for parameterized experience in Spring Boot junit
1.8
org.junit
junit-bom
5.7.0
pom
import
org.springframework.boot
spring-boot-starter-web
org.projectlombok
lombok
org.springframework.boot
spring-boot-starter-test
test
org.junit.jupiter
junit-jupiter
org.junit.jupiter
junit-jupiter
test
org.springframework.boot
spring-boot-maven-plugin

新建测试类HelloTest.java,其路径为junitpractice\parameterized\src\test\java\com\bolingcavalry\parameterized\service\impl,内容如下:

package com.bolingcavalry.parameterized.service.impl;import lombok.extern.slf4j.Slf4j;import org.junit.jupiter.api.DisplayName;import org.junit.jupiter.api.MethodOrderer;import org.junit.jupiter.api.Order;import org.junit.jupiter.api.TestMethodOrder;import org.junit.jupiter.params.ParameterizedTest;import org.junit.jupiter.params.provider.ValueSource;import org.springframework.boot.test.context.SpringBootTest;import static org.junit.jupiter.api.Assertions.assertTrue;@SpringBootTest@Slf4j@TestMethodOrder(MethodOrderer.OrderAnnotation.class)public class HelloTest {    @Order(1)    @DisplayName("多个字符串型入参")    @ParameterizedTest    @ValueSource(strings = { "a", "b", "c" })    void stringsTest(String candidate) {        log.info("stringsTest [{}]", candidate);        assertTrue(null != candidate);    }}

版本要求

  • SpringBoot-2.3.4.RELEASE依赖的junit-jupiter-5.6.2版本中的ParameterizedTest功能尚未完全成熟。
  • 升级到junit-jupiter-5.7.0版本后,ParameterizedTest功能更加稳定。

在pom.xml中执行以下操作:

  • 在dependencyManagement节点添加junit-bom,指定版本号:
  • org.junit
    junit-bom
    5.7.0
    pom
    import
    1. 排除spring-boot-starter-test和junit-jupiter的间接依赖关系:
    2. org.springframework.boot
      spring-boot-starter-test
      test
      org.junit.jupiter
      junit-jupiter
      1. 添加junit-jupiter依赖:
      2. org.junit.jupiter
        junit-jupiter
        test

        ValueSource数据源

        ValueSource是最简单常用的数据源,支持以下类型的数组:

        • short
        • byte
        • int
        • long
        • float
        • double
        • char
        • boolean
        • java.lang.String
        • java.lang.Class

        例如:

        @Order(2)@DisplayName("多个int型入参")@ParameterizedTest@ValueSource(ints = { 1, 2, 3 })void intsTest(int candidate) {    log.info("ints [{}]", candidate);    assertTrue(candidate < 3);}

        执行结果如下:

        • 值为3时,assertTrue无法通过,测试方法会失败。

        null、空字符串数据源

        在用字符串作为入参时,通常要考虑入参为null的情况。可以使用以下方式:

        @ValueSource(strings = { null, "a", "b", "c" })

        或者使用@NullSource注解:

        @NullSource@ValueSource(strings = { "a", "b", "c" })

        执行结果如下:

        • null作为入参被执行一次。

        枚举数据源

        EnumSource可以让一个枚举类中的全部或者部分值作为测试方法的入参。创建枚举类Types.java:

        public enum Types {    SMALL,    BIG,    UNKNOWN}

        测试方法:

        @Order(6)@DisplayName("多个枚举型入参")@ParameterizedTest@EnumSourcevoid enumSourceTest(Types type) {    log.info("enumSourceTest [{}]", type);}

        执行结果如下:

        • 测试方法会依次执行SMALL、BIG、UNKNOWN三个枚举值。

        方法数据源

        @MethodSource可以指定一个方法名称,该方法返回的元素集合作为测试方法的入参。定义一个静态方法:

        static Stream
        stringProvider() { return Stream.of("apple1", "banana1");}

        测试方法:

        @Order(9)@DisplayName("静态方法返回集合,用此集合中每个元素作为入参")@ParameterizedTest@MethodSource("stringProvider")void methodSourceTest(String candidate) {    log.info("methodSourceTest [{}]", candidate);}

        或者在同一个类中不指定方法名,JUnit会自动匹配:

        static Stream
        methodSourceWithoutMethodNameTest() { return Stream.of("apple3", "banana3");}

        测试方法:

        @Order(11)@DisplayName("静态方法返回集合,不指定静态方法名,自动匹配")@ParameterizedTest@MethodSourcevoid methodSourceWithoutMethodNameTest(String candidate) {    log.info("methodSourceWithoutMethodNameTest [{}]", candidate);}

        Csv格式数据源

        @CsvSource解决了测试方法入参有多个字段的问题。例如:

        @Order(12)@DisplayName("CSV格式多条记录入参")@ParameterizedTest@CsvSource({    "apple1, 11",    "banana1, 12",    "'lemon1, lime1', 0x0A"})void csvSourceTest(String fruit, int rank) {    log.info("csvSourceTest, fruit [{}], rank [{}]", fruit, rank);}

        执行结果如下:

        • 测试方法会依次执行每条CSV记录的字段。

        Csv文件数据源

        @CsvFileSource用于指定CSV文件作为数据源。创建文件two-column.csv:

        Country, referenceSweden, 1Poland, 2"United States of America", 3

        测试方法:

        @Order(14)@DisplayName("CSV文件多条记录入参")@ParameterizedTest@CsvFileSource(files = "src/test/resources/two-column.csv", numLinesToSkip = 1)void csvFileTest(String country, int reference) {    log.info("csvFileTest, country [{}], reference [{}]", country, reference);}

        预期《进阶》篇

        至此,我们对JUnit5的参数化测试(Parameterized Tests)有了基本的了解。下一篇将深入探讨更高级的功能。

    转载地址:http://khtkz.baihongyu.com/

    你可能感兴趣的文章
    Objective-C实现IIR数字滤波器(附完整源码)
    查看>>
    Objective-C实现insertion sort插入排序算法(附完整源码)
    查看>>
    Objective-C实现integer partition整数分区算法(附完整源码)
    查看>>
    Objective-C实现integerPartition整数划分算法(附完整源码)
    查看>>
    Objective-C实现interpolation search插值搜索算法(附完整源码)
    查看>>
    Objective-C实现Interpolation search插值查找算法(附完整源码)
    查看>>
    Objective-C实现intersection交集算法(附完整源码)
    查看>>
    Objective-C实现intro sort内省排序算法(附完整源码)
    查看>>
    Objective-C实现inversions倒置算法(附完整源码)
    查看>>
    Objective-C实现isalpha函数功能(附完整源码)
    查看>>
    Objective-C实现islower函数功能(附完整源码)
    查看>>
    Objective-C实现isPowerOfTwo算法(附完整源码)
    查看>>
    Objective-C实现isupper函数功能(附完整源码)
    查看>>
    Objective-C实现ItemCF算法(附完整源码)
    查看>>
    Objective-C实现ItemCF算法(附完整源码)
    查看>>
    Objective-C实现iterating through submasks遍历子掩码算法(附完整源码)
    查看>>
    Objective-C实现iterative merge sort迭代归并排序算法(附完整源码)
    查看>>
    Objective-C实现jaccard similarity相似度无平方因子数算法(附完整源码)
    查看>>
    Objective-C实现Julia集算法(附完整源码)
    查看>>
    Objective-C实现k nearest neighbours k最近邻分类算法(附完整源码)
    查看>>