Skip to content

[JUnit/TestNG] Always print snippets and summary #1790

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 1 commit into from
Oct 7, 2019
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
4 changes: 2 additions & 2 deletions core/src/main/java/io/cucumber/core/cli/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ public static byte run(String[] argv, ClassLoader classLoader) {

RuntimeOptions runtimeOptions = new CommandlineOptionsParser()
.parse(argv)
.addDefaultFormatterIfNotPresent()
.addDefaultSummaryPrinterIfNotPresent()
.addDefaultFormatterIfAbsent()
.addDefaultSummaryPrinterIfAbsent()
.build(systemOptions);


Expand Down
22 changes: 11 additions & 11 deletions core/src/main/java/io/cucumber/core/options/PluginOption.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package io.cucumber.core.options;

import io.cucumber.plugin.Plugin;
import io.cucumber.plugin.SummaryPrinter;
import io.cucumber.plugin.ConcurrentEventListener;
import io.cucumber.plugin.EventListener;
import io.cucumber.core.exception.CucumberException;
import io.cucumber.core.logging.Logger;
import io.cucumber.core.logging.LoggerFactory;
Expand All @@ -20,6 +16,10 @@
import io.cucumber.core.plugin.TimelineFormatter;
import io.cucumber.core.plugin.UnusedStepsSummaryPrinter;
import io.cucumber.core.plugin.UsageFormatter;
import io.cucumber.plugin.ConcurrentEventListener;
import io.cucumber.plugin.EventListener;
import io.cucumber.plugin.Plugin;
import io.cucumber.plugin.SummaryPrinter;

import java.util.HashMap;
import java.util.regex.Matcher;
Expand All @@ -31,19 +31,19 @@ public class PluginOption implements Options.Plugin {

private static final Pattern PLUGIN_WITH_ARGUMENT_PATTERN = Pattern.compile("([^:]+):(.*)");
private static final HashMap<String, Class<? extends Plugin>> PLUGIN_CLASSES = new HashMap<String, Class<? extends Plugin>>() {{
put("junit", JUnitFormatter.class);
put("testng", TestNGFormatter.class);
put("default_summary", DefaultSummaryPrinter.class);
put("html", HTMLFormatter.class);
put("json", JSONFormatter.class);
put("junit", JUnitFormatter.class);
put("null_summary", NullSummaryPrinter.class);
put("pretty", PrettyFormatter.class);
put("progress", ProgressFormatter.class);
put("json", JSONFormatter.class);
put("usage", UsageFormatter.class);
put("rerun", RerunFormatter.class);
put("summary", DefaultSummaryPrinter.class);
put("default_summary", DefaultSummaryPrinter.class);
put("null_summary", NullSummaryPrinter.class);
put("unused", UnusedStepsSummaryPrinter.class);
put("testng", TestNGFormatter.class);
put("timeline", TimelineFormatter.class);
put("unused", UnusedStepsSummaryPrinter.class);
put("usage", UsageFormatter.class);
}};

// Refuse plugins known to implement the old API
Expand Down
11 changes: 11 additions & 0 deletions core/src/main/java/io/cucumber/core/options/RuntimeOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ public static RuntimeOptions defaultOptions() {
return new RuntimeOptions();
}

void addDefaultFormatterIfAbsent(){
if (formatters.isEmpty()) {
formatters.add(PluginOption.parse("progress"));
}
}
void addDefaultSummaryPrinterIfAbsent(){
if (summaryPrinters.isEmpty()) {
summaryPrinters.add(PluginOption.parse("default_summary"));
}
}

public int getCount() {
return count;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public final class RuntimeOptionsBuilder {
private PickleOrder parsedPickleOrder = null;
private Integer parsedCount = null;
private Class<? extends ObjectFactory> parsedObjectFactoryClass = null;
private boolean addDefaultSummaryPrinterIfAbsent;
private boolean addDefaultFormatterIfAbsent;

public RuntimeOptionsBuilder addRerun(Collection<FeatureWithLines> featureWithLines) {
if (parsedRerunPaths == null) {
Expand Down Expand Up @@ -124,6 +126,14 @@ public RuntimeOptions build(RuntimeOptions runtimeOptions) {
runtimeOptions.setObjectFactoryClass(parsedObjectFactoryClass);
}

if(addDefaultFormatterIfAbsent) {
runtimeOptions.addDefaultFormatterIfAbsent();
}

if(addDefaultSummaryPrinterIfAbsent) {
runtimeOptions.addDefaultSummaryPrinterIfAbsent();
}

return runtimeOptions;
}

Expand Down Expand Up @@ -185,13 +195,13 @@ public RuntimeOptionsBuilder setWip(boolean wip) {
return this;
}

public RuntimeOptionsBuilder addDefaultSummaryPrinterIfNotPresent() {
parsedPluginData.addDefaultSummaryPrinterIfNotPresent();
public RuntimeOptionsBuilder addDefaultSummaryPrinterIfAbsent() {
this.addDefaultSummaryPrinterIfAbsent = true;
return this;
}

public RuntimeOptionsBuilder addDefaultFormatterIfNotPresent() {
parsedPluginData.addDefaultFormatterIfNotPresent();
public RuntimeOptionsBuilder addDefaultFormatterIfAbsent() {
this.addDefaultFormatterIfAbsent = true;
return this;
}

Expand All @@ -214,18 +224,6 @@ void addPluginName(String name, boolean isAddPlugin) {
}
}

void addDefaultSummaryPrinterIfNotPresent() {
if (summaryPrinters.names.isEmpty()) {
addPluginName("summary", false);
}
}

void addDefaultFormatterIfNotPresent() {
if (formatters.names.isEmpty()) {
addPluginName("progress", false);
}
}

void updateFormatters(List<Options.Plugin> formatter) {
this.formatters.updateNameList(formatter);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,23 @@ private void handleSnippetsSuggestedEvent(SnippetsSuggestedEvent event) {
private void print() {
out.println();
printStats();
out.println();
printErrors();
printSnippets();
out.println();
}

private void printStats() {
stats.printStats(out);
out.println();
}

private void printErrors() {
for (Throwable error : stats.getErrors()) {
List<Throwable> errors = stats.getErrors();
if (errors.isEmpty()) {
return;
}
out.println();
for (Throwable error : errors) {
error.printStackTrace(out);
out.println();
}
Expand All @@ -56,6 +62,7 @@ private void printSnippets() {
out.println();
for (String snippet : snippets) {
out.println(snippet);
out.println();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import io.cucumber.core.backend.ObjectFactory;
import io.cucumber.core.exception.CucumberException;
import io.cucumber.plugin.Plugin;
import io.cucumber.core.plugin.PluginFactory;
import io.cucumber.core.plugin.Plugins;
import io.cucumber.core.runtime.TimeServiceEventBus;
import io.cucumber.core.snippets.SnippetType;
import io.cucumber.plugin.Plugin;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;

Expand Down Expand Up @@ -50,8 +50,8 @@ void create_non_strict() {
void create_without_options() {
RuntimeOptions runtimeOptions = parser()
.parse(WithoutOptions.class)
.addDefaultSummaryPrinterIfNotPresent()
.addDefaultFormatterIfNotPresent()
.addDefaultSummaryPrinterIfAbsent()
.addDefaultFormatterIfAbsent()
.build();

assertAll("Checking RuntimeOptions",
Expand Down Expand Up @@ -80,8 +80,8 @@ void create_without_options_with_base_class_without_options() {
Class<?> subClassWithMonoChromeTrueClass = WithoutOptionsWithBaseClassWithoutOptions.class;
RuntimeOptions runtimeOptions = parser()
.parse(subClassWithMonoChromeTrueClass)
.addDefaultFormatterIfNotPresent()
.addDefaultSummaryPrinterIfNotPresent()
.addDefaultFormatterIfAbsent()
.addDefaultSummaryPrinterIfAbsent()
.build();
Plugins plugins = new Plugins(new PluginFactory(), runtimeOptions);
plugins.setEventBusOnEventListenerPlugins(new TimeServiceEventBus(Clock.systemUTC()));
Expand Down Expand Up @@ -140,7 +140,7 @@ private String getRegexpPattern(Object pattern) {
void create_default_summary_printer_when_no_summary_printer_plugin_is_defined() {
RuntimeOptions runtimeOptions = parser()
.parse(ClassWithNoSummaryPrinterPlugin.class)
.addDefaultSummaryPrinterIfNotPresent()
.addDefaultSummaryPrinterIfAbsent()
.build();
Plugins plugins = new Plugins(new PluginFactory(), runtimeOptions);
plugins.setEventBusOnEventListenerPlugins(new TimeServiceEventBus(Clock.systemUTC()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ void creates_html_formatter() {
void creates_progress_formatter_as_default() {
RuntimeOptions options = new CommandlineOptionsParser()
.parse()
.addDefaultFormatterIfNotPresent()
.addDefaultFormatterIfAbsent()
.build();
Plugins plugins = new Plugins(new PluginFactory(), options);
plugins.setEventBusOnEventListenerPlugins(new TimeServiceEventBus(Clock.systemUTC()));
Expand All @@ -177,7 +177,7 @@ void creates_progress_formatter_as_default() {
void creates_default_summary_printer_when_no_summary_printer_plugin_is_specified() {
RuntimeOptions options = new CommandlineOptionsParser()
.parse("--plugin", "pretty")
.addDefaultSummaryPrinterIfNotPresent()
.addDefaultSummaryPrinterIfAbsent()
.build();
Plugins plugins = new Plugins(new PluginFactory(), options);
plugins.setEventBusOnEventListenerPlugins(new TimeServiceEventBus(Clock.systemUTC()));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.cucumber.examples.testng;

import io.cucumber.examples.testng.RpnCalculator;
import io.cucumber.java.DataTableType;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
Expand Down
2 changes: 1 addition & 1 deletion java/src/main/java/io/cucumber/java/JavaSnippet.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ public MessageFormat template() {
"public void {2}({3}) '{'\n" +
" // {4}\n" +
"{5} throw new " + PendingException.class.getName() + "();\n" +
"'}'\n");
"'}'");
}
}
2 changes: 1 addition & 1 deletion java8/src/main/java/io/cucumber/java8/Java8Snippet.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ public MessageFormat template() {
"{0}(\"{1}\", ({3}) -> '{'\n" +
" // {4}\n" +
"{5} throw new " + PendingException.class.getName() + "();\n" +
"'}');\n");
"'}');");
}
}
1 change: 1 addition & 0 deletions junit/src/main/java/io/cucumber/junit/Cucumber.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public Cucumber(Class clazz) throws InitializationError {

RuntimeOptions runtimeOptions = new CucumberPropertiesParser()
.parse(CucumberProperties.fromSystemProperties())
.addDefaultSummaryPrinterIfAbsent()
.build(environmentOptions);

// Next parse the junit options
Expand Down
24 changes: 5 additions & 19 deletions testng/src/main/java/io/cucumber/testng/TestNGCucumberRunner.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public TestNGCucumberRunner(Class clazz) {

runtimeOptions = new CucumberPropertiesParser()
.parse(CucumberProperties.fromSystemProperties())
.addDefaultSummaryPrinterIfAbsent()
.build(environmentOptions);

ClassLoader classLoader = clazz.getClassLoader();
Expand All @@ -111,26 +112,11 @@ public void runScenario(Pickle pickle) throws Throwable {
runner.runPickle(cucumberPickle);
testCaseResultListener.finishExecutionUnit();

if (testCaseResultListener.isPassed()) {
return;
if (!testCaseResultListener.isPassed()) {
// null pointer is covered by isPassed
// noinspection ConstantConditions
throw testCaseResultListener.getError();
}

// Log the reason we skipped the test. TestNG doesn't provide it by
// default
Throwable error = testCaseResultListener.getError();
if (error instanceof SkipException) {
SkipException skipException = (SkipException) error;
if (skipException.isSkip()) {
System.out.println(format("Skipped scenario: '%s'. %s",
cucumberPickle.getName(),
skipException.getMessage()
));
}
}

// null pointer is covered by isPassed
// noinspection ConstantConditions
throw error;
}

public void finish() {
Expand Down