使用 Maven/JUnit/Spring 在第一次失败时停止测试
我希望 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
十年后回答,因为我需要完全相同的东西。
可以使用
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
这更像是一条评论,而不是一个答案,但也许您仍然会发现它有用。
我建议将集成测试分成一个单独的阶段,并使用 Failsafe 而不是 Surefire 来运行它们。通过这种方式,您可以决定是只需要运行快速单元测试,还是需要运行长时间运行的集成测试的完整测试集:
解决您的问题的方法可能是将一个测试挑选出来单独执行并首先运行它;这样执行就会失败,并且后续的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:
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.