我想要以程序记录JUnit测试运行的时间。我的测试类中有很多测试方法,我希望能够找出每个测试方法的执行时间。
我可以更改继承结构或在方法上添加注释,但是我不想在测试方法内部或设置测试业务逻辑的before/after方法中添加代码。
我可以更改继承结构或在方法上添加注释,但是我不想在测试方法内部或设置测试业务逻辑的before/after方法中添加代码。
您可以使用JUnit StopWatch规则,并按照JUnit API文档中提供的方法覆盖其方法,通过在每个单独的测试用例类中包含一行代码,将时间打印到控制台或日志文件中。
创建您的自定义StopWatch类(已提供示例)
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);
}
或者
在每个测试类中都要包含这一行
@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
它还会显示失败和跳过测试用例的时间。
尝试使用@Before和@After注解。 使用@Before或@After注解的方法会在测试之前或之后运行。
@Before
public void start() {
start = System.currentTimeMillis();
}
@After
public void end() {
System.out.println(System.currentTimeMillis() - start);
}
public class CompositeWithTeardownDBUnitTest extends DBTestCase {
DBTestCase扩展了TestCase
在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");
}
};
JUnit测试类只需扩展这个名为CompositeWithTeardownDBUnitTest的类。
创建自己的TestWatcher
实现,以捕获每个测试方法的运行。使用Guava 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));
}
};
然后在每个测试类中使用JUnit的@Rule
注解和你的TimeTestWatcher
:
public class YourTest {
@Rule
public TimeTestWatcher watcher = new TimeTestWatcher();
@Test
public void testXXX() {}
@Test
public void testYYY() {}
}
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
方法来在日志中显示方法名称。像这样: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 毫秒