天天看點

軟體測試學習筆記丨JUnit5動态測試建立

作者:測試人666
本文轉自測試人社群,原文連結:jck28-lucio-junit5動态測試建立 - 學習筆記 - 測試人社群

概念

  • 動态測試是 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);
    }           

軟體測試開發免費視訊教程分享 - 公衆号 - 測試人社群

軟體測試學習筆記丨JUnit5動态測試建立

繼續閱讀