laitimes

Software Testing Learning Notes丨JUnit5 Dynamic Test Creation

author:Tested 666
This article is transferred from the tester community, the original link: jck28-lucio-junit5 dynamic test creation - study notes - tester community

conception

  • Dynamic testing is a new programming model introduced in JUnit5.
  • This new test is called dynamic testing, which is generated at runtime by the factory method that @TestFactory annotations.

What is dynamic testing

  • Dynamic Testing: DynamicTest dynamically generates test cases when it is running.
    • The method declared by the @TestFactory annotation.
  • Static Tests: Test cases that @Test annotations, as the case is fully specified at compile time

Dynamic testing is different from static testing

  • In contrast to @Test methods, @TestFactory methods are not test cases per se, but rather factories of test cases.
    • So the conclusion: dynamic testing is a product of the factory.
  • DynamicTest is quite different from the standard @Test use case
    • The execution method is different, and the dynamic test "DynamicTest" does not support lifecycle callbacks.

Composition of dynamic testing

  1. There must be a @TestFactory annotation on the method.
  2. 由显示名称和Executable组成。
  3. 方法返回值类型。 「流、集合、迭代器」 Stream Collection Iterable Iterator DynamicNode > 如果不返回以上类型会发生什么?

Composition of dynamic testing

  • 返回其它类型报错:JUnitException

create

  • Static test cases
  • Dynamic test cases

pom file

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>           

Static test case creation

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);
}           

Changed static testing to dynamic testing

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))
    );
}           

conclusion

  • Dynamic tests are tests that are generated at runtime using @TestFactory annotations using the factory method.
  • The method marked as @TestFactory is not a test case, but a factory for the test case
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);
    }           

Free video tutorial sharing for software testing and development - Official Account - Tester Community

Software Testing Learning Notes丨JUnit5 Dynamic Test Creation

Read on