From 74d94caaa4a1c8bd535884d47fd5dc84f9d4d563 Mon Sep 17 00:00:00 2001 From: dustin Date: Tue, 12 Mar 2024 21:57:13 -0400 Subject: [PATCH 1/2] feat: add Karate runner argument ignore no scenarios assertion If JUnit 5 is used to run a Karate feature file, but no scenarios are found that match the run criteria, this error is thrown: no features or scenarios found: [classpath:karate/features/helloworld/] For example, if Karate is run with these arguments and this feature file, the error will be thrown. -Dkarate.options="--tags @smoke" Feature: Hello World @prod Scenario: Hello to the world Refs: #2531 --- .../main/java/com/intuit/karate/Runner.java | 10 +++++++-- .../main/java/com/intuit/karate/Suite.java | 19 +++++++--------- .../java/com/intuit/karate/junit5/Karate.java | 3 +-- .../java/karate/NoFeatureNoScenarioTest.java | 22 +++++++++++++++++++ .../java/karate/noFeatureNoScenario.feature | 5 +++++ 5 files changed, 44 insertions(+), 15 deletions(-) create mode 100644 karate-junit5/src/test/java/karate/NoFeatureNoScenarioTest.java create mode 100644 karate-junit5/src/test/java/karate/noFeatureNoScenario.feature diff --git a/karate-core/src/main/java/com/intuit/karate/Runner.java b/karate-core/src/main/java/com/intuit/karate/Runner.java index a8b4fc72d..0a635d151 100644 --- a/karate-core/src/main/java/com/intuit/karate/Runner.java +++ b/karate-core/src/main/java/com/intuit/karate/Runner.java @@ -34,10 +34,11 @@ import com.intuit.karate.http.HttpClientFactory; import com.intuit.karate.report.SuiteReports; import com.intuit.karate.resource.ResourceUtils; +import org.slf4j.LoggerFactory; + import java.io.File; import java.util.*; import java.util.stream.Collectors; -import org.slf4j.LoggerFactory; /** * @@ -112,6 +113,7 @@ public static class Builder { boolean outputCucumberJson; boolean dryRun; boolean debugMode; + boolean ignoreJunitNoScenariosAssertion; Map systemProperties; Map callSingleCache; Map callOnceCache; @@ -145,6 +147,7 @@ public synchronized Builder copy() { b.outputCucumberJson = outputCucumberJson; b.dryRun = dryRun; b.debugMode = debugMode; + b.ignoreJunitNoScenariosAssertion = ignoreJunitNoScenariosAssertion; b.systemProperties = systemProperties; b.callSingleCache = callSingleCache; b.callOnceCache = callOnceCache; @@ -436,7 +439,10 @@ public T debugMode(boolean value) { debugMode = value; return (T) this; } - + public T ignoreJunitNoScenariosAssertion(boolean value) { + ignoreJunitNoScenariosAssertion = value; + return (T) this; + } public T callSingleCache(Map value) { callSingleCache = value; return (T) this; diff --git a/karate-core/src/main/java/com/intuit/karate/Suite.java b/karate-core/src/main/java/com/intuit/karate/Suite.java index eb1aa5b4b..65272787f 100644 --- a/karate-core/src/main/java/com/intuit/karate/Suite.java +++ b/karate-core/src/main/java/com/intuit/karate/Suite.java @@ -28,8 +28,6 @@ import com.intuit.karate.core.FeatureCall; import com.intuit.karate.core.FeatureResult; import com.intuit.karate.core.FeatureRuntime; -import com.intuit.karate.driver.DriverRunner; -import com.intuit.karate.report.ReportUtils; import com.intuit.karate.core.Scenario; import com.intuit.karate.core.ScenarioCall; import com.intuit.karate.core.ScenarioResult; @@ -37,27 +35,23 @@ import com.intuit.karate.core.Step; import com.intuit.karate.core.SyncExecutorService; import com.intuit.karate.core.Tags; +import com.intuit.karate.driver.DriverRunner; import com.intuit.karate.http.HttpClientFactory; +import com.intuit.karate.report.ReportUtils; import com.intuit.karate.report.SuiteReports; import com.intuit.karate.resource.Resource; import com.intuit.karate.resource.ResourceUtils; +import org.slf4j.LoggerFactory; + import java.io.File; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; -import java.util.concurrent.locks.ReentrantLock; import java.util.stream.Collectors; import java.util.stream.Stream; -import org.slf4j.LoggerFactory; import static java.util.function.Predicate.not; @@ -78,6 +72,7 @@ public class Suite implements Runnable { public final String tagSelector; public final boolean dryRun; public final boolean debugMode; + public final boolean ignoreJunitNoScenariosAssertion; public final File workingDir; public final String buildDir; public final String reportDir; @@ -135,6 +130,7 @@ public Suite(Runner.Builder rb) { if (rb.forTempUse) { dryRun = false; debugMode = false; + ignoreJunitNoScenariosAssertion = false; backupReportDir = false; outputHtmlReport = false; outputCucumberJson = false; @@ -179,6 +175,7 @@ public Suite(Runner.Builder rb) { outputJunitXml = rb.outputJunitXml; dryRun = rb.dryRun; debugMode = rb.debugMode; + ignoreJunitNoScenariosAssertion = rb.ignoreJunitNoScenariosAssertion; classLoader = rb.classLoader; clientFactory = rb.clientFactory; env = rb.env; diff --git a/karate-junit5/src/main/java/com/intuit/karate/junit5/Karate.java b/karate-junit5/src/main/java/com/intuit/karate/junit5/Karate.java index 85475215a..26ad0efab 100644 --- a/karate-junit5/src/main/java/com/intuit/karate/junit5/Karate.java +++ b/karate-junit5/src/main/java/com/intuit/karate/junit5/Karate.java @@ -25,7 +25,6 @@ import com.intuit.karate.Runner; import com.intuit.karate.Suite; -import com.intuit.karate.core.Feature; import com.intuit.karate.core.FeatureCall; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.DynamicContainer; @@ -68,7 +67,7 @@ public Iterator iterator() { DynamicNode node = DynamicContainer.dynamicContainer(testName, featureNode); list.add(node); } - if (list.isEmpty()) { + if (!suite.ignoreJunitNoScenariosAssertion && list.isEmpty()) { Assertions.fail("no features or scenarios found: " + this); } return list.iterator(); diff --git a/karate-junit5/src/test/java/karate/NoFeatureNoScenarioTest.java b/karate-junit5/src/test/java/karate/NoFeatureNoScenarioTest.java new file mode 100644 index 000000000..3563bff45 --- /dev/null +++ b/karate-junit5/src/test/java/karate/NoFeatureNoScenarioTest.java @@ -0,0 +1,22 @@ +package karate; + +import com.intuit.karate.junit5.Karate; + +class NoFeatureNoScenarioTest { + + @Karate.Test + Karate testValidTagWithIgnoreJunitNoScenarioAssertion() { + return Karate.run("noFeatureNoScenario") + .tags("@smoke") + .ignoreJunitNoScenariosAssertion(true) + .relativeTo(getClass()); + } + @Karate.Test + Karate testInvalidTagWithIgnoreJunitNoScenarioAssertion() { + return Karate.run("noFeatureNoScenario") + .tags("@tagnotexist") + .ignoreJunitNoScenariosAssertion(true) + .relativeTo(getClass()); + } + +} diff --git a/karate-junit5/src/test/java/karate/noFeatureNoScenario.feature b/karate-junit5/src/test/java/karate/noFeatureNoScenario.feature new file mode 100644 index 000000000..b94c0acfa --- /dev/null +++ b/karate-junit5/src/test/java/karate/noFeatureNoScenario.feature @@ -0,0 +1,5 @@ +Feature: ignoreJunitNoScenariosAssertion argument for Karate runner + + @smoke + Scenario: smoke + * print 'smoke' From d089d81b3e95cb08ee807f8cdd0f6954ef922568 Mon Sep 17 00:00:00 2001 From: dustin Date: Wed, 13 Mar 2024 09:10:53 -0400 Subject: [PATCH 2/2] refactor: renamed runner field per Peter's comments Renamed ignoreJunitNoScenariosAssertion to failWhenNoScenariosFound. Refs: #2531 --- .../src/main/java/com/intuit/karate/Runner.java | 10 ++++++---- karate-core/src/main/java/com/intuit/karate/Suite.java | 6 +++--- .../src/main/java/com/intuit/karate/junit5/Karate.java | 2 +- .../src/test/java/karate/NoFeatureNoScenarioTest.java | 10 +++++----- 4 files changed, 15 insertions(+), 13 deletions(-) diff --git a/karate-core/src/main/java/com/intuit/karate/Runner.java b/karate-core/src/main/java/com/intuit/karate/Runner.java index 0a635d151..0dd4418d9 100644 --- a/karate-core/src/main/java/com/intuit/karate/Runner.java +++ b/karate-core/src/main/java/com/intuit/karate/Runner.java @@ -113,7 +113,7 @@ public static class Builder { boolean outputCucumberJson; boolean dryRun; boolean debugMode; - boolean ignoreJunitNoScenariosAssertion; + boolean failWhenNoScenariosFound; Map systemProperties; Map callSingleCache; Map callOnceCache; @@ -147,7 +147,7 @@ public synchronized Builder copy() { b.outputCucumberJson = outputCucumberJson; b.dryRun = dryRun; b.debugMode = debugMode; - b.ignoreJunitNoScenariosAssertion = ignoreJunitNoScenariosAssertion; + b.failWhenNoScenariosFound = failWhenNoScenariosFound; b.systemProperties = systemProperties; b.callSingleCache = callSingleCache; b.callOnceCache = callOnceCache; @@ -439,10 +439,12 @@ public T debugMode(boolean value) { debugMode = value; return (T) this; } - public T ignoreJunitNoScenariosAssertion(boolean value) { - ignoreJunitNoScenariosAssertion = value; + + public T failWhenNoScenariosFound(boolean value) { + failWhenNoScenariosFound = value; return (T) this; } + public T callSingleCache(Map value) { callSingleCache = value; return (T) this; diff --git a/karate-core/src/main/java/com/intuit/karate/Suite.java b/karate-core/src/main/java/com/intuit/karate/Suite.java index 65272787f..265112d77 100644 --- a/karate-core/src/main/java/com/intuit/karate/Suite.java +++ b/karate-core/src/main/java/com/intuit/karate/Suite.java @@ -72,7 +72,7 @@ public class Suite implements Runnable { public final String tagSelector; public final boolean dryRun; public final boolean debugMode; - public final boolean ignoreJunitNoScenariosAssertion; + public final boolean failWhenNoScenariosFound; public final File workingDir; public final String buildDir; public final String reportDir; @@ -130,7 +130,7 @@ public Suite(Runner.Builder rb) { if (rb.forTempUse) { dryRun = false; debugMode = false; - ignoreJunitNoScenariosAssertion = false; + failWhenNoScenariosFound = false; backupReportDir = false; outputHtmlReport = false; outputCucumberJson = false; @@ -175,7 +175,7 @@ public Suite(Runner.Builder rb) { outputJunitXml = rb.outputJunitXml; dryRun = rb.dryRun; debugMode = rb.debugMode; - ignoreJunitNoScenariosAssertion = rb.ignoreJunitNoScenariosAssertion; + failWhenNoScenariosFound = rb.failWhenNoScenariosFound; classLoader = rb.classLoader; clientFactory = rb.clientFactory; env = rb.env; diff --git a/karate-junit5/src/main/java/com/intuit/karate/junit5/Karate.java b/karate-junit5/src/main/java/com/intuit/karate/junit5/Karate.java index 26ad0efab..0d5ad194b 100644 --- a/karate-junit5/src/main/java/com/intuit/karate/junit5/Karate.java +++ b/karate-junit5/src/main/java/com/intuit/karate/junit5/Karate.java @@ -67,7 +67,7 @@ public Iterator iterator() { DynamicNode node = DynamicContainer.dynamicContainer(testName, featureNode); list.add(node); } - if (!suite.ignoreJunitNoScenariosAssertion && list.isEmpty()) { + if (suite.failWhenNoScenariosFound && list.isEmpty()) { Assertions.fail("no features or scenarios found: " + this); } return list.iterator(); diff --git a/karate-junit5/src/test/java/karate/NoFeatureNoScenarioTest.java b/karate-junit5/src/test/java/karate/NoFeatureNoScenarioTest.java index 3563bff45..78bf7a015 100644 --- a/karate-junit5/src/test/java/karate/NoFeatureNoScenarioTest.java +++ b/karate-junit5/src/test/java/karate/NoFeatureNoScenarioTest.java @@ -5,18 +5,18 @@ class NoFeatureNoScenarioTest { @Karate.Test - Karate testValidTagWithIgnoreJunitNoScenarioAssertion() { + Karate testHasScenariosWithFailWhenNoScenariosFound() { return Karate.run("noFeatureNoScenario") .tags("@smoke") - .ignoreJunitNoScenariosAssertion(true) + .failWhenNoScenariosFound(true) .relativeTo(getClass()); } + @Karate.Test - Karate testInvalidTagWithIgnoreJunitNoScenarioAssertion() { + Karate testNoScenarios() { return Karate.run("noFeatureNoScenario") .tags("@tagnotexist") - .ignoreJunitNoScenariosAssertion(true) + .failWhenNoScenariosFound(false) .relativeTo(getClass()); } - }