如何确保在使用maven失败的jUnit测试后进行清理?

时间:2017-05-17 22:42:17

标签: maven unit-testing intellij-idea junit

编辑以在底部添加其他信息

我看过这里: What is a good way in maven/surefire to clean up after unit tests, whether they pass or not? 但它没有帮助我。我可能错误地配置了故障保护和/或万无一失。

我有依赖配置文件的jUnit测试,并且我已经在process-test-resources阶段添加了copy-rename-maven-plugin来为jUnit设置配置文件的测试副本在集成后测试阶段进行测试,复制重命名-maven-plugin和maven-clean-plugin,以恢复我完成时的所有方式。但是当jUnit测试失败时,后续阶段都没有运行,即使我也有故障安全插件。

故障安全文档中的jUnit示例都使用surefire,因此我对如何运行它们的安全性感到困惑 - 或者甚至是我需要做的事情。

我已将jUnit声明为依赖

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>${junit.version}</version>
    <scope>test</scope>
</dependency>

我在构建中获得了故障安全插件:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-failsafe-plugin</artifactId>
        <version>2.20</version>
        <!--
        <configuration>
            <skipITs>true</skipITs>
        </configuration>
        -->
        <executions>
            <execution>
                <id>run-nonautowired-tests</id>
                <phase>integration-test</phase>
                <goals>
                    <goal>integration-test</goal>
                    <goal>verify</goal>
                </goals>
                <configuration>
                    <includes>
                        <include>*Test.java</include>
                    </includes>
                </configuration>
            </execution>
        </executions>
    </plugin>

我没有遵循* IT命名约定的任何集成测试,只有遵循* Test命名约定的jUnit测试。我只是尝试使用故障安全来确保我的清理工作完成。我尝试过skipITs,我已经尝试过评论。我已尝试使用和不使用配置包括。

我目前已经将surefire插件注释掉了,但根据我看到的输出,它仍在运行测试并且无法构建:

    <!--
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.19.1</version>
        <configuration>
            <skipTests>true</skipTests>
        </configuration>
    </plugin>
    -->

我错过了什么/做错了什么?看起来我之前有这个工作,但它在一个使用Spring的不同项目中运行了两次jUnit测试,在那里我需要添加没有使用Spring的测试 - 但我不记得我做了什么不同。

我在IntelliJ 14.1.5中执行此操作,但是将Maven生命周期运行到验证阶段,而不是让IntelliJ直接运行jUnit测试。

此外,似乎它的surefire 2.12.4运行默认测试,我不知道它来自哪里。

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12.4:test (default-test) on project RefRange: There are test failures.

我应该在哪里查看2.12.4的来源?我怎样才能让它运行一次jUnit测试,并继续下一阶段,即使它们失败了?

感谢您的帮助,

Rebeccah

其他信息: 取消注释我的surefire插件后,添加

<skip>false</skip>
<skipTests>true</skipTests>

到其配置,将故障安全包括更改为

<includes>**/*Test.java</includes>,

并运行mvn verify -X,我在故意测试失败后立即得到以下内容:

[INFO] Results:
[INFO]
[ERROR] Failures:
[ERROR]   ref_range_by_RTest.testGetConfigManager:56
    Expected: is "C:\Program Files (x86)\Apache Software Foundation\Apache Tomcat 8.0.27/app_rsrcs/refrange/data"
         but: was "C:\Program Files (x86)\Apache Software Foundation\Apache Tomcat 8.0.27/app_rsrcs/refrange/data-not"
[INFO]
[ERROR] Tests run: 5, Failures: 1, Errors: 0, Skipped: 0
[INFO]
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is platform dependent! The file encoding for reports output files should be provided by the POM property ${project.reporting.   outputEncoding}.
[INFO]
[INFO] --- maven-failsafe-plugin:2.20:verify (run-nonautowired-tests) @ RefRange ---
[DEBUG] Configuring mojo org.apache.maven.plugins:maven-failsafe-plugin:2.20:verify from plugin realm ClassRealm[plugin>org.apache.maven.plugins:maven-failsafe-plugin:2.20, parent: sun.misc.Launcher$AppClassLoader@647e05]
[DEBUG] Configuring mojo 'org.apache.maven.plugins:maven-failsafe-plugin:2.20:verify' with basic configurator -->
[DEBUG]   (s) basedir = C:\Git\clincor-clindev\refrange
[DEBUG]   (s) reportsDirectory = C:\Git\clincor-clindev\refrange\target\failsafe-reports
[DEBUG]   (s) skip = false
[DEBUG]   (f) summaryFile = C:\Git\clincor-clindev\refrange\target\failsafe-reports\failsafe-summary.xml
[DEBUG]   (s) testClassesDirectory = C:\Git\clincor-clindev\refrange\target\test-classes
[DEBUG]   (s) testFailureIgnore = false
[DEBUG]   (f) session = org.apache.maven.execution.MavenSession@2eef62
[DEBUG] -- end configuration --
[DEBUG] Failsafe report directory: C:\Git\clincor-clindev\refrange\target\failsafe-reports
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 11.976 s
[INFO] Finished at: 2017-05-17T20:54:09-07:00
[INFO] Final Memory: 17M/247M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-failsafe-plugin:2.20:verify (run-nonautowired-tests) on project RefRange: There are test failures.
[ERROR]
[ERROR] Please refer to C:\Git\clincor-clindev\refrange\target\failsafe-reports for the individual test results.
[ERROR] Please refer to dump files (if any exist) [date]-jvmRun[N].dump, [date].dumpstream and [date]-jvmRun[N].dumpstream.
[ERROR] -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-failsafe-plugin:2.20:verify (run-nonautowired-tests) on project RefRange: There are test failures.

1 个答案:

答案 0 :(得分:0)

目前,failsafe:verify已绑定到integration-test阶段:

<executions>
    <execution>
    <id>run-nonautowired-tests</id>
    <phase>integration-test</phase>
    <goals>
        <goal>integration-test</goal>
        <goal>verify</goal>
    </goals>
    <configuration>
        <includes>
            <include>*Test.java</include>
        </includes>
    </configuration>
    </execution>
</executions>

failsafe会在post-integration-test清理之前导致失败。

尝试从<goal>verify</goal>阶段删除integration-test。如果需要,您可以为verify阶段创建单独的执行。

更新:考虑在插件本身声明configuration属性,而不是在执行中声明:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.20</version>
    <configuration>
        <includes>**/*Test.java</includes>
    </configuration>
    ...

这样你就可以省略为failafe插件明确声明的执行。