用于记录Junit5中每个测试用例的执行时间的注释

问题描述 投票:3回答:3

我需要记录每个测试用例的执行时间。我不想使用自定义实现。我想知道junit5 Btw中是否有对此可用的注释,我知道Stopwatch或Junit4 @Timeout

time execution junit5
3个回答
4
投票

浏览了junit5文档后,我发现了这个示例TimeExtension

此摘录来自他们的文档:

@ExtendWith(TimingExtension.class)
class TimingExtensionTests {

  @Test
  void sleep20ms() throws Exception {
     Thread.sleep(20);
  }

  @Test
  void sleep50ms() throws Exception {
    Thread.sleep(50);
  }

}

编辑:Source code for TimingExtension


2
投票

我写了一个BenchmarkExtension,您可以将其与BenchmarkExtension一起使用。您可以按如下方式使用它:

@Benchmark

应用于该类时,您将获得一条消息,其中包含该类中所有测试方法的总运行时间。当应用于单个测试方法时,您将仅收到该测试的消息。

我希望它最终会进入@Benchmark


0
投票

Junit5也具有@Benchmark class BenchmarkTest { @Test void notBenchmarked() { assertTrue(true); } @Test @Benchmark void benchmarked() throws InterruptedException { Thread.sleep(100); assertTrue(true); } } 批注。

还有JUnit Pioneer@Timout(请参阅assertTimeout)。

最后,JUnit 4秒表功能在JUnit 5中不可用。如前所述,使用assertTimeoutPreemptively。但是,此扩展名是发行版的[[not(还?)的一部分(请参阅documentation)。

© www.soinside.com 2019 - 2024. All rights reserved.