记录花费的时间JUnit测试运行
我想记录需要多长时间我的JUnit测试以编程方式运行。我在各种测试类中有大量的测试,我想知道每种测试方法运行多长时间。记录花费的时间JUnit测试运行
我可以不同地更改继承结构或注释方法,但我想避免必须在测试方法本身内以及在用于设置测试业务逻辑的前/后方法内添加代码。
如果使用@Before和@After注解和注意的JUnit测试用例开始和结束时间。然后找到两个时间戳的差异应该会给你测试用例执行时间。类似这样的:
public class Example {
long startTime;
long endTime;
@Before public void recordStartTime() {
startTime = System.currentTimeMillis();
}
@Test public void testSomething() {
//test method
}
@After public void recordEndAndExecutionTime() {
endTime = System.currentTimeMillis();
System.out.println("Last testcase exection time in millisecond : " + (endTime - startTime));
}
}
尝试使用@Before和@After。 用@Before或@After注释的方法在测试之前或之后运行。
@Before
public void start() {
start = System.currentTimeMillis();
}
@After
public void end() {
System.out.println(System.currentTimeMillis() - start);
}
您还可以创建@Rule并实例化TestWatcher类。这对我来说很有用。这是在扩展TestCase的类中定义的。
public class CompositeWithTeardownDBUnitTest extends DBTestCase {
DBTestCase延伸CompositeWithTeardownDBUnitTest
@Rule
public TestRule watcher = new TestWatcher() {
protected void starting(Description description) {
timeStart = System.currentTimeMillis();
cal = Calendar.getInstance();
System.out
.println("===========================================================================");
System.out.println("Test: " + description.getMethodName());
System.out.println("Start Time: " + dateFormat.format(cal.getTime()));
System.out
.println("===========================================================================");
}
protected void finished(Description description) {
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");
}
};
的TestCase
代码片段和JUnit测试类只是继承这个类CompositeWithTeardownDBUnitTest。
除了现有的答案,您可以使用测试名称规则以及Before
和After
方法在日志中显示方法名称。就像这样:
public class ImageSavingTest {
@Rule
public TestName name = new TestName();
private long start;
@Before
public void start() {
start = System.currentTimeMillis();
}
@After
public void end() {
System.out.println("Test " + name.getMethodName() + " took " + (System.currentTimeMillis() - start) + " ms");
}
@Test
public void foobar() {
// test code here
}
}
将输出:
测试foobar的花1828毫秒
创建自己的TestWatcher
实现其捕获每个测试方法运行。使用番石榴Stopwatch
您可以测量时间为每个测试:
public class TimeTestWatcher extends TestWatcher {
private Stopwatch stopwatch = Stopwatch.createUnstarted();
protected void starting(Description description) {
stopwatch.start();
}
protected void finished(Description description) {
stopwatch.stop();
String testName = description.getMethodName();
long elapsed = stopwatch.elapsed(TimeUnit.MILLISECONDS);
System.out.println(String.format("Test %s took %d ms.", testName, elapsed));
}
};
然后用你的TimeTestWatcher
添加的JUnit @Rule
标注为每个测试类:
public class YourTest {
@Rule
public TimeTestWatcher watcher = new TimeTestWatcher();
@Test
public void testXXX() {}
@Test
public void testYYY() {}
}
你可以使用JUnit的秒表规则并覆盖其方法作为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(); }
子类继承父项。在Child类或方法之前或之后没有其他更改。
public class TestUniqueCharacterString extends ParentTestCase {
private String uniqueChars = null;
@Before
public void before(){
uniqueChars = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnop";
}
@Test
public void testMyUniqueCharacterMethod(){
UniqueCharacteString.doesStringHaveUniqueCharacters(uniqueChars);
}
或者
-
在每个测试类的包括此线
@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
样本输出
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
它也会显示时间和失败跳过测试用例。
您是否打算在构建工具中运行该工具,例如Maven?或者你想要Eclipse还是类似的? –