forked from junit-team/junit5
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce testfeed details mode for ConsoleLauncher (junit-team#3244)
The new `testfeed` mode provides a concise real-time stream of test execution events. Co-authored-by: halitanildonmez <halitanil.donmez@gmail.com>
- Loading branch information
Showing
30 changed files
with
661 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
130 changes: 130 additions & 0 deletions
130
...form-console/src/main/java/org/junit/platform/console/tasks/TestFeedPrintingListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
/* | ||
* Copyright 2015-2023 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
|
||
package org.junit.platform.console.tasks; | ||
|
||
import static org.junit.platform.engine.TestExecutionResult.Status.SUCCESSFUL; | ||
|
||
import java.io.PrintWriter; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
import org.junit.platform.commons.util.ExceptionUtils; | ||
import org.junit.platform.engine.TestExecutionResult; | ||
import org.junit.platform.engine.UniqueId; | ||
import org.junit.platform.launcher.TestIdentifier; | ||
import org.junit.platform.launcher.TestPlan; | ||
|
||
class TestFeedPrintingListener implements DetailsPrintingListener { | ||
|
||
private static final String INDENTATION = "\t"; | ||
private static final String STATUS_SEPARATOR = " :: "; | ||
|
||
private final PrintWriter out; | ||
private final ColorPalette colorPalette; | ||
private TestPlan testPlan; | ||
|
||
TestFeedPrintingListener(PrintWriter out, ColorPalette colorPalette) { | ||
this.out = out; | ||
this.colorPalette = colorPalette; | ||
} | ||
|
||
@Override | ||
public void testPlanExecutionStarted(TestPlan testPlan) { | ||
this.testPlan = testPlan; | ||
} | ||
|
||
@Override | ||
public void testPlanExecutionFinished(TestPlan testPlan) { | ||
this.testPlan = null; | ||
} | ||
|
||
@Override | ||
public void executionSkipped(TestIdentifier testIdentifier, String reason) { | ||
if (shouldPrint(testIdentifier)) { | ||
String msg = formatTestIdentifier(testIdentifier); | ||
String indentedReason = indented(String.format("Reason: %s", reason)); | ||
println(Style.SKIPPED, | ||
String.format("%s" + STATUS_SEPARATOR + "SKIPPED%n" + INDENTATION + "%s", msg, indentedReason)); | ||
} | ||
} | ||
|
||
@Override | ||
public void executionStarted(TestIdentifier testIdentifier) { | ||
if (shouldPrint(testIdentifier)) { | ||
String msg = formatTestIdentifier(testIdentifier); | ||
println(Style.NONE, String.format("%s" + STATUS_SEPARATOR + "STARTED", msg)); | ||
} | ||
} | ||
|
||
@Override | ||
public void executionFinished(TestIdentifier testIdentifier, TestExecutionResult testExecutionResult) { | ||
TestExecutionResult.Status status = testExecutionResult.getStatus(); | ||
if (testExecutionResult.getThrowable().isPresent()) { | ||
Style style = Style.valueOf(testExecutionResult); | ||
String msg = formatTestIdentifier(testIdentifier); | ||
Throwable throwable = testExecutionResult.getThrowable().get(); | ||
String stacktrace = indented(ExceptionUtils.readStackTrace(throwable)); | ||
println(style, | ||
String.format("%s" + STATUS_SEPARATOR + "%s%n" + INDENTATION + "%s", msg, status, stacktrace)); | ||
} | ||
else if (shouldPrint(testIdentifier) || testExecutionResult.getStatus() != SUCCESSFUL) { | ||
Style style = Style.valueOf(testExecutionResult); | ||
String msg = formatTestIdentifier(testIdentifier); | ||
println(style, String.format("%s" + STATUS_SEPARATOR + "%s", msg, status)); | ||
} | ||
} | ||
|
||
private String formatTestIdentifier(TestIdentifier testIdentifier) { | ||
return String.join(" > ", collectDisplayNames(testIdentifier.getUniqueIdObject())); | ||
} | ||
|
||
private void println(Style style, String message) { | ||
this.out.println(colorPalette.paint(style, message)); | ||
} | ||
|
||
private List<String> collectDisplayNames(UniqueId uniqueId) { | ||
int size = uniqueId.getSegments().size(); | ||
List<String> displayNames = new ArrayList<>(size); | ||
for (int i = 0; i < size; i++) { | ||
displayNames.add(0, testPlan.getTestIdentifier(uniqueId).getDisplayName()); | ||
if (i < size - 1) { | ||
uniqueId = uniqueId.removeLastSegment(); | ||
} | ||
} | ||
return displayNames; | ||
} | ||
|
||
private static String indented(String message) { | ||
return DetailsPrintingListener.indented(message, INDENTATION); | ||
} | ||
|
||
@Override | ||
public void listTests(TestPlan testPlan) { | ||
this.testPlan = testPlan; | ||
try { | ||
testPlan.accept(new TestPlan.Visitor() { | ||
@Override | ||
public void visit(TestIdentifier testIdentifier) { | ||
if (shouldPrint(testIdentifier)) { | ||
println(Style.NONE, formatTestIdentifier(testIdentifier)); | ||
} | ||
} | ||
}); | ||
} | ||
finally { | ||
this.testPlan = null; | ||
} | ||
} | ||
|
||
private static boolean shouldPrint(TestIdentifier testIdentifier) { | ||
return testIdentifier.isTest(); | ||
} | ||
} |
115 changes: 115 additions & 0 deletions
115
...m-tests/src/test/java/org/junit/platform/console/tasks/TestFeedPrintingListenerTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
/* | ||
* Copyright 2015-2023 the original author or authors. | ||
* | ||
* All rights reserved. This program and the accompanying materials are | ||
* made available under the terms of the Eclipse Public License v2.0 which | ||
* accompanies this distribution and is available at | ||
* | ||
* https://www.eclipse.org/legal/epl-v20.html | ||
*/ | ||
|
||
package org.junit.platform.console.tasks; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertLinesMatch; | ||
import static org.junit.platform.commons.util.CollectionUtils.getOnlyElement; | ||
import static org.mockito.Mockito.mock; | ||
|
||
import java.io.PrintWriter; | ||
import java.io.StringWriter; | ||
import java.util.Collections; | ||
import java.util.stream.Stream; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.platform.engine.TestExecutionResult; | ||
import org.junit.platform.engine.UniqueId; | ||
import org.junit.platform.engine.support.descriptor.EngineDescriptor; | ||
import org.junit.platform.fakes.TestDescriptorStub; | ||
import org.junit.platform.launcher.TestIdentifier; | ||
import org.junit.platform.launcher.TestPlan; | ||
import org.opentest4j.TestAbortedException; | ||
|
||
public class TestFeedPrintingListenerTests { | ||
|
||
TestPlan testPlan; | ||
TestIdentifier testIdentifier; | ||
|
||
StringWriter stringWriter = new StringWriter(); | ||
TestFeedPrintingListener listener = new TestFeedPrintingListener(new PrintWriter(stringWriter), ColorPalette.NONE); | ||
|
||
@BeforeEach | ||
void prepareListener() { | ||
var engineDescriptor = new EngineDescriptor(UniqueId.forEngine("demo-engine"), "Demo Engine"); | ||
var testDescriptor = new TestDescriptorStub(engineDescriptor.getUniqueId().append("type", "test"), | ||
"%c ool test"); | ||
engineDescriptor.addChild(testDescriptor); | ||
|
||
testPlan = TestPlan.from(Collections.singleton(engineDescriptor), mock()); | ||
testIdentifier = testPlan.getTestIdentifier(testDescriptor.getUniqueId()); | ||
|
||
listener.testPlanExecutionStarted(testPlan); | ||
} | ||
|
||
@Test | ||
public void testExecutionSkipped() { | ||
listener.executionSkipped(testIdentifier, "Test disabled"); | ||
assertLinesMatch( // | ||
""" | ||
Demo Engine > %c ool test :: SKIPPED | ||
\tReason: Test disabled | ||
""".lines(), // | ||
actualLines() // | ||
); | ||
} | ||
|
||
@Test | ||
public void testExecutionFailed() { | ||
listener.executionFinished(testIdentifier, TestExecutionResult.failed(new AssertionError("Boom!"))); | ||
assertLinesMatch( // | ||
""" | ||
Demo Engine > %c ool test :: FAILED | ||
\tjava.lang.AssertionError: Boom! | ||
>>>> | ||
""".lines(), // | ||
actualLines() // | ||
); | ||
} | ||
|
||
@Test | ||
public void testExecutionAborted() { | ||
listener.executionFinished(testIdentifier, TestExecutionResult.aborted(new TestAbortedException("Boom!"))); | ||
assertLinesMatch( // | ||
""" | ||
Demo Engine > %c ool test :: ABORTED | ||
\torg.opentest4j.TestAbortedException: Boom! | ||
>>>> | ||
""".lines(), // | ||
actualLines() // | ||
); | ||
} | ||
|
||
@Test | ||
public void testExecutionSucceeded() { | ||
listener.executionFinished(testIdentifier, TestExecutionResult.successful()); | ||
assertLinesMatch(Stream.of("Demo Engine > %c ool test :: SUCCESSFUL"), actualLines()); | ||
} | ||
|
||
@Test | ||
public void testExecutionFailedOfContainer() { | ||
var engineIdentifier = getOnlyElement(testPlan.getRoots()); | ||
listener.executionFinished(engineIdentifier, TestExecutionResult.failed(new AssertionError("Boom!"))); | ||
assertLinesMatch( // | ||
""" | ||
Demo Engine :: FAILED | ||
\tjava.lang.AssertionError: Boom! | ||
>>>> | ||
""".lines(), // | ||
actualLines() // | ||
); | ||
} | ||
|
||
private Stream<String> actualLines() { | ||
return stringWriter.toString().lines(); | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
...s/src/test/resources/console/details/basic/Basic-changeDisplayName-testfeed-ascii.out.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
JUnit Jupiter > Basic > .oO fancy display name Oo. :: STARTED | ||
JUnit Jupiter > Basic > .oO fancy display name Oo. :: SUCCESSFUL | ||
|
||
Test run finished after [\d]+ ms | ||
[ 2 containers found ] | ||
[ 0 containers skipped ] | ||
[ 2 containers started ] | ||
[ 0 containers aborted ] | ||
[ 2 containers successful ] | ||
[ 0 containers failed ] | ||
[ 1 tests found ] | ||
[ 0 tests skipped ] | ||
[ 1 tests started ] | ||
[ 0 tests aborted ] | ||
[ 1 tests successful ] | ||
[ 0 tests failed ] |
16 changes: 16 additions & 0 deletions
16
...src/test/resources/console/details/basic/Basic-changeDisplayName-testfeed-unicode.out.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
JUnit Jupiter > Basic > .oO fancy display name Oo. :: STARTED | ||
JUnit Jupiter > Basic > .oO fancy display name Oo. :: SUCCESSFUL | ||
|
||
Test run finished after [\d]+ ms | ||
[ 2 containers found ] | ||
[ 0 containers skipped ] | ||
[ 2 containers started ] | ||
[ 0 containers aborted ] | ||
[ 2 containers successful ] | ||
[ 0 containers failed ] | ||
[ 1 tests found ] | ||
[ 0 tests skipped ] | ||
[ 1 tests started ] | ||
[ 0 tests aborted ] | ||
[ 1 tests successful ] | ||
[ 0 tests failed ] |
16 changes: 16 additions & 0 deletions
16
platform-tests/src/test/resources/console/details/basic/Basic-empty-testfeed-ascii.out.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
JUnit Jupiter > Basic > empty() :: STARTED | ||
JUnit Jupiter > Basic > empty() :: SUCCESSFUL | ||
|
||
Test run finished after [\d]+ ms | ||
[ 2 containers found ] | ||
[ 0 containers skipped ] | ||
[ 2 containers started ] | ||
[ 0 containers aborted ] | ||
[ 2 containers successful ] | ||
[ 0 containers failed ] | ||
[ 1 tests found ] | ||
[ 0 tests skipped ] | ||
[ 1 tests started ] | ||
[ 0 tests aborted ] | ||
[ 1 tests successful ] | ||
[ 0 tests failed ] |
Oops, something went wrong.