From e9a9bb31394be7b6d5a66d186c14900edfe3eea9 Mon Sep 17 00:00:00 2001 From: "M.P. Korstanje" Date: Thu, 27 Mar 2025 14:31:34 +0100 Subject: [PATCH] Update documentation on running single scenarios with Gradle --- README.md | 59 +++++++++++++++---- gradle/build.gradle.kts | 7 +++ .../io/cucumber/skeleton/RunCucumberTest.java | 2 - .../test/resources/junit-platform.properties | 4 +- .../io/cucumber/skeleton/RunCucumberTest.java | 2 - .../test/resources/junit-platform.properties | 4 +- 6 files changed, 57 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 4bc477c5..095e6c67 100644 --- a/README.md +++ b/README.md @@ -56,12 +56,8 @@ For available parameters see: `io.cucumber.junit.platform.engine.Constants` ## Run a subset of Features or Scenarios -Specify a particular scenario by *line* - - @SelectClasspathResource(value = "io/cucumber/skeleton/belly.feature", line = 3) - -In case you have multiple feature files or scenarios to run against repeat the -annotation. +### By Tag +#### With the JUnit Suite Engine You can also specify what to run by *tag*. @@ -80,26 +76,51 @@ Then add an annotation to `RunCucumberTest`. @IncludeTags("Zucchini") ``` +#### With Maven + When using Maven, tags can be selected from the CLI using the `groups` and `excludedGroups` parameters. These take a [JUnit5 Tag Expression](https://junit.org/junit5/docs/current/user-guide/#running-tests-tag-expressions). Note: When using JUnit, the `@` is not part of the tag. -``` +```shell mvn verify -DexcludedGroups="Haricots" -Dgroups="Zucchini | Gherkin" ``` -### Running a single scenario or feature +#### With Gradle + +When using Gradle, tags can be selected through the [build configuration](https://docs.gradle.org/current/userguide/java_testing.html#test_grouping). These take a [JUnit5 Tag Expression](https://junit.org/junit5/docs/current/user-guide/#running-tests-tag-expressions). +Note: When using JUnit, the `@` is not part of the tag. + +```kotlin +tasks.withType().configureEach { + useJUnitPlatform { + includeTags("Zucchini | Gherkin") + excludeTags("Haricots") + } +} +``` + +### By line + +#### With the JUnit Suite Engine + +Specify a particular scenario by *line* -Maven and Gradle do not (yet) support selecting single features or scenarios + @SelectClasspathResource(value = "io/cucumber/skeleton/belly.feature", line = 3) + +In case you have multiple feature files or scenarios to run against repeat the +annotation. + +#### With Maven + +Maven does not (yet) support selecting single features or scenarios with JUnit selectors. As a work around the `cucumber.features` property can be used. Because this property will cause Cucumber to ignore any other selectors from JUnit it is prudent to only execute the Cucumber engine. -#### With Maven - To select the scenario on line 3 of the `belly.feature` file use: -``` +```shell ./mvnw test -Dsurefire.includeJUnit5Engines=cucumber -Dcucumber.features=src/test/resources/io/cucumber/skeleton/belly.feature:3 ``` @@ -107,4 +128,16 @@ Note: Add `-Dcucumber.plugin=pretty` to get a more detailed output during test e #### With Gradle -TODO: (I don't know how to do this. Feel free to send a pull request. ;)) +Gradle does not (yet) support selecting single features or scenarios +with JUnit selectors. As a work around the `cucumber.features` property can be +used. Because this property will cause Cucumber to ignore any other selectors +from JUnit it is prudent to only execute the Cucumber engine. + +```shell +./gradlew test --rerun-tasks --info -Dcucumber.features=src/test/resources/io/cucumber/skeleton/belly.feature:3 +``` + +Note: Add `-Dcucumber.plugin=pretty` to get a more detailed output during test execution. +Note: Because both the Suite Engine and the Cucumber Engine are included, this + will run tests twice. (If you know how to prevent this, please send a pull + request). \ No newline at end of file diff --git a/gradle/build.gradle.kts b/gradle/build.gradle.kts index d84332e5..04c6069b 100644 --- a/gradle/build.gradle.kts +++ b/gradle/build.gradle.kts @@ -20,6 +20,13 @@ repositories { tasks.withType { useJUnitPlatform() + + // Pass selected system properties to Cucumber + systemProperty("cucumber.features", System.getProperty("cucumber.features")) + systemProperty("cucumber.filter.tags", System.getProperty("cucumber.filter.tags")) + systemProperty("cucumber.filter.name", System.getProperty("cucumber.filter.name")) + systemProperty("cucumber.plugin", System.getProperty("cucumber.plugin")) + // Work around. Gradle does not include enough information to disambiguate // between different examples and scenarios. systemProperty("cucumber.junit-platform.naming-strategy", "long") diff --git a/gradle/src/test/java/io/cucumber/skeleton/RunCucumberTest.java b/gradle/src/test/java/io/cucumber/skeleton/RunCucumberTest.java index f2ae9119..321df1ff 100644 --- a/gradle/src/test/java/io/cucumber/skeleton/RunCucumberTest.java +++ b/gradle/src/test/java/io/cucumber/skeleton/RunCucumberTest.java @@ -6,12 +6,10 @@ import org.junit.platform.suite.api.Suite; import static io.cucumber.junit.platform.engine.Constants.PLUGIN_PROPERTY_NAME; -import static io.cucumber.junit.platform.engine.Constants.GLUE_PROPERTY_NAME; @Suite @IncludeEngines("cucumber") @SelectPackages("io.cucumber.skeleton") @ConfigurationParameter(key = PLUGIN_PROPERTY_NAME, value = "pretty") -@ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "io.cucumber.skeleton") public class RunCucumberTest { } diff --git a/gradle/src/test/resources/junit-platform.properties b/gradle/src/test/resources/junit-platform.properties index 0cfe8da5..69051e7a 100644 --- a/gradle/src/test/resources/junit-platform.properties +++ b/gradle/src/test/resources/junit-platform.properties @@ -1,2 +1,2 @@ -# Change this to false to display the Cucumber Reports banner -cucumber.publish.quiet=true +# Cucumber searches this package for your step definitions, ect +cucumber.glue=io.cucumber.skeleton diff --git a/maven/src/test/java/io/cucumber/skeleton/RunCucumberTest.java b/maven/src/test/java/io/cucumber/skeleton/RunCucumberTest.java index f2ae9119..ddffab91 100644 --- a/maven/src/test/java/io/cucumber/skeleton/RunCucumberTest.java +++ b/maven/src/test/java/io/cucumber/skeleton/RunCucumberTest.java @@ -5,13 +5,11 @@ import org.junit.platform.suite.api.SelectPackages; import org.junit.platform.suite.api.Suite; -import static io.cucumber.junit.platform.engine.Constants.PLUGIN_PROPERTY_NAME; import static io.cucumber.junit.platform.engine.Constants.GLUE_PROPERTY_NAME; @Suite @IncludeEngines("cucumber") @SelectPackages("io.cucumber.skeleton") -@ConfigurationParameter(key = PLUGIN_PROPERTY_NAME, value = "pretty") @ConfigurationParameter(key = GLUE_PROPERTY_NAME, value = "io.cucumber.skeleton") public class RunCucumberTest { } diff --git a/maven/src/test/resources/junit-platform.properties b/maven/src/test/resources/junit-platform.properties index 0cfe8da5..69051e7a 100644 --- a/maven/src/test/resources/junit-platform.properties +++ b/maven/src/test/resources/junit-platform.properties @@ -1,2 +1,2 @@ -# Change this to false to display the Cucumber Reports banner -cucumber.publish.quiet=true +# Cucumber searches this package for your step definitions, ect +cucumber.glue=io.cucumber.skeleton