Browse Source

bz-64836 junitlauncher - Print a more informative and instant summary for tests

master
Jaikiran Pai 4 years ago
parent
commit
635ccfb5e4
2 changed files with 34 additions and 8 deletions
  1. +3
    -2
      manual/Tasks/junitlauncher.html
  2. +31
    -6
      src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/LauncherSupport.java

+ 3
- 2
manual/Tasks/junitlauncher.html View File

@@ -166,8 +166,9 @@
<tr>
<td>printSummary</td>
<td>If the value is set to <code>true</code> then this task, upon completion of the test execution,
prints the summary of the execution to <code>System.out</code>. The summary itself is generated
by the JUnit 5 platform and not by this task.
prints the summary of the execution to <code>System.out</code>. Starting Ant 1.10.10, unlike
in previous versions, this task itself generates the summary instead of using the one generated
by the JUnit 5 platform.
</td>
<td>No; defaults to <code>false</code></td>
</tr>


+ 31
- 6
src/main/org/apache/tools/ant/taskdefs/optional/junitlauncher/LauncherSupport.java View File

@@ -37,6 +37,7 @@ import org.junit.platform.launcher.Launcher;
import org.junit.platform.launcher.LauncherDiscoveryRequest;
import org.junit.platform.launcher.TagFilter;
import org.junit.platform.launcher.TestExecutionListener;
import org.junit.platform.launcher.TestIdentifier;
import org.junit.platform.launcher.TestPlan;
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
import org.junit.platform.launcher.core.LauncherFactory;
@@ -49,7 +50,6 @@ import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
@@ -128,7 +128,7 @@ public class LauncherSupport {
final List<TestExecutionListener> testExecutionListeners = new ArrayList<>();
// a listener that we always put at the front of list of listeners
// for this request.
final Listener firstListener = new Listener();
final Listener firstListener = new Listener(System.out);
// we always enroll the summary generating listener, to the request, so that we
// get to use some of the details of the summary for our further decision making
testExecutionListeners.add(firstListener);
@@ -301,10 +301,6 @@ public class LauncherSupport {
}

private void handleTestExecutionCompletion(final TestDefinition test, final TestExecutionSummary summary) {
if (this.launchDefinition.isPrintSummary()) {
// print the summary to System.out
summary.printTo(new PrintWriter(System.out, true));
}
final boolean hasTestFailures = summary.getTotalFailureCount() != 0;
if (hasTestFailures) {
// keep track of the test failure(s) for the entire launched instance
@@ -596,12 +592,41 @@ public class LauncherSupport {
}

private final class Listener extends SummaryGeneratingListener {
private final PrintStream originalSysOut;

private Optional<SwitchedStreamHandle> switchedSysOutHandle;
private Optional<SwitchedStreamHandle> switchedSysErrHandle;

private Listener(final PrintStream originalSysOut) {
this.originalSysOut = originalSysOut;
}

@Override
public void executionStarted(final TestIdentifier testIdentifier) {
super.executionStarted(testIdentifier);
AbstractJUnitResultFormatter.isTestClass(testIdentifier).ifPresent(testClass -> {
this.originalSysOut.println("Running " + testClass.getClassName());
});
}

@Override
public void testPlanExecutionFinished(final TestPlan testPlan) {
super.testPlanExecutionFinished(testPlan);
if (launchDefinition.isPrintSummary()) {
final TestExecutionSummary summary = this.getSummary();
// Keep the summary as close to as the old junit task summary
// tests run, failed, skipped, duration
final StringBuilder sb = new StringBuilder("Tests run: ");
sb.append(summary.getTestsStartedCount());
sb.append(", Failures: ");
sb.append(summary.getTestsFailedCount());
sb.append(", Skipped: ");
sb.append(summary.getTestsSkippedCount());
sb.append(", Time elapsed: ");
sb.append((summary.getTimeFinished() - summary.getTimeStarted()) / 1000f);
sb.append(" sec");
this.originalSysOut.println(sb.toString());
}
// now that the test plan execution is finished, close the switched sysout/syserr output streams
// and wait for the sysout and syserr content delivery, to result formatters, to finish
if (this.switchedSysOutHandle.isPresent()) {


Loading…
Cancel
Save