Skip to content

Commit

Permalink
Console runner has new option --hide-details
Browse files Browse the repository at this point in the history
  • Loading branch information
jlink committed Nov 20, 2015
1 parent 48b0380 commit b548280
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 18 deletions.
Expand Up @@ -56,6 +56,9 @@ public class ConsoleRunner {
@Option(name = {"-a", "--all"}, description = "Run all tests")
private boolean runAllTests;

@Option(name = {"-D", "--hide-details"}, description = "Hide details while tests are being executed")
private boolean hideDetails;

@Arguments(description = "Test classes, methods or packages to execute (ignore if --all|-a has been chosen)")
private List<String> arguments;

Expand Down Expand Up @@ -109,12 +112,10 @@ private void run() {

private void registerListeners(Launcher launcher, TestExecutionSummary summary) {
SummaryCreatingTestListener testSummaryListener = new SummaryCreatingTestListener(summary);
launcher.registerTestPlanExecutionListeners(
// @formatter:off
new ColoredPrintingTestListener(System.out, disableAnsiColors),
testSummaryListener
// @formatter:on
);
launcher.registerTestPlanExecutionListeners(testSummaryListener);
if (!hideDetails) {
launcher.registerTestPlanExecutionListeners(new ColoredPrintingTestListener(System.out, disableAnsiColors));
}
}

private TestPlanSpecification createTestPlanSpecification() {
Expand All @@ -130,9 +131,14 @@ private TestPlanSpecification createTestPlanSpecification() {
}

private void printSummaryToStandardOut(TestExecutionSummary summary) {
PrintWriter soutWriter = new PrintWriter(System.out);
summary.printOn(soutWriter);
soutWriter.close();
PrintWriter stdout = new PrintWriter(System.out);

if (hideDetails) { //Otherwise the failures have already been printed
summary.printFailuresOn(stdout);
}

summary.printOn(stdout);
stdout.close();
}

private List<TestPlanSpecificationElement> testPlanSpecificationElementsFromArguments() {
Expand Down
Expand Up @@ -13,16 +13,15 @@
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.StringJoiner;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors;

import lombok.Value;

import org.junit.gen5.engine.EngineDescriptor;
import org.junit.gen5.engine.TestDescriptor;
import org.junit.gen5.launcher.TestPlan;

// TODO Give it a REAL interface and make it threadsafe
public class TestExecutionSummary {
Expand Down Expand Up @@ -61,13 +60,44 @@ public void printOn(PrintWriter writer) {
this.testsSkipped.get(), this.testsAborted.get(), this.testsSucceeded.get(), this.testsFailed.get()));
// @formatter:on

// if (countFailedTests() > 0) {
// writer.println("Test failures: ");
// }
writer.flush();
}

public void printFailuresOn(PrintWriter writer) {

if (countFailedTests() > 0) {
writer.println();
writer.println(String.format("Test failures (%d):", testsFailed.get()));
failures.stream().forEach(failure -> {
//TODO Add source description to text
writer.println(String.format(" %s", describeTest(failure.getDescriptor())));
writer.println(String.format(" => Exception: %s", failure.getException().getLocalizedMessage()));
});
}

writer.flush();
}

private String describeTest(TestDescriptor descriptor) {
List<String> descriptionParts = new ArrayList<>();
collectTestDescription(Optional.of(descriptor), descriptionParts);
return descriptionParts.stream().collect(Collectors.joining(":"));
}

private void collectTestDescription(Optional<TestDescriptor> optionalDescriptor, List<String> descriptionParts) {
optionalDescriptor.ifPresent(descriptor -> {
if (descriptor instanceof TestPlan) {
}
else if (descriptor instanceof EngineDescriptor) {
descriptionParts.add(0, descriptor.getUniqueId());
}
else {
descriptionParts.add(0, descriptor.getDisplayName());
}
collectTestDescription(descriptor.getParent(), descriptionParts);
});
}

public long countFailedTests() {
return testsFailed.get();
}
Expand Down
2 changes: 1 addition & 1 deletion sample-project/build.gradle
Expand Up @@ -13,7 +13,7 @@ task runSampleTestCases(type: JavaExec) {
classpath = sourceSets.test.runtimeClasspath
classpath += project(':junit-console').sourceSets.main.runtimeClasspath
main = 'org.junit.gen5.console.ConsoleRunner'
args '--all' // 'com.example'
args '--all', '--hide-details' // 'com.example'
}

test {
Expand Down

0 comments on commit b548280

Please sign in to comment.