Maven命令分开执行单元测试和集成测试

导读:本篇文章讲解 Maven命令分开执行单元测试和集成测试,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

前提

项目中一般会包含两种测试:单元测试 + 集成测试。为了方便管理,我个人偏向于将这两种测试放在不同的文件夹下。

项目结构

在这里插入图片描述

pom.xml

添加源文件

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>3.0.0</version>
    <executions>
        <execution>
            <id>add-integration-test-sources</id>
            <phase>generate-test-sources</phase>
            <goals>
                <goal>add-test-source</goal>
            </goals>
            <configuration>
                <sources>
                    <source>src/int/java</source>
                </sources>
            </configuration>
        </execution>

        <execution>
            <id>add-integration-test-resources</id>
            <phase>generate-test-resources</phase>
            <goals>
                <goal>add-test-resource</goal>
            </goals>
            <configuration>
                <resources>
                    <resource>
                        <directory>src/int/resources</directory>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>

设置参数

<profiles>
    <!-- 定义不同情景的变量,用于命令行控制执行不同的测试 -->
    <!-- mvn clean install -P skipTests:跳过所有的测试-->
    <profile>
        <id>skipTests</id>
        <properties>
            <skipIntTest>true</skipIntTest>
            <skipUnitTest>true</skipUnitTest>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <!-- mvn clean install -P unit:只执行单元测试 -->
    <profile>
        <id>unit</id>
        <properties>
            <skipIntTest>true</skipIntTest>
            <skipUnitTest>false</skipUnitTest>
        </properties>
    </profile>
    <!-- mvn clean install -P int:只执行集成测试 -->
    <profile>
        <id>int</id>
        <properties>
            <skipIntTest>false</skipIntTest>
            <skipUnitTest>true</skipUnitTest>
        </properties>
    </profile>
    <!-- mvn clean install -P tests:执行所有的测试(单元测试 + 集成测试) -->
    <profile>
        <id>tests</id>
        <properties>
            <skipIntTest>false</skipIntTest>
            <skipUnitTest>false</skipUnitTest>
        </properties>
    </profile>
</profiles>

单元测试 & 集成测试的区分

.Test.java结尾的文件为单元测试,以.IntTest.java结尾的文件为集成测试

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <configuration>
        <skipTests>${skipUnitTest}</skipTests>
        <includes>
            <include>**/*Test.java</include>
        </includes>
        <excludes>
            <exclude>**/*IntTest.java</exclude>
        </excludes>
    </configuration>
</plugin>

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <configuration>
        <skipITs>${skipIntTest}</skipITs>
        <includes>
            <include>**/*IntTest.java</include>
        </includes>
    </configuration>
</plugin>

运行结果展示

不执行任何测试

mvn clean install -P skipTests

结果

在这里插入图片描述

只执行单元测试

mvn clean install -P unit

结果

在这里插入图片描述

只执行集成测试

mvn clean install -P int

结果

在这里插入图片描述

全都执行

mvn clean install -P tests

结果

在这里插入图片描述

项目代码

Maven For Test

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/77891.html

(1)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!