记录JUnit测试运行所需的时间 [英] Record time it takes JUnit tests to run

查看:169
本文介绍了记录JUnit测试运行所需的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想记录我的JUnit测试以编程方式运行需要多长时间。我在各种测试类中都有大量的测试,我想知道每个测试方法运行多长时间。

I would like to record how long it takes my JUnit test to run programmatically. I have a large number of tests in various test classes, and I would like to find out how long each individual test method takes to run.

我可以改变继承结构或者注释方法不同,但我想避免在测试方法本身以及用于设置测试业务逻辑的before / after方法中添加代码。

I can change the inheritance structure or annotate methods differently, but I would like to avoid having to add code within the test method itself and also within the before/after methods which are used to setup test business logic.

推荐答案

您可以使用JUnit StopWatch规则并覆盖JUnit API文档中提供的方法,并通过包含一行代码将时间打印到每个测试的控制台或日志文件中在每个单独的测试用例类中。

You could use the JUnit StopWatch rule and override its methods as provided in the JUnit API documentation and have the time printed to console or log file for each test just by including one line of code in each of your individual test case class.


  1. 创建Customer StopWatch类(提供样本)

import java.util.concurrent.TimeUnit;
import org.junit.AssumptionViolatedException;
import org.junit.rules.Stopwatch;
import org.junit.runner.Description;

public class MyJUnitStopWatch extends Stopwatch{

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

 @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);
   }    
}


  • 创建一个ParentTestClass该行和每个测试类继承父测试类:

    public class ParentTestCase {
    
    
    @Rule
    public MyJUnitStopWatch stopwatch = new MyJUnitStopWatch();
    
    }
    


  • 子类继承父级。 Child类或方法之前或之后没有其他更改。

    Child classes inherits parent. No other change in Child classes or before or after methods.

    public class TestUniqueCharacterString extends ParentTestCase {    
    
        private String uniqueChars = null;
    
        @Before
        public void before(){
            uniqueChars = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnop";
        }
    
        @Test
        public void testMyUniqueCharacterMethod(){
    
    
            UniqueCharacteString.doesStringHaveUniqueCharacters(uniqueChars);
        }
    

    Or


    1. 在每个测试类中加入此行

    @Rule
    public MyJUnitStopWatch stopwatch = new MyJUnitStopWatch();
    

    样本测试类:

    import org.junit.After;
    import org.junit.Before;
    import org.junit.Rule;
    import org.junit.Test;
    
    
    
    public class TestUniqueCharacterString {    
    
    @Rule
    public MyJUnitStopWatch stopwatch = new MyJUnitStopWatch();
    
    private String uniqueChars = null;
    
    @Before
    public void before(){
        uniqueChars = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnop";
    }
    
    @Test
    public void testMyUniqueCharacterMethod(){
    
    
        UniqueCharacteString.doesStringHaveUniqueCharacters(uniqueChars);
    }
    
    @Test
    public void testGoodIsUniqueMethod(){
            UniqueCharacteString.isUniqueCharacs(uniqueChars);
    }
    
    @Test
    public void testGoodIsUniqueMethodWithoutArray(){
        UniqueCharacteString.isUniqueCharsWithoutArray(uniqueChars);
    }
    
    @After
    public void after(){
        uniqueChars = "";
    }    
     }
    


    JUnit API参考:

    http://junit.org/apidocs/org/junit/rules/Stopwatch.html

    示例输出

    Test testMyUniqueCharacterMethod succeeded, spent 3250 microseconds
    Test testMyUniqueCharacterMethod finished, spent 3250 microseconds
    Test testGoodIsUniqueMethod succeeded, spent 70 microseconds
    Test testGoodIsUniqueMethod finished, spent 70 microseconds
    Test testGoodIsUniqueMethodWithoutArray succeeded, spent 54 microseconds
    Test testGoodIsUniqueMethodWithoutArray finished, spent 54 microseconds
    

    它还会显示失败和跳过测试用例的时间。

    It will also show time for failed and skipped test cases.

    这篇关于记录JUnit测试运行所需的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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