我想记录我的 JUnit 测试以编程方式运行需要多长时间。我在各种测试类中有大量测试,我想了解每个单独的测试方法运行需要多长时间。
我可以更改继承结构或以不同方式注释方法,但我希望避免在测试方法本身以及用于设置测试业务逻辑的之前/之后方法中添加代码。
您可以使用 JUnit StopWatch 规则并重写 JUnit API 文档中提供的方法,并且只需在每个单独的测试用例类中包含一行代码即可将每个测试的时间打印到控制台或日志文件中。
-
创建您的客户秒表类(提供样品)
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();
}
子类继承父类。子类或方法之前或之后没有其他变化。
public class TestUniqueCharacterString extends ParentTestCase {
private String uniqueChars = null;
@Before
public void before(){
uniqueChars = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnop";
}
@Test
public void testMyUniqueCharacterMethod(){
UniqueCharacteString.doesStringHaveUniqueCharacters(uniqueChars);
}
Or
-
将此行包含在您的每个测试类中
@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 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
它还将显示失败和跳过的测试用例的时间。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)