Skip to content

Commit 82f723a

Browse files
committed
Issue: #3139 Update to display each test method info
1. Add a `testPlan` getter to `platform/launcher/listeners/SummaryGeneratingListener.java`. 2. DiffPrinter now needs testPlan to display the testMethod info. Issue: #3139
1 parent 2383f3e commit 82f723a

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

junit-platform-console/src/main/java/org/junit/platform/console/tasks/ConsoleTestExecutor.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ public class ConsoleTestExecutor {
4949
private final TestConsoleOutputOptions outputOptions;
5050
private final Supplier<Launcher> launcherSupplier;
5151

52+
private TestPlan testPlanListeners;
53+
5254
public ConsoleTestExecutor(TestDiscoveryOptions discoveryOptions, TestConsoleOutputOptions outputOptions) {
5355
this(discoveryOptions, outputOptions, LauncherFactory::create);
5456
}
@@ -79,7 +81,6 @@ private void discoverTests(PrintWriter out) {
7981

8082
LauncherDiscoveryRequest discoveryRequest = new DiscoveryRequestCreator().toDiscoveryRequest(discoveryOptions);
8183
TestPlan testPlan = launcher.discover(discoveryRequest);
82-
8384
commandLineTestPrinter.ifPresent(printer -> printer.listTests(testPlan));
8485
if (outputOptions.getDetails() != Details.NONE) {
8586
printFoundTestsSummary(out, testPlan);
@@ -103,6 +104,8 @@ private TestExecutionSummary executeTests(PrintWriter out, Optional<Path> report
103104
launcher.execute(discoveryRequest);
104105
TestExecutionSummary summary = summaryListener.getSummary();
105106
if (summary.getTotalFailureCount() > 0 || outputOptions.getDetails() != Details.NONE) {
107+
//get testPlan from summaryListener
108+
testPlanListeners = summaryListener.getTestPlan();
106109
printSummary(summary, out);
107110
}
108111

@@ -182,6 +185,7 @@ private void printSummary(TestExecutionSummary summary, PrintWriter out) {
182185
if (EnumSet.of(Details.NONE, Details.SUMMARY, Details.TREE).contains(outputOptions.getDetails())) {
183186
summary.printFailuresTo(out);
184187
//adding diff code here
188+
out.printf("%nDiffs (Markdown):%n");
185189
summary.getFailures().forEach(failure -> {
186190
//get AssertionFailedError
187191
if (failure.getException() instanceof AssertionFailedError) {
@@ -190,10 +194,8 @@ private void printSummary(TestExecutionSummary summary, PrintWriter out) {
190194
ValueWrapper actual = assertionFailedError.getActual();
191195
//apply diff function
192196
if (isCharSequence(expected) && isCharSequence(actual)) {
193-
out.printf("%nDiffs (Markdown):%n");
194-
out.printf(" %s:", failure.getTestIdentifier().getDisplayName());
195-
DiffPrinter.printDiff(out, expected.getStringRepresentation(),
196-
actual.getStringRepresentation());
197+
new DiffPrinter(testPlanListeners).printDiff(out, expected.getStringRepresentation(),
198+
actual.getStringRepresentation(), failure.getTestIdentifier());
197199
}
198200

199201
}

junit-platform-console/src/main/java/org/junit/platform/console/tasks/DiffPrinter.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,32 @@
1010

1111
package org.junit.platform.console.tasks;
1212

13+
import static java.lang.String.join;
14+
1315
import java.io.PrintWriter;
16+
import java.util.ArrayList;
1417
import java.util.Arrays;
1518
import java.util.List;
1619

1720
import com.github.difflib.text.DiffRow;
1821
import com.github.difflib.text.DiffRowGenerator;
1922

23+
import org.junit.platform.launcher.TestIdentifier;
24+
import org.junit.platform.launcher.TestPlan;
25+
2026
/**
2127
* class provide access to printDiff function
2228
*/
2329
class DiffPrinter {
30+
private final TestPlan testPlan;
31+
32+
public DiffPrinter(TestPlan testPlan) {
33+
this.testPlan = testPlan;
34+
}
35+
2436
//print the difference of two print to out
25-
static void printDiff(PrintWriter out, String expected, String actual) {
37+
void printDiff(PrintWriter out, String expected, String actual, TestIdentifier testIdentifier) {
38+
out.printf(" %s:", describeTest(testIdentifier));
2639
boolean inlineDiffByWordFlag = false;
2740
if (expected.contains(" ") || actual.contains(" ")) {
2841
inlineDiffByWordFlag = true;
@@ -38,4 +51,15 @@ static void printDiff(PrintWriter out, String expected, String actual) {
3851
out.println();
3952
}
4053
}
54+
55+
private String describeTest(TestIdentifier testIdentifier) {
56+
List<String> descriptionParts = new ArrayList<>();
57+
collectTestDescription(testIdentifier, descriptionParts);
58+
return join(":", descriptionParts);
59+
}
60+
61+
private void collectTestDescription(TestIdentifier identifier, List<String> descriptionParts) {
62+
descriptionParts.add(0, identifier.getDisplayName());
63+
testPlan.getParent(identifier).ifPresent(parent -> collectTestDescription(parent, descriptionParts));
64+
}
4165
}

junit-platform-launcher/src/main/java/org/junit/platform/launcher/listeners/SummaryGeneratingListener.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ public TestExecutionSummary getSummary() {
4646
return this.summary;
4747
}
4848

49+
/**
50+
* Get the testPlan of this listener.
51+
*/
52+
public TestPlan getTestPlan() {
53+
return testPlan;
54+
}
55+
4956
@Override
5057
public void testPlanExecutionStarted(TestPlan testPlan) {
5158
this.testPlan = testPlan;

0 commit comments

Comments
 (0)