-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Output diff of expected and actual values in ConsoleLauncher
#4017
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 13 commits
e0d2849
bc1b464
2fa9a8e
50552b4
e8d11bd
7b4088d
bb29695
2c27b53
af16685
3d73d1e
c8a7b95
53d884c
26bcaa4
3248752
04f789a
983e090
eef49f1
bcd0608
cc69bfb
705b4cf
2383f3e
82f723a
536e9c7
f44f929
eef975b
de00ade
a3f8f25
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,11 +16,15 @@ | |
import java.net.URL; | ||
import java.net.URLClassLoader; | ||
import java.nio.file.Path; | ||
import java.util.Arrays; | ||
import java.util.EnumSet; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.function.Supplier; | ||
|
||
import com.github.difflib.text.DiffRow; | ||
import com.github.difflib.text.DiffRowGenerator; | ||
|
||
import org.apiguardian.api.API; | ||
import org.junit.platform.commons.JUnitException; | ||
import org.junit.platform.commons.util.ClassLoaderUtils; | ||
|
@@ -36,6 +40,8 @@ | |
import org.junit.platform.launcher.listeners.SummaryGeneratingListener; | ||
import org.junit.platform.launcher.listeners.TestExecutionSummary; | ||
import org.junit.platform.reporting.legacy.xml.LegacyXmlReportGeneratingListener; | ||
import org.opentest4j.AssertionFailedError; | ||
import org.opentest4j.ValueWrapper; | ||
|
||
/** | ||
* @since 1.0 | ||
|
@@ -180,11 +186,39 @@ private Optional<TestExecutionListener> createXmlWritingListener(PrintWriter out | |
private void printSummary(TestExecutionSummary summary, PrintWriter out) { | ||
// Otherwise the failures have already been printed in detail | ||
if (EnumSet.of(Details.NONE, Details.SUMMARY, Details.TREE).contains(outputOptions.getDetails())) { | ||
//adding diff code here | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be done in place where we print the exceptions (as it is in #3397). Otherwise, it would be difficult to find the test that belongs to the diff in case of multiple failing tests. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Which part do you wish to change? The first print happens in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We also need to make sure that the diff is printed if a details mode other than There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The solutions I can think of:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I think it should stay in junit-platform-console. Please create a separate There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
summary.getFailures().forEach(failure -> { | ||
//get AssertionFailedError | ||
if (failure.getException() instanceof AssertionFailedError) { | ||
AssertionFailedError assertionFailedError = (AssertionFailedError) failure.getException(); | ||
ValueWrapper expected = assertionFailedError.getExpected(); | ||
ValueWrapper actual = assertionFailedError.getActual(); | ||
//apply diff function | ||
if (isCharSequence(expected) && isCharSequence(actual)) { | ||
DiffRowGenerator generator = DiffRowGenerator.create().showInlineDiffs(true).inlineDiffByWord( | ||
true).oldTag(f -> "~").newTag(f -> "**").build(); | ||
List<DiffRow> rows = generator.generateDiffRows( | ||
Arrays.asList(expected.getStringRepresentation()), | ||
Arrays.asList(actual.getStringRepresentation())); | ||
|
||
out.printf( | ||
"\nPlease put the diff result below into a online markdown editor to see markdown effect: \n"); | ||
for (DiffRow row : rows) { | ||
out.printf(" | %s | %s | \n", row.getOldLine(), row.getNewLine()); | ||
} | ||
} | ||
|
||
} | ||
}); | ||
summary.printFailuresTo(out); | ||
} | ||
summary.printTo(out); | ||
} | ||
|
||
private boolean isCharSequence(ValueWrapper value) { | ||
return value != null && CharSequence.class.isAssignableFrom(value.getType()); | ||
} | ||
|
||
@FunctionalInterface | ||
public interface Factory { | ||
ConsoleTestExecutor create(TestDiscoveryOptions discoveryOptions, TestConsoleOutputOptions outputOptions); | ||
|
Uh oh!
There was an error while loading. Please reload this page.