JUnit @AfterClass运行时添加到一个糟糕的测试用例:( [英] JUnit @AfterClass run time is added to a poor testcase :(

查看:113
本文介绍了JUnit @AfterClass运行时添加到一个糟糕的测试用例:(的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于每个测试运行的运行时间,我面临轻微的JUnit不便。

I'm facing a slight JUnit inconvenience regarding the run time for each test to run.

我有一个 @AfterClass 注释和运行的最后一个测试将把它的运行时添加到它。

I have an @AfterClass annotation and the last test that runs will get it's runtime added to it's.

因此报告将错误地显示发生的不良测试的长时间运行时间最后运行。

So the report will incorrectly show a long run time for the poor test that happens to be run last.

有没有办法从上次测试中排除其运行时间?

Is there a way to exclude its run time from the last test?

推荐答案

这是Eclipse的一个问题,而不是junit。如果我们以此为例:

This is an issue with Eclipse, not junit. If we use this as an example:

public class TimingTest {
    @Test public void test1() {
        System.out.println("test1");
        sleep(1000);
    }

    @Test public void test2() {
        System.out.println("test2");
        sleep(2000);
    }

    @After public void after() {
        System.out.println("after");
        sleep(3000);
    }

    @AfterClass public static void afterClass() {
        System.out.println("afterClass");
        sleep(5000);
    }

    private static void sleep(int i) {
        try {
            Thread.sleep(i);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}

然后我们得到test1 = 4s,test2 = 10s(在Eclipse中)。但是,Eclipse中的junit runner没有使用junit提供的信息。 RunListener 界面定义了方法testStarted,testFinished,testRunStarted, testRunFinished。如果我们查看调用这些方法的时间,请使用:

then we get for test1=4s, test2=10s (in Eclipse). However, the junit runner in Eclipse isn't using the information that junit is giving it. The RunListener interface defines methods testStarted, testFinished, testRunStarted, testRunFinished. If we look at when these methods are called, using:

public class RunJunitTestRunListener {
    private static class MyListener extends RunListener {
        private long runStart = 0L;
        private long testStart = 0L;

        @Override
        public void testRunStarted(Description description) throws Exception {
            System.out.println("runStarted");
            runStart = System.currentTimeMillis();
            super.testRunStarted(description);
        }

        @Override
        public void testRunFinished(Result result) throws Exception {
            System.out.println("runFinished " + (System.currentTimeMillis() - runStart) + "ms");
            super.testRunFinished(result);
        }

        @Override
        public void testStarted(Description description) throws Exception {
            System.out.println("testStarted");
            testStart = System.currentTimeMillis();
            super.testStarted(description);
        }

        @Override
        public void testFinished(Description description) throws Exception {
            System.out.println("testFinished " + (System.currentTimeMillis() - testStart) + "ms");
            super.testFinished(description);
        }
    }

    public static void main(String[] args) {
        JUnitCore core= new JUnitCore();
        core.addListener(new MyListener());
        core.run(TimingTest.class);
    }
}

我们得到输出:

testFinished 4004ms
testFinished 5002ms
runFinished 14012ms

这是你所期望的。 Eclipse正在将@afterClass添加到时代。所以,如果你关心你的方法的时间,你需要

which is what you would expect. Eclipse is adding the @afterClass onto the times. So, if you care about the timing of your methods, you need to either


  1. 运行上面的代码,创建你自己的监听器。没有简单的方法将监听器附加到特定测试。

  2. 使用基准测试套件( JunitPerf 中的rel =nofollow> junit-benchmark li>
  3. @AfterClass不要做任何事情

  1. Run the above code, create your own listener. There is no easy way to attach a listener to a particular test.
  2. Use a benchmarking suite (junit-benchmark as DaveBall suggested or JunitPerf
  3. Don't do anything slow in @AfterClass

这篇关于JUnit @AfterClass运行时添加到一个糟糕的测试用例:(的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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