Skip to content

Prune stack traces up to test or lifecycle method #4829

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ repository on GitHub.
[[release-notes-6.0.0-RC1-junit-platform-new-features-and-improvements]]
==== New Features and Improvements

* ❓

* Prune stack traces up to test or lifecycle method

[[release-notes-6.0.0-RC1-junit-jupiter]]
=== JUnit Jupiter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,15 @@ public static String readStackTrace(Throwable throwable) {
}

/**
* Prune the stack trace of the supplied {@link Throwable} by removing
* {@linkplain StackTraceElement stack trace elements} from the {@code org.junit},
* {@code jdk.internal.reflect}, and {@code sun.reflect} packages. If a
* {@code StackTraceElement} matching one of the supplied {@code classNames}
* is encountered, all subsequent elements in the stack trace will be retained.
* Prune the stack trace of the supplied {@link Throwable}.
*
* <p>Prune all {@linkplain StackTraceElement stack trace elements} up one
* of the supplied {@code classNames} are pruned. All subsequent elements
* in the stack trace will be retained.
*
* <p>If the {@code classNames} do not match any of the stacktrace elements
* then the {@code org.junit}, {@code jdk.internal.reflect}, and
* {@code sun.reflect} packages are pruned.
*
* <p>Additionally, all elements prior to and including the first JUnit Platform
* Launcher call will be removed.
Expand All @@ -128,6 +132,9 @@ public static void pruneStackTrace(Throwable throwable, List<String> classNames)
String className = element.getClassName();

if (classNames.contains(className)) {
// We found the test
// everything before that is not informative.
prunedStackTrace.clear();
// Include all elements called by the test
prunedStackTrace.addAll(stackTrace.subList(i, stackTrace.size()));
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Stream;

import org.junit.platform.commons.util.ExceptionUtils;
import org.junit.platform.engine.EngineExecutionListener;
Expand Down Expand Up @@ -46,8 +47,9 @@ public void executionFinished(TestDescriptor testDescriptor, TestExecutionResult
}

private static List<String> getTestClassNames(TestDescriptor testDescriptor) {
return testDescriptor.getAncestors() //
.stream() //
Stream<? extends TestDescriptor> self = Stream.of(testDescriptor);
Stream<? extends TestDescriptor> ancestors = testDescriptor.getAncestors().stream();
return Stream.concat(self, ancestors) //
.map(TestDescriptor::getSource) //
.flatMap(Optional::stream) //
.map(source -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ void shouldAlwaysKeepJupiterAssumptionStackTraceElement() {
}

@Test
void shouldKeepEverythingAfterTestCall() {
void shouldKeepExactlyEverythingAfterTestCall() {
EngineExecutionResults results = EngineTestKit.engine("junit-jupiter") //
.configurationParameter("junit.platform.stacktrace.pruning.enabled", "true") //
.selectors(selectMethod(FailingTestTestCase.class, "failingAssertion")) //
Expand All @@ -128,15 +128,14 @@ void shouldKeepEverythingAfterTestCall() {
\\Qorg.junit.jupiter.api.AssertionUtils.fail(AssertionUtils.java:\\E.+
\\Qorg.junit.jupiter.api.Assertions.fail(Assertions.java:\\E.+
\\Qorg.junit.platform.StackTracePruningTests$FailingTestTestCase.failingAssertion(StackTracePruningTests.java:\\E.+
>>>>
""");
}

@ParameterizedTest
@ValueSource(strings = { "org.junit.platform.StackTracePruningTests$FailingBeforeEachTestCase",
"org.junit.platform.StackTracePruningTests$FailingBeforeEachTestCase$NestedTestCase",
"org.junit.platform.StackTracePruningTests$FailingBeforeEachTestCase$NestedTestCase$NestedNestedTestCase" })
void shouldKeepEverythingAfterLifecycleMethodCall(Class<?> methodClass) {
void shouldKeepExactlyEverythingAfterLifecycleMethodCall(Class<?> methodClass) {
EngineExecutionResults results = EngineTestKit.engine("junit-jupiter") //
.configurationParameter("junit.platform.stacktrace.pruning.enabled", "true") //
.selectors(selectMethod(methodClass, "test")) //
Expand All @@ -149,7 +148,6 @@ void shouldKeepEverythingAfterLifecycleMethodCall(Class<?> methodClass) {
\\Qorg.junit.jupiter.api.AssertionUtils.fail(AssertionUtils.java:\\E.+
\\Qorg.junit.jupiter.api.Assertions.fail(Assertions.java:\\E.+
\\Qorg.junit.platform.StackTracePruningTests$FailingBeforeEachTestCase.setUp(StackTracePruningTests.java:\\E.+
>>>>
""");
}

Expand Down
Loading