使用 Maven/JUnit/Spring 在第一次失败时停止测试

发布于 2024-12-10 11:19:40 字数 562 浏览 0 评论 0原文

我希望 Maven 在遇到第一个错误时停止尝试运行我的 JUnit Spring 测试。这可能吗?

我的测试类如下所示,我将它们作为标准 Maven 目标运行。

@ContextConfiguration(locations = {"classpath:/spring-config/store-persistence.xml","classpath:/spring-config/store-security.xml","classpath:/spring-config/store-service.xml", "classpath:/spring-config/store-servlet.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
@Transactional
public class SkuLicenceServiceIntegrationTest
{
...

如果 Spring 配置中存在错误,则每个测试都会尝试重新启动 Spring 上下文,这每次需要 20 秒。这意味着我们很长时间都没有发现任何测试失败,因为它会在得出构建失败的结论之前尝试运行全部测试!

I'd like Maven to stop trying to run my JUnit Spring tests when it encounters the first error. Is this possible?

My test classes look like the following, and I run them just as a standard Maven target.

@ContextConfiguration(locations = {"classpath:/spring-config/store-persistence.xml","classpath:/spring-config/store-security.xml","classpath:/spring-config/store-service.xml", "classpath:/spring-config/store-servlet.xml" })
@RunWith(SpringJUnit4ClassRunner.class)
@Transactional
public class SkuLicenceServiceIntegrationTest
{
...

If there's an error in the Spring config, then each test will try to restart the Spring context, which takes 20 seconds a go. This means we don't find out for ages that any tests have failed, as it'll try to run the whole lot before concluding that the build was a failure!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

走走停停 2024-12-17 11:19:40

十年后回答,因为我需要完全相同的东西。

可以使用 skipAfterFailureCount 参数将故障安全插件配置为“失败后跳过测试”。

官方文档:

https://maven.apache .org/surefire/maven-failsafe-plugin/examples/skip-after-failure.html

Answering ten years later, since I needed exactly the same.

The failsafe plugin can be configure to "skip tests after failure", using the skipAfterFailureCount parameter.

Official documentation:

https://maven.apache.org/surefire/maven-failsafe-plugin/examples/skip-after-failure.html

苏大泽ㄣ 2024-12-17 11:19:40

这更像是一条评论,而不是一个答案,但也许您仍然会发现它有用。

我建议将集成测试分成一个单独的阶段,并使用 Failsafe 而不是 Surefire 来运行它们。通过这种方式,您可以决定是只需要运行快速单元测试,还是需要运行长时间运行的集成测试的完整测试集:

    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.6</version>
            <executions>
                <execution>
                    <id>integration-test</id>
                    <goals>
                        <goal>integration-test</goal>
                    </goals>
                </execution>
                <!-- Uncomment/comment this in order to fail the build if any integration test fail -->
                <execution>
                    <id>verify</id>
                    <goals><goal>verify</goal></goals>  
                </execution>
            </executions>
        </plugin>
    </plugins>

解决您的问题的方法可能是将一个测试挑选出来单独执行并首先运行它;这样执行就会失败,并且后续的surefire/failsafe执行将不会启动。请参阅如何配置插件来执行此操作

This is more a remark, than an answer, but still, maybe you'll find it useful.

I'd recommend separating your integration tests into a separate phase, and running them with Failsafe, rather than Surefire. This way you can decide whether you need to run only fast unit tests, or the complete set with long-running integration tests:

    <plugins>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.6</version>
            <executions>
                <execution>
                    <id>integration-test</id>
                    <goals>
                        <goal>integration-test</goal>
                    </goals>
                </execution>
                <!-- Uncomment/comment this in order to fail the build if any integration test fail -->
                <execution>
                    <id>verify</id>
                    <goals><goal>verify</goal></goals>  
                </execution>
            </executions>
        </plugin>
    </plugins>

A workaround for your problem might be singling out a test into a separate execution and run it first; this way the execution would fail and subsequent surefire/failsafe executions will not be launched. See how to configure the plugin to do it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文