Parameterise JUnit CucumberOptions through Jenkins - java

I am busy with a BDD automation project that is coded in Java. We use Cucumber to run the BDD. The JUnit class that we use to kick off the run is as follows:
#RunWith(Cucumber.class)
#CucumberOptions(
plugin = {"usage", "json:target/cucumber.json", "html:target/cucumber", "com.cucumber.listener.ExtentCucumberFormatter:"},
glue = {"BDD.step_definitions"},
features = {"Unit-Tests\\Features"},
dryRun = false,
monochrome = false,
strict = true
)
The issue that I am having is that I would like to parameterize the CucumberOptions through Jenkins, ideally setting the feature file or scenario name through a Jenkins choice parameter.
I am using AntBuild to build and test my project on Jenkins. Would it be possible to set the CucumberOptions on the build XML?

You are interested in understanding how you can send arguments to the Java process you run in Jenkins.
Setting Cucumber options can be done like this:
-Dcucumber.options="--help"
If you are running from Maven, it would look like this:
mvn test -Dcucumber.options="--help"
Running a specific tag could be done like this:
-Dcucumber.options="--tags #wip"
More details can be found in the Cucumber docs or in a blog post I wrote a while back.

Related

Cucumber/Java - Missing "matching glue code"

I've setup some automation in Eclipse using Java, Cucumber and Selenium.
I've defined my Feature file, Steps and been using a Page Object Model and all my tests are running and reporting successfully.
However, I'm getting a warning on every Step saying it "does not have matching glue code". I've tried modifying my path names and moving the files around, but that just seems to cause issues that prevent it running.
Using cucumber 6.2.2 jars and cucumber-expressions 10.2.1
Folder Structure
#RunWith(Cucumber.class)
#CucumberOptions(
features = {"src/test/resources/functionalTests"},
tags = "#register2",
glue = {"stepDefinitions"},
plugin = {"pretty", "html:target/cucumber-reports.html"},
monochrome = true
)
In your glue option can you write full package name.
For example:
com.testautomation.test.stepdefinitions

Rerun.txt not populating when trying to rerun failed tests using cucumber and jenkins

I am trying to rerun my flaky tests in jenkins as sometimes my environment is very slow I have a main runner which is meant to add the failed features to a rerun.txt file and then a conditional step is set up in jenkins to run the failed runner, but the txt file doesnt seem to populate
My main runner is:
#CucumberOptions(
features = {"src/test/java/noting/feature_files/"},
glue = {"clinical_noting.steps", "clinical_noting.hooks"},
tags = {"#regression"},
monochrome = true,
plugin = {"pretty", "rerun:rerun.txt","json:target/json-report.json", "junit:target/junit-
report.xml", "html:target/html-report","junit:target/cucumber.xml"}
)
public class MainRunner {
}
and my failed runner is
#RunWith(Cucumber.class)
#CucumberOptions(
features = {"#target/rerun.txt"},
plugin = {"pretty", "json:target/cucumber-
report/cucumber.json","rerun:target/rerun.txt","junit:target/cucumber.xml"}
//plugin = {"html:target/cucumber/"}
)
public class FailedRunner {
}
The rerun.txt file is created but nothing is entered into it, ive tried moving to resources file but it doesnt seem to work, and ament sure if the conditional step in jenkins is where the Failed runner should be
Don't use rerun plugin in failed runner.

Run cucumber tests in a serial fashion in Java

I have a number of cucumber .feature file written in Java.
It looks like they are running in parallel.
How do I run the cucumber tests one by one in a single threaded fashion?
The run test config is:
#RunWith(Cucumber.class)
#CucumberOptions(strict = true,
glue = "com.myproject.steps",
features = "classpath:cucumber/features/",
format = { "pretty", "html:target/report/cucumber",
"json:target/report/cucumber/cucumber.json" })
public class RunIntegrationTest {
}
You are using Junit and junit not have such parallel execution functionality
Another thing is cucumber cucumber-jvm-parallel, are you using it?
Does you have multiple TestRunner java file with CucumberOptions in src.test.java package, if so keep one class file only

how to specify cucumber tags as invocation param in eclipse debug configuration?

I have a Spring Boot project. In it I have a java class BookTests which contains:
#RunWith(Cucumber.class)
#CucumberOptions(
plugin = {
"pretty",
"html:target/acceptance-test-report",
"json:target/cucumber/cucumber.json"
},
glue = {
"com.my.sdk.test",
"com.my.book.test"
}
,tags = {"#findConsideringGrants"}
)
public class BookTests {
// No implementation required. The Cucumber annotations drive the test execution.
}
I'm using Junit 4. when I "Debug As JUnit Test" this class it only runs the feature file which is annotated with "#findConsideringGrants (as it should)
Is there a way i don't have to hard-code this tags annotation in the java class but instead specify the tags as Program argumnents or VM arguuments?
I tried configuring it with Program argumnents, but with all the following attempts it ignored the argument and ran the tests on all my features/tags:
--tags findConsideringGrants
-tags findConsideringGrants
--tags #findConsideringGrants
-tags #findConsideringGrants
So to summerize, i'm looking for a way to
1) debug in eclipse a certain feature
2) without hardcoding the name of the feature into the java source. Somehow by just configuring the debug configuration.
is this even possible?
If you are using maven you can use -- mvn test -Dcucumber.options="--tags #findConsideringGrants".
Or you can use the cucumber cli command to run -- Main.main(new String[]{"-g", "classpath to step definition file","-t", "tags to use", "-p", "plugins to use", "Full path to feature file"}). The feature file path has to be the last option.
Running from eclipse --
Go to the "Environment" tab of the "Debug Configurations" or "Run Configurations" window. Click on "New" button. For the "Name" add "cucumber.options" and "Value" add "--tags #findConsideringGrants". Apply and Run. If you want to add a specific feature file path add only the path to the end of this.

How to run a Cucumber-JVM feature file from the command line

I have a file with file extension .feature. HOw do run this from the command line?
In order to make a batch file for each feature. I am using Cucumber-JVM with Java and Selenium.
Cucumber-JVM is based on JUnit so its just like running any unit tests from the command line
java -cp /path/to/junit.jar org.junit.runner.JUnitCore [test class name]
where test class name is annotated with #CucumberOptions whose features
refer to the feature file. If youre using Maven you could use
mvn test
If you are using Maven, you can run your .feature file this way:
mvn -Dcucumber.options="from/the/root/of/module/to/the/feature/MyFeature.feature" test
This will override the #CucumberOptions in the test runner class.
You can run any test runner or override default test runner by using the command line below. This means you only want to run the test named SmokeTestRunner. By the way, you are free to set surefire plugin in your pom.xml any way. For example, you can set up your surefire plugin in your pom.xml to run the regression test named RegressionTestRunner. It doesn't matter.
mvn -Dtest=**/SmokeTestRunner.java test
Your SmokeTestRunner java file should look like this. ->
#CucumberOptions(
features = "src/test/resources/features/Smoke.feature",
glue = {"stepdefinitions", "hooks"},
tags = "#chrome", // It runs only scenarios tagged with "#chrome"
plugin = {"pretty"/*you can add more cucumber plugins here if you want.*/},
monochrome = true)
public class SmokeTestRunner extends AbstractTestNGCucumberTests {}
For more details, have a look at the maven surefire plugin docs. (https://maven.apache.org/surefire/maven-surefire-plugin/examples/single-test.html)

Categories