本文转自测试人社区,原文链接:https://ceshiren.com/t/topic/28217

概念

  • 动态测试是 JUnit5 中引入的一种新的编程模型。
  • 这种新的测试就是动态测试,它是由@TestFactory注解的工厂方法在运行时生成的。

什么是动态测试

  • 动态测试:就是DynamicTest在运行的时候动态的生成测试用例。
  • @TestFactory注解声明的方法。
  • 静态测试:@Test注解的测试用例,因为该用例在编译时已经完全指定好的

动态测试与静态测试区别

  • @Test方法相比,@TestFactory方法本身不是测试用例,而是测试用例的工厂
  • 所以得到结论:动态测试是工厂的产物。
  • 动态测试DynamicTest与标准 @Test 用例完全不同
  • 执行方式不同,动态测试「DynamicTest」不支持生命周期回调。

动态测试的构成

  1. 方法上必须有 @TestFactory 注解。
  2. 由显示名称和Executable组成。
  3. 方法返回值类型。「流、集合、迭代器」 Stream Collection Iterable Iterator DynamicNode > 如果不返回以上类型会发生什么?

动态测试的构成

  • 返回其它类型报错:JUnitException

创建

  • 静态测试用例
  • 动态测试用例

pom文件

ld.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>11</java.version> <maven.compiler.target>11</maven.compiler.target> <maven.compiler.source>11</maven.compiler.source> <junit.jupiter.version>5.8.2</junit.jupiter.version>
<maven.compiler.version>3.8.1</maven.compiler.version>
    <maven.surefire.version>3.0.0-M5</maven.surefire.version>
    <!-- plugins -->
    <maven-surefire-plugin.version>3.0.0-M5</maven-surefire-plugin.version>
    <poi.version>5.2.2</poi.version>
</properties>

<!--    物料清单 (BOM)-->
<dependencyManagement>
    <dependencies>
        <!--当使用 Gradle 或 Maven 引用多个 JUnit 工件时,此物料清单 POM 可用于简化依赖项管理。不再需要在添加依赖时设置版本-->
        <dependency>
            <groupId>org.junit</groupId>
            <artifactId>junit-bom</artifactId>
            <version>${junit.jupiter.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>

    </dependencies>
</dependencyManagement>



<dependencies>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter</artifactId>
        <!--            对应添加的依赖的作用范围-->
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-suite</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.junit.vintage</groupId>
        <artifactId>junit-vintage-engine</artifactId>
        <version>${junit.jupiter.version}</version>
    </dependency>

    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-all</artifactId>
        <version>1.3</version>
        <scope>test</scope>
    </dependency>
</dependencies>


<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>${maven-surefire-plugin.version}</version>

            <dependencies>
                <dependency>
                    <groupId>org.junit.jupiter</groupId>
                    <artifactId>junit-jupiter-engine</artifactId>
                    <version>${junit.jupiter.version}</version>
                </dependency>
                <dependency>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                    <version>${junit.jupiter.version}</version>
                </dependency>
            </dependencies>
        </plugin>
    </plugins>
</build>

静态测试用例创建

java public class DynamicBaseTest {
// Static test 1
@Test
void test_Add() {
    assertEquals(5, 3+2);
}

// Static test 2
@Test
void test_Devide() {
    assertEquals(5,25/5);
}

改编静态测试为动态测试

java public class DynamicBaseTest {
 // This method produces Dynamic test cases @TestFactory Collection dynamicTestsFromCollection() {
    return Arrays.asList(
            dynamicTest("1stDynamic", () -> assertEquals(5, 3+2)),
            dynamicTest("2ndDynamic", () -> assertEquals(5, 25/5))
    );
}

结论

  • 动态测试是在运行时通过工厂方法使用@TestFactory 注解生成的测试。
  • 标记为@TestFactory 的方法不是测试用例,而是测试用例的工厂
package com.junit5.createdynamictest;

import org.junit.jupiter.api.*;

import java.util.Arrays;
import java.util.Collection;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class DynamicBaseTest {
    // Static test 1
    @Test
    @DisplayName("加法静态测试用例")
    void test_Add() {
        assertEquals(5, 3+2);
    }

    // Static test 2
    @Test
    @DisplayName("除法静态测试用例")
    void test_Devide() {
        assertEquals(5,25/5);
    }