Maven - 将集成测试与单元测试分开

2022-09-04 21:44:01

是否可以将集成测试与同一模块中的单元测试隔离开来?

我创建了简单的pom:

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <artifactId>prj</artifactId>
    <packaging>war</packaging>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>**/integration/**/*.java</exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <id>integration</id>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <configuration>
                            <includes>
                                <include>**/integration/**/*.java</include>
                            </includes>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

</project>

然而,使用mvn -Pintegration test,它不会调用任何东西。如果我注释掉主构建中的排除部分 - 那么它开始执行测试,但也没有配置文件。


答案 1

而不是:

<exclude>*/integration/**/*.java</exclude>

尝试:

 <include>*/unit/**.java</include>

然后在集成配置文件中做

<includes>
   <exclude>**/unit/**/*.java</exclude>
   <include>**/integration/**/*.java</include>
</includes>

您可能必须尝试完全正确地获取包含/排除,但这是一般的想法。


答案 2

推荐