è possibile utilizzare la regola JUnit cronometro e sovrascrivere i suoi metodi come previsto nella documentazione dell'API JUnit e avere l'ora stampata su console o file di registro per ogni test includendo solo una riga di codice in ciascuna delle singole classi di test case.
Create il vostro cliente di classe cronometro (esempio fornito)
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);
}
}
Avere un ParentTestClass creata con quella linea e ciascuna della vostra classe di test ereditare la classe sperimentale progenitore:
public class ParentTestCase {
@Rule
public MyJUnitStopWatch stopwatch = new MyJUnitStopWatch();
}
Le classi figlio ereditano il genitore. Nessun altro cambiamento nelle classi Child o prima o dopo i metodi.
public class TestUniqueCharacterString extends ParentTestCase {
private String uniqueChars = null;
@Before
public void before(){
uniqueChars = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnop";
}
@Test
public void testMyUniqueCharacterMethod(){
UniqueCharacteString.doesStringHaveUniqueCharacters(uniqueChars);
}
O
Includere questa linea in ciascuna della vostra classe Test
@Rule
public MyJUnitStopWatch stopwatch = new MyJUnitStopWatch();
campione Classe di prova:
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 riferimento API:
http://junit.org/apidocs/org/junit/rules/Stopwatch.html
Esempio di 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
Inoltre mostrerà il tempo per casi di test falliti e saltato.
Hai intenzione di eseguirlo in uno strumento di creazione, ad esempio Maven? O vuoi Eclipse o simili? –