简单的方法一遍又一遍地运行相同的junit测试? [英] Easy way of running the same junit test over and over?

查看:121
本文介绍了简单的方法一遍又一遍地运行相同的junit测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

像标题所示,我正在寻找一些简单的方式来使用Eclipse自动运行几次JUnit 4.x测试。

Like the title says, I'm looking for some simple way to run JUnit 4.x tests several times in a row automatically using Eclipse.

连续运行相同的测试10次并报告结果。

An example would be running the same test 10 times in a row and reporting back the result.

我们已经有一个复杂的方式来做,但我正在寻找一种简单的方法这样做,以便我可以确定我一直在努力修复的片断测试保持固定。

We already have a complex way of doing this but I'm looking for a simple way of doing it so that I can be sorta sure that the flaky test I've been trying to fix stays fixed.

一个理想的解决方案是Eclipse插件/设置/功能我不知道。

An ideal solution would be an Eclipse plugin/setting/feature that I am unaware of.

推荐答案

这样做最简单(因为最少的新代码需要)的方式是运行测试作为参数化测试(用 @RunWith(Parameterized.class)注释,并添加一个方法来提供10个空参数)。这样框架将运行测试10次。

The easiest (as in least amount of new code required) way to do this is to run the test as a parametrized test (annotate with an @RunWith(Parameterized.class) and add a method to provide 10 empty parameters). That way the framework will run the test 10 times.

此测试需要是类中唯一的测试,或者更好地将所有测试方法应该需要运行

This test would need to be the only test in the class, or better put all test methods should need to be run 10 times in the class.

这是一个例子:

@RunWith(Parameterized.class)
public class RunTenTimes {

    @Parameterized.Parameters
    public static List<Object[]> data() {
        return Arrays.asList(new Object[10][0]);
    }

    public RunTenTimes() {
    }

    @Test
    public void runsTenTimes() {
        System.out.println("run");
    }
}

使用上述方法,甚至可以做到使用一个无参数的构造函数,但我不知道框架作者是否打算这样做,否则将在将来发生。

With the above, it is possible to even do it with a parameter-less constructor, but I'm not sure if the framework authors intended that, or if that will break in the future.

如果你正在实现自己的跑步者,那么你可以让跑步者跑10次。如果您使用第三方运行程序,则使用4.7,您可以使用新的 @Rule 注释并实现 MethodRule 接口,以便它在语句中执行,并在for循环中执行10次。目前这种方法的缺点是 @Before @After 仅运行一次。这可能会在下一个版本的JUnit中发生变化( @Before 将在 @Rule 之后运行),但不管您将在对象的相同实例上执行(对于参数化运行程序不正确)。这假设您正在运行类的任何运行者正确识别 @Rule 注释。只有在委托给JUnit赛跑者的情况下才是这种情况。

If you are implementing your own runner, then you could have the runner run the test 10 times. If you are using a third party runner, then with 4.7, you can use the new @Rule annotation and implement the MethodRule interface so that it takes the statement and executes it 10 times in a for loop. The current disadvantage of this approach is that @Before and @After get run only once. This will likely change in the next version of JUnit (the @Before will run after the @Rule), but regardless you will be acting on the same instance of the object (something that isn't true of the Parameterized runner). This assumes that whatever runner you are running the class with correctly recognizes the @Rule annotations. That is only the case if it is delegating to the JUnit runners.

如果您使用无法识别 @Rule的自定义赛跑者注释,那么你真的坚持不得不写自己的跑步者,适当地委托给那个跑步者,并运行10次。

If you are running with a custom runner that does not recognize the @Rule annotation, then you are really stuck with having to write your own runner that delegates appropriately to that Runner and runs it 10 times.

请注意还有其他方法可以解决这个问题(如理论赛跑者),但都需要一个赛跑者。不幸的是,JUnit目前不支持跑步者层。这是连锁其他跑步者的跑步者。

Note that there are other ways to potentially solve this (such as the Theories runner) but they all require a runner. Unfortunately JUnit does not currently support layers of runners. That is a runner that chains other runners.

这篇关于简单的方法一遍又一遍地运行相同的junit测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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