记录JUnit测试运行所需的时间

one*_*elf 9 java junit load-testing

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

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

小智 10

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

  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);
       }    
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 使用该行创建ParentTestClass,并且每个测试类都继承父测试类:

    public class ParentTestCase {
    
    
    @Rule
    public MyJUnitStopWatch stopwatch = new MyJUnitStopWatch();
    
    }
    
    Run Code Online (Sandbox Code Playgroud)

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

public class TestUniqueCharacterString extends ParentTestCase {    

    private String uniqueChars = null;

    @Before
    public void before(){
        uniqueChars = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnop";
    }

    @Test
    public void testMyUniqueCharacterMethod(){


        UniqueCharacteString.doesStringHaveUniqueCharacters(uniqueChars);
    }
Run Code Online (Sandbox Code Playgroud)

要么

  1. 在每个Test类中包含此行

    @Rule
    public MyJUnitStopWatch stopwatch = new MyJUnitStopWatch();
    
    Run Code Online (Sandbox Code Playgroud)

    样品测试类:

    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 = "";
    }    
     }
    
    Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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


Hee*_*jin 7

尝试使用@Before和@After.在测试之前或之后运行@Before或@After注释的方法.

    @Before
    public void start() {
        start = System.currentTimeMillis();
    }

    @After
    public void end() {
        System.out.println(System.currentTimeMillis() - start);
    }
Run Code Online (Sandbox Code Playgroud)


cjs*_*hno 0

您可以创建一个JUnit 规则来记录调用之前/之后的时间。此规则可以用作实例和/或类规则,以便为您提供每个单独的测试方法以及每个测试类的时间。