Skip to content

Commit

Permalink
7903752: Include duration information in JUnit-based tests
Browse files Browse the repository at this point in the history
Reviewed-by: mcimadamore
  • Loading branch information
sormuras committed Jun 14, 2024
1 parent 2e57b47 commit 8af904b
Showing 1 changed file with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import org.junit.platform.engine.TestExecutionResult;
import org.junit.platform.engine.TestSource;
import org.junit.platform.engine.UniqueId;
import org.junit.platform.engine.discovery.DiscoverySelectors;
import org.junit.platform.engine.reporting.ReportEntry;
import org.junit.platform.engine.support.descriptor.MethodSource;
Expand All @@ -44,7 +45,10 @@
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Method;
import java.time.Duration;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

Expand Down Expand Up @@ -190,6 +194,7 @@ static class PrintingListener implements TestExecutionListener {
final PrintWriter printer;
final Lock lock;
final AgentVerbose verbose;
final Map<UniqueId, Long> startNanosByUniqueId = new ConcurrentHashMap<>();

PrintingListener(PrintStream stream, AgentVerbose verbose) {
this(new PrintWriter(stream, true), verbose);
Expand Down Expand Up @@ -220,6 +225,7 @@ public void executionSkipped(TestIdentifier identifier, String reason) {

@Override
public void executionStarted(TestIdentifier identifier) {
startNanosByUniqueId.put(identifier.getUniqueIdObject(), System.nanoTime());
if (verbose.passMode == AgentVerbose.Mode.NONE) return;
if (identifier.isTest()) {
String status = "STARTED";
Expand All @@ -241,6 +247,10 @@ public void executionFinished(TestIdentifier identifier, TestExecutionResult res
if (status == TestExecutionResult.Status.SUCCESSFUL) {
if (verbose.passMode == AgentVerbose.Mode.NONE) return;
}
Long startNanos = startNanosByUniqueId.remove(identifier.getUniqueIdObject());
Duration duration = startNanos == null
? Duration.ZERO
: Duration.ofNanos(System.nanoTime() - startNanos);
lock.lock();
try {
if (status == TestExecutionResult.Status.ABORTED) {
Expand All @@ -252,7 +262,8 @@ public void executionFinished(TestIdentifier identifier, TestExecutionResult res
if (identifier.isTest()) {
String source = toSourceString(identifier);
String name = identifier.getDisplayName();
printer.printf("%-10s %s '%s'%n", status, source, name);
long millis = duration.toMillis();
printer.printf("%-10s %s '%s' [%dms]%n", status, source, name, millis);
}
}
finally {
Expand Down

0 comments on commit 8af904b

Please sign in to comment.