【问题标题】:Maven failsafe: followed guide, but does not run on "verify"Maven 故障安全:遵循指南,但不在“验证”上运行
【发布时间】:2014-02-03 23:10:49
【问题描述】:

我正在尝试让 Maven 故障安全插件运行我的集成测试,但即使我遵循了 https://maven.apache.org/surefire/maven-failsafe-plugin/usage.html 的指南,它也不起作用。

我的 maven 项目(MvnTest)如下所示(源文件如下):

pom.xml
-src
  -main
    -java
    -resources
 -test
  -java
    SomeIT.java
    SomeTest.java

mvn test 仅使用 sunfire 插件正确运行 SomeTest.java 中的测试。输出:

...
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ MvnTest ---
[INFO] Surefire report directory: ...
...
Running SomeTest
Tests run: 1, Failures: 1

但是,mvn verify 命令输出的结果与 mvn test 完全相同,这在两个方面是错误的:1)它调用 sunfire,2)它运行SomeTest.java 中的测试,但只能在 SomeIT.java 中运行 (https://maven.apache.org/surefire/maven-failsafe-plugin/examples/inclusion-exclusion.html)

我相信我已经按照指南正确使用故障保护,但显然我一定错过了一些东西。我做错了什么?

源文件

pom.xml:

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

    <groupId>MvnTest</groupId>
    <artifactId>MvnTest</artifactId>
    <version>1.0-SNAPSHOT</version>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.16</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
        </dependency>
    </dependencies>
</project>

SomeIT.java:

import org.junit.Test;
import static org.junit.Assert.assertTrue;

public class SomeIT {
    @Test
    public void test2() {
        assertTrue(4 == 3);
    }
}

SomeTest.java:

import org.junit.Test;
import static org.junit.Assert.assertTrue;

public class SomeTest {
    @Test
    public void test1() {
        assertTrue(2 == 3);
    }
}

【问题讨论】:

  • 正如您提到的Tests run: 1, Failures: 1,这意味着您通常应该在末尾看到BUILD FAILURE,其中包含有关单元测试失败的适当消息。您的单元测试将失败,并且基于生命周期 maven 表示,如果单元测试失败,继续进行集成测试是没有意义的。所以你必须先修复你的单元测试,这意味着mvn clean package 应该产生一个BUILD SUCCESS 消息。之后你可以尝试 doe mvn verify.
  • 谢谢,但这不起作用。 mvn clean package 产生BUILD FAILURE;似乎它也运行测试。但是,如果单元测试失败,我不明白您关于不运行集成测试的评论。 failsafe 的全部意义不是将集成测试与单元测试分开,以便您可以单独运行它们而不会相互影响(除了编译错误之外)?
  • mvn clean package 将运行单元测试但不会运行集成测试,如果您遵循了如下所示的命名约定。也许您可以显示完整的 pom 文件或将其放在 github 或类似的东西上以深入了解。如果你运行mvn verify,整个生命周期将贯穿其中是站compiletestpackageintegration-test等。
  • khmarbaise 是对的。这里发生的情况是,您没有达到使用 Failsafe 运行集成测试的程度——因为您的单元测试首先失败并因此停止构建。集成测试依赖于通过的单元测试,因为它们执行整个代码堆栈。如果堆栈的任何部分未能通过单元测试,那么在部分堆栈被破坏时尝试对整个堆栈进行集成测试是没有意义的。一旦单元测试通过,Failsafe 就会启动。
  • 我现在明白了 - 谢谢!但是,当单元测试失败时执行“集成测试”没有意义,这是不正确的。就我而言,我实际上没有“集成”测试,而是在 BDD 循环中使用黄瓜编写的“验收”测试 (AT)。在我的 BDD 中,循环是 (AT -> (UT -> pass)*),所以我写了一个 AT,然后我写了几个 UT 以使其通过。在这种情况下,无论 UT 的状态如何,能够在任何时候运行 AT 都很好。另外,当我只想查看 AT 的结果时,为什么还要在控制台中看到 UT 结果呢?

标签: java maven maven-failsafe-plugin


【解决方案1】:

在 pom.xml 中为 maven-failsafe-plugin 添加包含部分

看起来像:

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.16</version>
            <configuration>
                <includes>
                    <include>**/*IT.java</include>
                </includes>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>integration-test</goal>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

【讨论】:

    猜你喜欢
    • 2013-05-12
    • 1970-01-01
    • 1970-01-01
    • 2013-11-12
    • 1970-01-01
    • 1970-01-01
    • 2012-09-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多