maven - 当单元测试耗时太长时失败 [英] maven - fail build when unit test takes too long

查看:241
本文介绍了maven - 当单元测试耗时太长时失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目中有很多单元测试,用JUnit和TestNG编写。构建过程基于带有surefire插件的maven。

I have in my project a lot of unit tests, written in JUnit and TestNG. The building process is based on maven with surefire plugin.

当至少一个单元测试花费太多秒时,maven是否有任何方法/插件可以使构建失败?我知道有一些插件可以在TeamCity,Jenkins中构建失败,但这太过分了。

Is there any way/plugin for maven to fail the build when at least one unit test takes too many seconds? I know that there are some plugins to fail build in TeamCity, Jenkins but this is "too far".

在我的项目中我想只有快速测试才能拥有单元测试过程有效。我可以改进我的旧测试,但我需要保护未来的承诺

In my project I want to have only fast tests to have unit testing process effective. I can improve my old tests but I need to protect for the future commitments

推荐答案

如果你的所有测试扩展了一些基类,我认为你可以坚持 @Rule ,这将适用于子类中的所有 @Test

If all your tests extend some base class, I think you can stick @Rule in there which will apply to all the @Test in the child class.

例如

import org.junit.rules.Timeout;
public abstract class BaseTestConfiguration {
    @Rule public Timeout testTimeout = new Timeout(60000); // 60k ms = 1 minute
}

public class ThingTests extends BaseTestConfiguration {
    @Test
    public void testThis() {
        /*will fail if not done in 1 minute*/
    }
    @Test
    public void testThat() {
        /*will fail if not done in 1 minute*/
    }
}

我们发现这是最简单的,而不是搞乱AspectJ或秘密配置指令。开发人员更有可能找出Java部分而不是所有秘密XML。
您可以在基类中放置 @Before @After 和任意数量的其他junit指令。

We found this to be easiest rather than messing with AspectJ or secret configuration directives. Devs are more likely to figure out the Java parts than all the secret XML this and that. You can put @Before @After and any number of other junit directives in the base class too.

这篇关于maven - 当单元测试耗时太长时失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆