Skip to content

Commit

Permalink
Fixes fabriciorby#59 - Include hide-succes-tests option
Browse files Browse the repository at this point in the history
  • Loading branch information
rsantirso committed Nov 18, 2024
1 parent dbab2d4 commit d547eb3
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 3 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ The output can be printed using two Themes: UNICODE and ASCII (by default).
![Imgur](https://i.imgur.com/FzcIWwe.png "ASCII Output")


## Reduce verbosity

Output in large projects could become too verbose, making failures or errors difficult to find inside the printed trees.
To reduce verbosity, you can a _hide successful results_ option can be configured as follows:

```xml
<statelessTestsetInfoReporter
implementation="org.apache.maven.plugin.surefire.extensions.junit5.JUnit5StatelessTestsetInfoTreeReporter">
<hideResultsOnSuccess>true</hideResultsOnSuccess>
</statelessTestsetInfoReporter>
```

## Failure details

By default, `<consoleOutputReporter><disable>true</disable></consoleOutputReporter>` disables all console output. To debug test failures, it may be useful to see the console output and stack traces when a test fails. To do so, you can configure this extension like this:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class JUnit5StatelessTestsetInfoTreeReporter extends JUnit5StatelessTests
private boolean printStdoutOnError;
private boolean printStdoutOnFailure;
private boolean printStdoutOnSuccess;
private boolean hideResultsOnSuccess;
private Theme theme = Theme.ASCII;

@Override
Expand All @@ -48,6 +49,7 @@ public Object clone(ClassLoader target) {
cls.getMethod("setPrintStdoutOnError", boolean.class).invoke(clone, isPrintStdoutOnError());
cls.getMethod("setPrintStdoutOnFailure", boolean.class).invoke(clone, isPrintStdoutOnFailure());
cls.getMethod("setPrintStdoutOnSuccess", boolean.class).invoke(clone, isPrintStdoutOnSuccess());
cls.getMethod("setHideResultsOnSuccess", boolean.class).invoke(clone, isPrintStdoutOnSuccess());
cls.getMethod("setTheme", themeClass).invoke(clone, clonedTheme);

return clone;
Expand Down Expand Up @@ -98,6 +100,10 @@ public boolean isPrintStdoutOnSuccess() {
return printStdoutOnSuccess;
}

public boolean isHideResultsOnSuccess() {
return hideResultsOnSuccess;
}

public void setPrintStacktraceOnError(boolean printStacktraceOnError) {
this.printStacktraceOnError = printStacktraceOnError;
}
Expand Down Expand Up @@ -130,6 +136,10 @@ public void setPrintStdoutOnSuccess(boolean printStdoutOnSuccess) {
this.printStdoutOnSuccess = printStdoutOnSuccess;
}

public void setHideResultsOnSuccess(boolean hideResultsOnSuccess) {
this.hideResultsOnSuccess = hideResultsOnSuccess;
}

public void setTheme(Theme theme) {
this.theme = theme;
}
Expand All @@ -154,6 +164,7 @@ private ReporterOptions newReporterOptions() {
.printStdoutOnError(isPrintStdoutOnError())
.printStdoutOnFailure(isPrintStdoutOnFailure())
.printStdoutOnSuccess(isPrintStdoutOnSuccess())
.hideResultsOnSuccess(isHideResultsOnSuccess())
.usePhrasedClassNameInRunning(isUsePhrasedClassNameInRunning())
.usePhrasedClassNameInTestCaseSummary(isUsePhrasedClassNameInTestCaseSummary())
.theme(getTheme())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class ReporterOptions {
private final boolean printStdoutOnError;
private final boolean printStdoutOnFailure;
private final boolean printStdoutOnSuccess;
private final boolean hideResultsOnSuccess;
private final Theme theme;
private final boolean usePhrasedClassNameInRunning;
private final boolean usePhrasedClassNameInTestCaseSummary;
Expand All @@ -22,6 +23,7 @@ private ReporterOptions(Builder builder) {
this.printStdoutOnError = builder.printStdoutOnError;
this.printStdoutOnFailure = builder.printStdoutOnFailure;
this.printStdoutOnSuccess = builder.printStdoutOnSuccess;
this.hideResultsOnSuccess = builder.hideResultsOnSuccess;
this.usePhrasedClassNameInRunning = builder.usePhrasedClassNameInRunning;
this.usePhrasedClassNameInTestCaseSummary = builder.usePhrasedClassNameInTestCaseSummary;
this.theme = builder.theme != null ? builder.theme : Theme.ASCII;
Expand Down Expand Up @@ -63,6 +65,10 @@ public boolean isPrintStdoutOnSuccess() {
return printStdoutOnSuccess;
}

public boolean isHIdeResultsOnSuccess() {
return hideResultsOnSuccess;
}

public boolean isUsePhrasedClassNameInRunning() {
return usePhrasedClassNameInRunning;
}
Expand All @@ -84,6 +90,7 @@ public static final class Builder {
private boolean printStdoutOnError;
private boolean printStdoutOnFailure;
private boolean printStdoutOnSuccess;
private boolean hideResultsOnSuccess;
private Theme theme;
private boolean usePhrasedClassNameInRunning;
private boolean usePhrasedClassNameInTestCaseSummary;
Expand Down Expand Up @@ -135,6 +142,11 @@ public Builder printStdoutOnSuccess(boolean printStdoutOnSuccess) {
return this;
}

public Builder hideResultsOnSuccess(boolean hideResultsOnSuccess) {
this.hideResultsOnSuccess = hideResultsOnSuccess;
return this;
}

public Builder theme(Theme theme) {
this.theme = theme;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,15 @@ public void printTests() {
.stream()
.map(TestPrinter::new)
.forEach(printer -> {
printer.printTest();
printer.printTest(isSuccessPrintAllowed());
printer.printDetails();
});
}

private boolean isSuccessPrintAllowed() {
return !options.isHIdeResultsOnSuccess();
}

private class TestPrinter {

private final WrappedReportEntry testResult;
Expand Down Expand Up @@ -123,13 +127,13 @@ private void printDetails() {
}
}

private void printTest() {
private void printTest(boolean isPrintSuccessAllowed) {
printClass();
if (testResult.isErrorOrFailure()) {
printFailure();
} else if (testResult.isSkipped()) {
printSkipped();
} else if (testResult.isSucceeded()) {
} else if (isPrintSuccessAllowed && testResult.isSucceeded()) {
printSuccess();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.codehaus.plexus.PlexusContainerException;
import org.codehaus.plexus.logging.Logger;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;
Expand Down Expand Up @@ -90,4 +91,53 @@ void testSetCompleted() {
//TODO see how to unit test this
}

@Test
void testHideSuccess() {
//TestStarting parameters
SimpleReportEntry simpleReportEntry1 = new SimpleReportEntry(RunMode.NORMAL_RUN, 123L, "NestedExampleTest", "Nested Sample", null, null);
SimpleReportEntry simpleReportEntry2 = new SimpleReportEntry(RunMode.NORMAL_RUN, 123L, "NestedExampleTest$InnerTest", "Inner Test", null, null);
SimpleReportEntry simpleReportEntry3 = new SimpleReportEntry(RunMode.NORMAL_RUN, 123L, "NestedExampleTest$InnerTest$InnerInnerTest", "Inner Inner Test", null, null);
SimpleReportEntry simpleReportEntry4 = new SimpleReportEntry(RunMode.NORMAL_RUN, 123L, "NestedExampleTest$InnerTest$InnerInnerTest$InnerInnerInnerTest", "Inner Inner Inner Test", null, null);

SimpleReportEntry firstTest = new SimpleReportEntry(RunMode.NORMAL_RUN, 123L, "NestedExampleTest", "Nested Sample", "test", "Should not be displayed");
WrappedReportEntry wrappedReportEntry1 = new WrappedReportEntry(firstTest, ReportEntryType.SUCCESS, 1, stdout, stderr);

SimpleReportEntry secondTest = new SimpleReportEntry(RunMode.NORMAL_RUN, 123L, "NestedExampleTest", "Nested Sample", "test2", "Should not be displayed");
WrappedReportEntry wrappedReportEntry2 = new WrappedReportEntry(secondTest, ReportEntryType.SUCCESS, 1, stdout, stderr);

SimpleReportEntry thirdTest = new SimpleReportEntry(RunMode.NORMAL_RUN, 123L, "NestedExampleTest$InnerTest", "Inner Test", "test", "Inner failure test should be displayed");
WrappedReportEntry wrappedReportEntry3 = new WrappedReportEntry(thirdTest, ReportEntryType.FAILURE, 1, stdout, stderr);

SimpleReportEntry fourthTest = new SimpleReportEntry(RunMode.NORMAL_RUN, 123L, "NestedExampleTest$InnerTest$InnerInnerTest", "Inner Inner Test", "test", "Inner Inner error test should be displayed");
WrappedReportEntry wrappedReportEntry4 = new WrappedReportEntry(fourthTest, ReportEntryType.ERROR, 1, stdout, stderr);

SimpleReportEntry fifthTest = new SimpleReportEntry(RunMode.NORMAL_RUN, 123L, "NestedExampleTest$InnerTest$InnerInnerTest$InnerInnerInnerTest", "Inner Inner Inner Test", "test", "Inner Inner Inner skipped test should be displayed");
WrappedReportEntry wrappedReportEntry5 = new WrappedReportEntry(fifthTest, ReportEntryType.SKIPPED, 1, stdout, stderr);

SimpleReportEntry sixthTest = new SimpleReportEntry(RunMode.NORMAL_RUN, 123L, "NestedExampleTest$FirstInnerTest", "First Inner Test", "test", "FirstInnerTest should not be displayed");
WrappedReportEntry wrappedReportEntry6 = new WrappedReportEntry(sixthTest, ReportEntryType.SUCCESS, 1, stdout, stderr);

TestSetStats testSetStats = new TestSetStats(false, true);
testSetStats.testSucceeded(wrappedReportEntry1);
testSetStats.testSucceeded(wrappedReportEntry2);
testSetStats.testFailure(wrappedReportEntry3);
testSetStats.testError(wrappedReportEntry4);
testSetStats.testSkipped(wrappedReportEntry5);
testSetStats.testSucceeded(wrappedReportEntry6);

TestSetStats testSetStatsForClass = new TestSetStats(false, true);

ConsoleTreeReporter consoleTreeReporter = new ConsoleTreeReporter(new PluginConsoleLogger(logger), ReporterOptions.builder().build());
consoleTreeReporter.testSetStarting(simpleReportEntry1);
consoleTreeReporter.testSetStarting(simpleReportEntry2);
consoleTreeReporter.testSetStarting(simpleReportEntry3);
consoleTreeReporter.testSetStarting(simpleReportEntry4);
consoleTreeReporter.testSetCompleted(wrappedReportEntry5, testSetStats, null);
consoleTreeReporter.testSetCompleted(wrappedReportEntry4, testSetStatsForClass, null);
consoleTreeReporter.testSetCompleted(wrappedReportEntry3, testSetStatsForClass, null);
consoleTreeReporter.testSetCompleted(wrappedReportEntry2, testSetStatsForClass, null);

}


}

0 comments on commit d547eb3

Please sign in to comment.