I would like to record how long it takes my JUnit test to run programmatically. I have a large number of tests in various test classes, and I would like to find out how long ea
You can also create a @Rule and instantiate the TestWatcher class. This is what worked for me. This is being defined in a class that extends TestCase.
public class CompositeWithTeardownDBUnitTest extends DBTestCase {
DBTestCase extends TestCase
code snippet in 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");
}
};
And the JUnit test classes just extend this class CompositeWithTeardownDBUnitTest.