簡體   English   中英

多模塊Maven項目中共享集成測試

[英]Sharing integration test in multi module Maven project

考慮一個 Maven 項目,它具有三個模塊CommonServiceAServiceB ,其中兩個服務都依賴於Common並產生部署為單獨微服務的戰爭。 對於我想運行的每個服務進行集成測試,檢查服務是否在/health路徑上公開了一個健康檢查端點。

@Test
open fun testHealthCheck() {
    // implicitly asserts that response status is 200
    perform("/health", method = RequestMethod.GET)
}

我能想出的唯一解決方案是將此測試復制到每個服務的測試包中。 但是,這不是很干燥。 我希望在一個地方(最好在Common模塊中)定義此集成測試,但要在每個服務的集成測試階段運行。

我怎樣才能做到這一點?

有幾種方法可以實現這一目標。 這兩者各有利弊。

解壓共享測試

Maven 可以將多個工件附加到每個模塊(例如源或測試等)。 第一步是附加共享模塊的測試類。

<build>
    <plugins>
        <!-- ... -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.2.0</version>
            <executions>
                <execution>
                    <goals>
                        <goal>test-jar</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
    <!-- ... -->
</build>

在此之后,它很容易在其他項目中作為依賴項使用:

<dependencies>
    <dependency>
        <groupId>io.github.zforgo.stackoverflow</groupId>
        <artifactId>shared-tests</artifactId>
        <version>0.1.0-SNAPSHOT</version>
        <type>test-jar</type>
        <scope>test</scope>
    </dependency>
</dependencies>

在依賴項目或模塊共享測試應該解包。

<build>
    <plugins>
        <!-- ... -->
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-dependency-plugin</artifactId>
            <version>3.1.2</version>
            <executions>
                <execution>
                    <id>share-tests</id>
                    <phase>generate-test-sources</phase>
                    <goals>
                        <goal>unpack</goal>
                    </goals>
                    <configuration>
                        <artifactItems>
                            <artifactItem>
                                <groupId>io.github.zforgo.stackoverflow</groupId>
                                <artifactId>shared-tests</artifactId>
                                <version>0.1.0-SNAPSHOT</version>
                                <classifier>tests</classifier>
                                <outputDirectory>${project.build.testOutputDirectory}</outputDirectory>
                            </artifactItem>
                        </artifactItems>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
    <!-- ... -->
</build>

注意ArtifactItem忽略type屬性,因此應該使用classifier屬性而不是type maven-dependency-plugin問題跟蹤系統存在問題。 MDEP-732

在單元或集成測試階段,解壓的類將運行。

[INFO] ---------------< io.github.zforgo.stackoverflow:project >---------------
[INFO] Building project 0.1.0-SNAPSHOT                                    [3/3]
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:3.1.2:unpack (share-tests) @ project ---
[INFO] Configured Artifact: io.github.zforgo.stackoverflow:shared-tests:tests:0.2.0-SNAPSHOT:jar
[INFO] Unpacking /work/source/stackoverflow/junit-test-sharing/multimodule-test-sharing/shared-tests/target/shared-tests-0.1.0-SNAPSHOT-tests.jar to /work/source/stackoverflow/junit-test-sharing/multimodule-test-sharing/project/target/test-classes with includes "" and excludes ""
[INFO] 
[INFO] --- maven-surefire-plugin:3.0.0-M5:test (default-test) @ project ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running io.github.zforgo.stackoverflow.shared.SharedUnitTest
>>>> THIS IS A SHARED UNIT TEST <<<<
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.044 s - in io.github.zforgo.stackoverflow.shared.SharedUnitTest
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
[INFO] --- maven-failsafe-plugin:3.0.0-M5:integration-test (default) @ project ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[INFO] Running io.github.zforgo.stackoverflow.shared.SharedIntegrationTestIT
>>>> THIS IS A SHARED INTEGRATION TEST <<<<
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.005 s - in io.github.zforgo.stackoverflow.shared.SharedIntegrationTestIT
優點 缺點
單元測試和集成測試都在一個步驟中共享。 需要解決MDEP-732的原因
共享測試將被打包到依賴工件中。 防止更改。 Class 重復使依賴的測試罐不必要地變大。
解包期間支持包含和排除。

使用故障安全和萬無一失的插件的<dependenciesToScan>屬性(推薦)

首先,我要感謝@khmarbaise 的建議。

幸運的是maven-surefire-pluginmaven-failsafe-plugin都有<dependenciesToScan>配置屬性。 將掃描所有列出的依賴項以查找要包含和運行的測試類。 注意列出的工件必須是依賴項目或模塊中的<dependency>元素。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>io.github.zforgo.stackoverflow</groupId>
        <artifactId>shared-tests-aggregator</artifactId>
        <version>0.2.0-SNAPSHOT</version>
    </parent>

    <artifactId>project</artifactId>

    <dependencies>
        <dependency>
            <groupId>${project.groupId}</groupId>
            <artifactId>shared-tests</artifactId>
            <version>${project.version}</version>
            <type>test-jar</type>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.0.0-M5</version>
                <configuration>
                    <dependenciesToScan>
                        <dependency>${project.groupId}:shared-tests:test-jar</dependency>
                    </dependenciesToScan>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>3.0.0-M5</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                        <configuration>
                            <dependenciesToScan>
                                <dependency>${project.groupId}:shared-tests:test-jar</dependency>
                            </dependenciesToScan>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

注意:如果依賴項目中缺少相同的 package, maven-failsafe-plugin:integration-test目標將向控制台報告錯誤錯誤消息。

[INFO] 
[INFO] --- maven-failsafe-plugin:3.0.0-M5:integration-test (default) @ project ---
[INFO] 
[INFO] -------------------------------------------------------
[INFO]  T E S T S
[INFO] -------------------------------------------------------
[ERROR] WARNING: package io.github.zforgo.stackoverflow.shared not in project
[INFO] Running io.github.zforgo.stackoverflow.shared.SharedIntegrationTestIT
>>>> THIS IS A SHARED INTEGRATION TEST <<<<
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.034 s - in io.github.zforgo.stackoverflow.shared.SharedIntegrationTestIT
[INFO] 
[INFO] Results:
[INFO] 
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO] 
優點 缺點
易於分離的單元和集成測試共享 共享單元測試和集成測試時必須復制配置
共享測試不會被復制,沒有 class 重復 日志中有虛假的 [ERROR] 消息,但構建成功。
負責的插件將處理依賴的測試類 可能的配置不時更改,版本到版本。 始終閱讀文檔

使用build-helper-maven-plugin

這個 MojoHaus 插件允許在一個模塊或項目中使用多個源或測試源目錄。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>io.github.zforgo.stackoverflow</groupId>
        <artifactId>shared-tests</artifactId>
        <version>0.1.0-SHAPSHOT</version>
    </parent>
    <artifactId>project</artifactId>

    <dependencies>
        <dependency>
            <groupId>io.github.zforgo.stackoverflow</groupId>
            <artifactId>shared-test</artifactId>
            <version>0.1.0-SHAPSHOT</version>
            <type>test-jar</type>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>build-helper-maven-plugin</artifactId>
                <version>3.2.0</version>
                <executions>
                    <execution>
                        <phase>generate-test-sources</phase>
                        <goals>
                            <goal>add-test-source</goal>
                        </goals>
                        <configuration>
                            <sources>
                                <source>${project.parent.basedir}/shared-test/src/test/java/com/example/demo/shared</source>
                            </sources>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>
優點 缺點
僅適用於本地文件夾。 不支持外部工件。
文件夾是硬編碼的。 維護問題。

簡而言之,在項目和模塊之間共享測試有更多的可能性,但輪到你選擇最好的了。

專門為集成測試創建一個新的 Maven 模塊怎么樣? 然后,此模塊可以同時依賴ServiceAServiceB 然后,您的測試可以作為這個新模塊的集成測試階段的一部分運行。

您可能希望使用 Maven 故障安全插件進行集成測試。 這允許集成測試從一個單獨的目標運行。 這樣做的原因是針對諸如 Jenkins 之類的持續集成工具,您希望在其中運行單元測試,而不是集成測試,因為例如,它們依賴於運行的外部 web 服務。 如果該服務未運行,您的 CI 構建將失敗。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM