【问题标题】:How to ensure cleanup after failed jUnit tests using maven?如何确保在使用 maven 的 jUnit 测试失败后进行清理?
【发布时间】:2017-05-17 22:42:17
【问题描述】:

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

我看过这里: 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 true,我试过把它注释掉。我试过有无配置包括。

我目前已将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 测试。

另外,它似乎是运行默认测试的万无一失的 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 测试,即使它们失败了也可以继续下一个阶段?

感谢您的帮助,

丽贝卡


附加信息: 取消注释我的万能插件后,添加

<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.

【问题讨论】:

  • Surefire插件是default lifecycle的一部分,2.12.4来自default-bindings.xml
  • 看看这个答案是否对你有帮助:stackoverflow.com/questions/6612344/…
  • 谢谢,我缩小了一些范围。如果我在我的 pom.xml 中取消注释 surefire 插件,我会在我的 pom.xml 中获得版本。如果我在 surefire 中告诉跳过测试,则构建不会失败,并且我可以看到来自故障安全插件的一些消息(以前,我根本没有从故障安全中得到任何信息)。我收到两个故障安全目标(集成测试和验证)的“测试被跳过”消息。
  • 现在,如果我还在surefire中添加 falsetrue 并将故障安全包含更改为 **/*Test.java ,我得到了运行测试的故障保护——但现在我得到“org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.apache.maven.plugins:maven-failsafe-plugin:2.20:verify (run-nonautowired-tests) 项目 RefRange:有测试失败。”并且集成后测试阶段没有运行。
  • 我已经把它带到了运行测试的故障安全位置,并且不确定,这不再是问题了。现在的问题是,当故障安全测试失败时,后集成测试阶段根本没有运行。我认为集成测试后阶段应该在验证阶段之前。

标签: maven unit-testing intellij-idea junit


【解决方案1】:

目前,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 清理之前导致失败。

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

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

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

这样您可能可以省略故障安全插件的显式声明执行。

【讨论】:

  • 就是这样,谢谢。我知道它必须是简单而基本的东西,但它被所有其他配置问题所掩盖。不需要单独执行(默认情况下它绑定到验证阶段,但我明确地将它绑定到后集成测试阶段)。
  • @Rebeccah 我已经更新了我的答案以涵盖可能的配置问题
  • 经过昨天的讨论,我尝试在验证阶段添加回验证目标的执行。现在我执行了集成测试后阶段和验证阶段的构建失败,这是应该的。
猜你喜欢
  • 2019-05-02
  • 2014-05-28
  • 2010-11-11
  • 1970-01-01
  • 2019-07-19
  • 2020-01-07
  • 1970-01-01
  • 1970-01-01
  • 2019-07-06
相关资源
最近更新 更多