我需要记录每个测试用例的执行时间。我不想使用自定义实现。我想知道junit5 Btw中是否有对此可用的注释,我知道Stopwatch
或Junit4 @Timeout
。
浏览了junit5文档后,我发现了这个示例TimeExtension
此摘录来自他们的文档:
@ExtendWith(TimingExtension.class)
class TimingExtensionTests {
@Test
void sleep20ms() throws Exception {
Thread.sleep(20);
}
@Test
void sleep50ms() throws Exception {
Thread.sleep(50);
}
}
我写了一个BenchmarkExtension
,您可以将其与BenchmarkExtension
一起使用。您可以按如下方式使用它:
@Benchmark
应用于该类时,您将获得一条消息,其中包含该类中所有测试方法的总运行时间。当应用于单个测试方法时,您将仅收到该测试的消息。
我希望它最终会进入@Benchmark
。
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)。