java - 如何使用故障安全插件单独启动集成测试?

标签 java maven maven-surefire-plugin

我无法运行集成测试,只能运行单元测试。

这是我的 Maven 配置(请参阅下面的代码)。它使用两个插件。其中之一是 maven-failsafe-plugin,第二个是 maven-surefire-plugin

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <skipTests>false</skipTests>
    <skipITs>${skipTests}</skipITs>
    <skipUTs>${skipTests}</skipUTs>
</properties>
<dependencies>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.8.7</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.16</version>
            <configuration>
                <skipTests>${skipUTs}</skipTests>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.16</version>
            <configuration>
                <skipTests>${skipTests}</skipTests>
                <skipITs>${skipITs}</skipITs>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

我尝试使用此命令运行单元测试

mvn clean test

还有单独启动集成测试的命令

mvn clean failsafe:integration-test verify

最后一个命令的调用结果是

[INFO] --- maven-failsafe-plugin:2.16:integration-test (default-cli) @ integration-test-demo ---
[INFO] No tests to run.

最佳答案

我使用这些配置文件:

属性

<properties>
        <!-- Only unit tests are run by default. -->
        <skip.integration.tests>true</skip.integration.tests>
        <skip.unit.tests>false</skip.unit.tests>
    </properties>

个人资料

<profiles>
        <profile>
            <id>all-tests</id>
            <properties>
                <build.profile.id>all-tests</build.profile.id>
                <!-- All tests are run. -->
                <skip.integration.tests>false</skip.integration.tests>
                <skip.unit.tests>false</skip.unit.tests>
            </properties>
        </profile>
        <profile>
            <id>dev</id>
        </profile>
        <profile>
            <id>integration-tests</id>
            <properties>
                <!-- Used to locate the profile specific configuration file. -->
                <build.profile.id>integration-test</build.profile.id>
                <!-- Only integration tests are run. -->
                <skip.integration.tests>false</skip.integration.tests>
                <skip.unit.tests>true</skip.unit.tests>
            </properties>
        </profile>
    </profiles>

maven-surefire-插件

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.15</version>
            <configuration>
                <!-- Skips unit tests if the value of skip.unit.tests property is true -->
                <skipTests>${skip.unit.tests}</skipTests>
                <!-- Excludes integration tests when unit tests are run. -->
                <excludes>
                    <exclude>**/*IntegrationTest.java</exclude>
                </excludes>
            </configuration>
        </plugin>

maven-failsafe-插件

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.15</version>
            <executions>
                <!-- Ensures that both integration-test and verify goals of the Failsafe Maven plugin are executed. -->
                <execution>
                    <id>integration-tests</id>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                    <configuration>
                        <!-- Skips integration tests if the value of skip.integration.tests property is true -->
                        <skipTests>${skip.integration.tests}</skipTests>
                        <includes>
                            <include>**/*IntegrationTest.java</include>
                        </includes>
                    </configuration>
                </execution>
            </executions>
        </plugin>

集成测试以 ...IntegrationTest.java 结束,我运行所需的配置文件(所有测试、集成测试)。默认情况下运行单元测试。

我很确定我是从某个地方复制的,但现在我不记得链接了。抱歉。

关于java - 如何使用故障安全插件单独启动集成测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27239283/

相关文章:

java - Grails 找不到 customCacheKeyGenerator

java - 如何在运行 Maven 测试时优化 CPU 利用率

java - Maven Java GAE编译错误

maven Surefire 测试插件运行测试,即使它们被排除 :

java - 无法使用 Maven 运行 JUnit5 测试

maven - 如何运行两个具有不同配置的 maven surefire 插件?

Java - 使用客户端证书身份验证时取消 SSL 流

java - asmack - 无法读取 VCard

maven - 使用(maven)存储库和 Maven/Gradle 管理多个项目的最佳实践?

java - 为什么我的数据结构很慢(自定义数据结构)