Skip to content

Commit

Permalink
Issue: junit-team#3139 Update to display each test method info
Browse files Browse the repository at this point in the history
1. Add a `testPlan` getter to `platform/launcher/listeners/SummaryGeneratingListener.java`.

2. DiffPrinter now needs testPlan to display the testMethod info.
Issue: junit-team#3139
  • Loading branch information
XJ114514 committed Oct 19, 2024
1 parent 2383f3e commit 82f723a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public class ConsoleTestExecutor {
private final TestConsoleOutputOptions outputOptions;
private final Supplier<Launcher> launcherSupplier;

private TestPlan testPlanListeners;

public ConsoleTestExecutor(TestDiscoveryOptions discoveryOptions, TestConsoleOutputOptions outputOptions) {
this(discoveryOptions, outputOptions, LauncherFactory::create);
}
Expand Down Expand Up @@ -79,7 +81,6 @@ private void discoverTests(PrintWriter out) {

LauncherDiscoveryRequest discoveryRequest = new DiscoveryRequestCreator().toDiscoveryRequest(discoveryOptions);
TestPlan testPlan = launcher.discover(discoveryRequest);

commandLineTestPrinter.ifPresent(printer -> printer.listTests(testPlan));
if (outputOptions.getDetails() != Details.NONE) {
printFoundTestsSummary(out, testPlan);
Expand All @@ -103,6 +104,8 @@ private TestExecutionSummary executeTests(PrintWriter out, Optional<Path> report
launcher.execute(discoveryRequest);
TestExecutionSummary summary = summaryListener.getSummary();
if (summary.getTotalFailureCount() > 0 || outputOptions.getDetails() != Details.NONE) {
//get testPlan from summaryListener
testPlanListeners = summaryListener.getTestPlan();
printSummary(summary, out);
}

Expand Down Expand Up @@ -182,6 +185,7 @@ private void printSummary(TestExecutionSummary summary, PrintWriter out) {
if (EnumSet.of(Details.NONE, Details.SUMMARY, Details.TREE).contains(outputOptions.getDetails())) {
summary.printFailuresTo(out);
//adding diff code here
out.printf("%nDiffs (Markdown):%n");
summary.getFailures().forEach(failure -> {
//get AssertionFailedError
if (failure.getException() instanceof AssertionFailedError) {
Expand All @@ -190,10 +194,8 @@ private void printSummary(TestExecutionSummary summary, PrintWriter out) {
ValueWrapper actual = assertionFailedError.getActual();
//apply diff function
if (isCharSequence(expected) && isCharSequence(actual)) {
out.printf("%nDiffs (Markdown):%n");
out.printf(" %s:", failure.getTestIdentifier().getDisplayName());
DiffPrinter.printDiff(out, expected.getStringRepresentation(),
actual.getStringRepresentation());
new DiffPrinter(testPlanListeners).printDiff(out, expected.getStringRepresentation(),
actual.getStringRepresentation(), failure.getTestIdentifier());
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,32 @@

package org.junit.platform.console.tasks;

import static java.lang.String.join;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import com.github.difflib.text.DiffRow;
import com.github.difflib.text.DiffRowGenerator;

import org.junit.platform.launcher.TestIdentifier;
import org.junit.platform.launcher.TestPlan;

/**
* class provide access to printDiff function
*/
class DiffPrinter {
private final TestPlan testPlan;

public DiffPrinter(TestPlan testPlan) {
this.testPlan = testPlan;
}

//print the difference of two print to out
static void printDiff(PrintWriter out, String expected, String actual) {
void printDiff(PrintWriter out, String expected, String actual, TestIdentifier testIdentifier) {
out.printf(" %s:", describeTest(testIdentifier));
boolean inlineDiffByWordFlag = false;
if (expected.contains(" ") || actual.contains(" ")) {
inlineDiffByWordFlag = true;
Expand All @@ -38,4 +51,15 @@ static void printDiff(PrintWriter out, String expected, String actual) {
out.println();
}
}

private String describeTest(TestIdentifier testIdentifier) {
List<String> descriptionParts = new ArrayList<>();
collectTestDescription(testIdentifier, descriptionParts);
return join(":", descriptionParts);
}

private void collectTestDescription(TestIdentifier identifier, List<String> descriptionParts) {
descriptionParts.add(0, identifier.getDisplayName());
testPlan.getParent(identifier).ifPresent(parent -> collectTestDescription(parent, descriptionParts));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ public TestExecutionSummary getSummary() {
return this.summary;
}

/**
* Get the testPlan of this listener.
*/
public TestPlan getTestPlan() {
return testPlan;
}

@Override
public void testPlanExecutionStarted(TestPlan testPlan) {
this.testPlan = testPlan;
Expand Down

0 comments on commit 82f723a

Please sign in to comment.