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 each individual test method takes to run.
I can change the inheritance structure or annotate methods differently, but I would like to avoid having to add code within the test method itself and also within the before/after methods which are used to setup test business logic.
Try to use @Before and @After. A method annotated with @Before or @After runs before or after the test.
@Before
public void start() {
start = System.currentTimeMillis();
}
@After
public void end() {
System.out.println(System.currentTimeMillis() - start);
}
You could use the JUnit StopWatch rule and override its methods as provided in the JUnit API documentation and have the time printed to console or log file for each test just by including one line of code in each of your individual test case class.
Create your Customer StopWatch class (Sample provided)
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);
}
}
Have a ParentTestClass created with that line and each of your test class inherit the parent test class:
public class ParentTestCase {
@Rule
public MyJUnitStopWatch stopwatch = new MyJUnitStopWatch();
}
Child classes inherits parent. No other change in Child classes or before or after methods.
public class TestUniqueCharacterString extends ParentTestCase {
private String uniqueChars = null;
@Before
public void before(){
uniqueChars = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnop";
}
@Test
public void testMyUniqueCharacterMethod(){
UniqueCharacteString.doesStringHaveUniqueCharacters(uniqueChars);
}
Or
Include this line in each of your Test class
@Rule
public MyJUnitStopWatch stopwatch = new MyJUnitStopWatch();
Sample Test Class:
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 reference:
http://junit.org/apidocs/org/junit/rules/Stopwatch.html
Sample Output
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
It will also show time for failed and skipped test cases.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With