gpt4 book ai didi

java - 如何检查单元测试(全类)所需的时间

转载 作者:太空宇宙 更新时间:2023-11-04 13:36:19 25 4
gpt4 key购买 nike

我的项目中有大约 5000 个单元测试,我试图记录每个单元测试所花费的时间。我无法通过进入每个单元测试来编辑@Before 和@After。

我正在尝试使用@Rule,但不知怎的,这没有帮助,我什至无法让它工作,但我将注释更改为@ClassRule并使我的观察者静态,我至少能够掌握单元测试执行的开始和结束,我只是不知道单元测试的名称。这是我的代码:

我有一个名为 IIMUnitTest (父类)的类,所有单元测试都扩展该类。

我将以下代码添加到 IIMUnitTest

@Rule
public TestRule watcher = new TestWatcher() {
Long timeStart;
protected void starting(Description description) {
timeStart = System.currentTimeMillis();
Calendar cal = Calendar.getInstance();
System.out
.println("===========================================================================");
System.out.println("Test: " + description.getMethodName());
TimeZoneFormat dateFormat = null;
System.out.println("Start Time: " + dateFormat.format(cal.getTime()));
System.out
.println("==============================================");
}

protected void finished(Description description) {
Long timeEnd = System.currentTimeMillis();
double seconds = (timeEnd-timeStart)/1000.0;
System.out
.println("\n============================================================");
System.out
.println("Test completed - ran in: "+new DecimalFormat("0.000").format(seconds)+" sec");
System.out
.println("========================\n");

}
};

提前致谢。

最佳答案

您可以扩展规则StopWatch这是 JUnit 4.12 的一部分。它会为您进行计算。

关于java - 如何检查单元测试(全类)所需的时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31664742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com