27

我的问题与这个问题非常相似:maven-failsafe-plugin Failures and BUILD SUCCESS?

如果测试失败,我设法设置故障安全插件以失败。

但是如果测试进入错误状态,故障安全插件仍然不会破坏构建。

.................
-------------------------------------------------------
   T E S T S
-------------------------------------------------------
Running xxxxx.IntegrationTierFunctionalTestCase
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.054 sec <<< FAILURE!

Results :

Tests in error:
  testException(xxxxx.IntegrationTierFunctionalTestCas

Tests run: 1, Failures: 0, Errors: 1, Skipped: 0

[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is
[INFO] [failsafe:verify {execution: functional-test-1024}]
[INFO] Failsafe report directory: C:\projects\oec-integration-server\trunk\oec-integrati
[WARNING] File encoding has not been set, using platform encoding Cp1252, i.e. build is
[INFO] [failsafe:integration-test {execution: functional-test-24}]
[INFO] Failsafe report directory: C:\projects\oec-integration-server\trunk\oec-integrati
.............
[INFO] ------------------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESSFUL
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 58 seconds
[INFO] Finished at: Tue May 28 17:48:13 BST 2013
[INFO] Final Memory: 114M/781M
[INFO] ------------------------------------------------------------------------

为简单起见,IntegrationTierFunctionalTestCase 仅包含此代码

import org.junit.Test;
import static org.junit.Assert.fail;
public class IntegrationTierFunctionalTestCase 
{

    @Test
    public void testException(){
        //fail();
        throw new RuntimeException("super error");
    }
}

如果我取消注释 fail() 整个构建正确失败,构建失败。但如果我只是抛出一个异常,它会如上所示失败。

我们的插件配置看起来像这样

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-failsafe-plugin</artifactId>
    <version>2.7</version>
    <configuration>
        <redirectTestOutputToFile>true</redirectTestOutputToFile>
        <systemPropertyVariables>
            <oec.env>TEST</oec.env>
            <mule.test.timeoutSecs>2400</mule.test.timeoutSecs>
        </systemPropertyVariables>
        <additionalClasspathElements>
            <additionalClasspathElement>${basedir}/src/main/resources/config</additionalClasspathElement>
            </additionalClasspathElement>
        </additionalClasspathElements>
    </configuration>
    <executions>
        <execution>
            <id>functional-test-1024</id>
            <phase>test</phase>
            <goals>
                <goal>integration-test</goal>
                <goal>verify</goal>
            </goals>
            <configuration>
                <includes>
                    <include>**/IntegrationTierFunctionalTestCase.java</include>
                </includes>
                <forkMode>once</forkMode>
                <argLine>-XX:MaxPermSize=256M -Xmx1024M</argLine>
            </configuration>
        </execution>
    </executions>
</plugin>

我错过了什么?不,我不想将它包装在 try-catch 块中并手动失败测试。

4

4 回答 4

40

您需要有两个执行块,因为 maven-failsafe-plugin 的验证目标旨在检查集成测试的结果。

  <executions>
    <execution>
        <id>functional-test-1024</id>
        <phase>test</phase>
        <goals>
            <goal>integration-test</goal>
        </goals>
        <configuration>
            <includes>
                <include>**/IntegrationTierFunctionalTestCase.java</include>
            </includes>
            <forkMode>once</forkMode>
            <argLine>-XX:MaxPermSize=256M -Xmx1024M</argLine>
        </configuration>
    </execution>
    <execution>
        <id>verify</id>
        <phase>verify</phase>
        <goals>
            <goal>verify</goal>
        </goals>
    </execution>
  </executions>

此外,您应该将 maven-failsafe-plugin 的版本更新为 2.14.1 而不是 2.7。 更新:同时更新到2.17

于 2013-05-28T17:36:10.507 回答
17

如果您正在运行这样的集成测试:

mvn test-compile failsafe:integration-test

那么你应该知道,根据故障安全的 Maven 文档

Failsafe Plugin 在集成测试阶段不会使构建失败,从而使集成测试后阶段能够执行。

我能够像这样使构建失败:

mvn test-compile failsafe:integration-test failsafe:verify

这是我的故障安全配置供参考:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.19</version>
            <configuration>
                <includes>
                    <include>**/*IT.java</include>
                </includes>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>integration-test</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
于 2015-10-22T19:48:41.470 回答
1

请确认您的任何 maven pom.xml 文件中的 maven 属性“maven.test.failure.ignore”未设置为“true”,因为它可能是在测试失败后停止构建的唯一原因。

于 2013-05-29T08:38:11.950 回答
0

发生这种情况还有一个可能的额外原因。Failsafe verify 正在寻找测试类,并期望它们位于 ${project.build.directory}/test-classes - 如果您有不同的设置,它只会打印“没有要运行的测试”。并在构建成功时结束,无论集成测试阶段的结果是什么。

您必须在插件配置中设置testClassesDirectory :

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.18.1</version>

            <configuration>
                <systemPropertyVariables>
                    <environmentType>${environmentType}</environmentType>
                </systemPropertyVariables>
                <summaryFile>${project.build.directory}/failsafe-reports/failsafe-summary.xml</summaryFile>
                <testClassesDirectory>${project.build.directory}/classes</testClassesDirectory>
                <suiteXmlFiles>
                    <suiteXmlFile>${suiteXml}</suiteXmlFile>
                </suiteXmlFiles>
            </configuration>
于 2015-06-09T13:56:27.537 回答