Junit4运行测试类固定次数并显示结果(eclipse) [英] Junit4 run a test class a fixed number of times and display results (eclipse)

查看:187
本文介绍了Junit4运行测试类固定次数并显示结果(eclipse)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想能够运行一个测试类指定的次数。该类看起来像:

  @RunWith(Parameterized.class)
public class TestSmithWaterman {

private static String [] args;
私人静态SmithWaterman sw;
private Double [] [] h;
private String seq1aligned;

@Parameters
public static Collection< Object []> config(){
// h和seq1对齐值
}

public TestSmithWaterman(Double [] [] h,String seq1aligned){
this.h = h ;
this.seq1aligned = seq1aligned;
}

@BeforeClass
public static void init(){
//运行史密斯·沃特曼一次为所有
}

@Test
@Repeat(value = 20)// does nothing
// see http://codehowtos.blogspot.gr/2011/04/run-junit-test-repeatedly.html
public void testCalculateMatrices(){
assertEquals(h,sw.getH());
}

@Test
public void testAlignSeq1(){
assertEquals(seq1aligned,sw.getSeq1Aligned());
}

// etc
}

上述任何测试可能会失败(并发错误 - 编辑:故障提供有用的调试信息),所以我想要能够多次运行该类,最好将结果分组。尝试重复注释 - 但这是测试具体(并没有真正使它的工作 - 见上文),并努力与 RepeatedTest.class ,这似乎不能转移到Junit 4 - 在SO上找到的最接近的是这个 - 但显然是Junit3。在Junit4中,我的套件看起来像:

  @RunWith(Suite.class)
@SuiteClasses({TestSmithWaterman.class} )
public class AllTests {}

,我看到没有办法多次运行。
使用空选项进行参数化不是真的选择 - 因为我需要我的参数



所以我被卡住了一次又一次地在日食中击中Control + F11



帮助



编辑(2017.01.25):有人继续提出并将其标示为我明确表示不接受的接受答案的问题的重复这里

解决方案

根据@MatthewFarwell在评论中的建议,我实现了一个测试规则根据他的回答

  public static class Retry implements TestRule {

private final int retryCount;

public Retry(int retryCount){
this.retryCount = retryCount;
}

@Override
public Statement apply(final Statement base,
final描述){
return new Statement(){

@Override
@SuppressWarnings(synthetic-access)
public void evaluate()throws Throwable {
Throwable chuckThrowable = null;
int failuresCount = 0; (int i = 0; i< retryCount; i ++)
{
try {
base.evaluate();
} catch(Throwable t){
gotThrowable = t;
System.err.println(description.getDisplayName()
+:run+(i + 1)+failed:);
t.printStackTrace();
++ failuresCount;
}
}
if(snapsThrowable == null)return;
抛出新的AssertionError(description.getDisplayName()
+:failures+ failuresCount +出
+ retryCount +尝试,看最后一个可抛出的原因。 ;
}
};
}
}

作为我测试类中的嵌套类 - 并添加

  @Rule 
public Retry retry = new Retry(69);

在我的测试方法之前,在同一个类。



这确实是诀窍 - 它重复测试69次 - 在一些例外的情况下,一个新的AssertionError,包含一些统计信息的单个消息加上原始Throwable作为原因,被抛出。因此,统计信息也将在Eclipse的jUnit视图中显示。


I want to be able to run a Test class a specified number of times. The class looks like :

@RunWith(Parameterized.class)
public class TestSmithWaterman {

    private static String[] args;
    private static SmithWaterman sw;
    private Double[][] h;
    private String seq1aligned;

    @Parameters
    public static Collection<Object[]> configs() {
        // h and seq1aligned values 
    }

    public TestSmithWaterman(Double[][] h, String seq1aligned) {
        this.h = h;
        this.seq1aligned = seq1aligned;
    }

    @BeforeClass
    public static void init() {
        // run smith waterman once and for all
    }

    @Test
    @Repeat(value = 20) // does nothing
    // see http://codehowtos.blogspot.gr/2011/04/run-junit-test-repeatedly.html
    public void testCalculateMatrices() {
        assertEquals(h, sw.getH());
    }

    @Test
    public void testAlignSeq1() {
        assertEquals(seq1aligned, sw.getSeq1Aligned());
    }

    // etc
}

Any of the tests above may fail (concurrency bugs - EDIT : the failures provide useful debug info) so I want to be able to run the class multiple times and preferably have the results grouped somehow. Tried the Repeat annotation - but this is test specific (and did not really make it work - see above) and struggled with the RepeatedTest.class, which cannot seem to transfer to Junit 4 - the closest I found on SO is this - but apparently it is Junit3. In Junit4 my suite looks like :

@RunWith(Suite.class)
@SuiteClasses({ TestSmithWaterman.class })
public class AllTests {}

and I see no way to run this multiple times. Parametrized with empty options is not an option really - as I need my params anyway

So I am stuck hitting Control + F11 in eclipse again and again

Help

EDIT (2017.01.25): someone went ahead and flagged this as duplicate of the question whose accepted answer I explicitly say does not apply here

解决方案

As suggested by @MatthewFarwell in the comments I implemented a test rule as per his answer

public static class Retry implements TestRule {

    private final int retryCount;

    public Retry(int retryCount) {
        this.retryCount = retryCount;
    }

    @Override
    public Statement apply(final Statement base,
            final Description description) {
        return new Statement() {

            @Override
            @SuppressWarnings("synthetic-access")
            public void evaluate() throws Throwable {
                Throwable caughtThrowable = null;
                int failuresCount = 0;
                for (int i = 0; i < retryCount; i++) {
                    try {
                        base.evaluate();
                    } catch (Throwable t) {
                        caughtThrowable = t;
                        System.err.println(description.getDisplayName()
                            + ": run " + (i + 1) + " failed:");
                        t.printStackTrace();
                        ++failuresCount;
                    }
                }
                if (caughtThrowable == null) return;
                throw new AssertionError(description.getDisplayName()
                        + ": failures " + failuresCount + " out of "
                        + retryCount + " tries. See last throwable as the cause.", caughtThrowable);
            }
        };
    }
}

as a nested class in my test class - and added

@Rule
public Retry retry = new Retry(69);

before my test methods in the same class.

This indeed does the trick - it does repeat the test 69 times - in the case of some exception a new AssertionError, with an individual message containing some statistics plus the original Throwable as a cause, gets thrown. So the statistics will be also visible in the jUnit view of Eclipse.

这篇关于Junit4运行测试类固定次数并显示结果(eclipse)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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