从JUnit测试时间中排除@Before方法的持续时间 [英] Exclude @Before method duration from JUnit test time

查看:122
本文介绍了从JUnit测试时间中排除@Before方法的持续时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JUnit似乎包括报告我的测试时间时执行 @BeforeClass @Before所花费的时间.

JUnit looks like it's including the time taken to perform @BeforeClass @Before when it reports my test timing.

我正在使用JUnit 4.12

I'm using JUnit 4.12

在这种情况下,它针对测试显示23.863秒,总计25.649秒. 当我手动检测所有方法时,这是故障:

In this case it shows 23.863 seconds against the test and 25.649 total. When I manually instrument all the methods, this is the breakdown:

10:11:20.074 [INFO] [mainId] beforeClass completed in 12516ms
10:11:40.083 [INFO] [mainId] beforeTest completed in 20009ms
10:11:44.171 [INFO] [mainId] start_test completed in 4076ms 

因此,如果我添加beforeTest和start_test计时,则接近UI中报告的时间.

So if I add beforeTest and start_test timing I get close to the time reported in the UI.

  • 是否可以告诉JUnit仅显示start_test时间?

现在,我只是像这样的帖子中手动输出时间: https://stackoverflow.com/a/180191/1165140

Right now I just manually output the timing like in this post: https://stackoverflow.com/a/180191/1165140

更新2017年7月27日 实际上,问题出在之前的测试方法(@Before),而不是我之前认为的@BeforeClass-日志记录中的错误.我仍然认为是否可以包含此时间是可以配置的.我正在使用@beforetest在每次测试之前设置数据.

UPDATE 2017-07-27 Actually problem is with before test methods (@Before) not @BeforeClass as I previously thought - error in my logging. Still in my view it should be configurable whether this time is included. I'm using @beforetest to setup data before each test.

我已经确认,膨胀时间是在UI中输出的,也是从命令行输出的maven surefire XML.

I've confirmed that the inflated time is outputted in the UI as well as the maven surefire XML output from the command line.

推荐答案

public static class StopwatchTest {
     private static final Logger logger = Logger.getLogger("");

     private static void logInfo(Description description, String status, long nanos) {
         String testName = description.getMethodName();
         logger.info(String.format("Test %s %s, spent %d microseconds",
                                   testName, status, TimeUnit.NANOSECONDS.toMicros(nanos)));
     }

     @Rule
     public Stopwatch stopwatch = new Stopwatch() {
         @Override
         protected void succeeded(long nanos, Description description) {
             logInfo(description, "succeeded", nanos);
         }

         @Override
         protected void failed(long nanos, Throwable e, Description description) {
             logInfo(description, "failed", nanos);
         }

         @Override
         protected void skipped(long nanos, AssumptionViolatedException e, Description description) {
             logInfo(description, "skipped", nanos);
         }

         @Override
         protected void finished(long nanos, Description description) {
             logInfo(description, "finished", nanos);
         }
     };

     @Test
     public void succeeds() {
     }

     @Test
     public void fails() {
         fail();
     }

     @Test
     public void skips() {
         assumeTrue(false);
     }
 }

来自秒表

这篇关于从JUnit测试时间中排除@Before方法的持续时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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