Run cucumber tests in a serial fashion in Java - 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

Related

Can't run individual Cucumber scenarios using TestNG test runner

Is there anyway to run cucumber scenarios using the TestNG runner by right clicking individual scenarios in the feature files rather than using the command file or running the TestNG runner file directly?
I'm using Intellij to run cucumber scenarios in a maven testing framework. In the POM.xml file I have the Surefire plugin referencing the testNG.xml file which points to the TestNG runner class.
When I run "mvn test" from terminal it calls the TestNG testrunner,but when I right click on the scenario in the feature file to select run, it runs the io.cucumber.core.cli.main class that calls the io.cucumber.core.runner.Runner class testrunner.
I can't edit Run/Debug configuration to use the TestNG runner because the TestNG runner doesn't have a Main method.
you can create a runner class and add tags you need into CucumberOptions.
#CucumberOptions(plugin = {"pretty"}, strict = true, tags = {"#yourTag"})
public class RunTestNGTest extends AbstractTestNGCucumberTests {
}
If anybone runs into the same issue, I managed to get around the issue by creating another class with a main method and calling the mvn command to run the test from there. something like this:
String mvnCommand = "mvn test ";
ProcessBuilder processBuilder = new ProcessBuilder("cmd.exe", "/c", mvnCommand + args);
processBuilder.start();
Not sure if there's a better solution, but I can't seem to find any other option with Cucumber for Java plugin.

How to integrate both karate and cucumber and invoke both from a single file/class

I created cucumber automation code for a project, How to call karate feature files from cucumber runner class, or any other way to run the karate feature files from cucumber framework, My plan is to integrate both karate and cucumber and invoke both from a single file/class
#RunWith(Cucumber.class)
public class TestRunner_Cucumber {
}
Do this using the Java API. Read the docs: https://github.com/intuit/karate#java-api
Map<String, Object> result = Runner.runFeature("classpath:demo/java/from-java.feature", args, true);
By the way, I don't recommend BDD for API testing: https://stackoverflow.com/a/47799207/143475

Parameterise JUnit CucumberOptions through Jenkins

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.

JVM Cucumber Execution

I am building a framework that is able to execute Selenium instructions and Cucumber instructions. I have created the runner for Cucumber in a seperate empty class with the following code
import cucumber.api.junit.Cucumber;
import org.junit.runner.RunWith;
#RunWith(Cucumber.class)
public class RunCukesTest {
}
I also will have a processor for Selenium instructions in a separate class.
prop.load(ClassLoader.getSystemResourceAsStream("src/main/resources/runConfig.properties"));
if(prop.getProperty("instructionsheet").contains("InstructionSheets")){
KeywordProcessor kp = new KeywordProcessor();
}else if(prop.getProperty("instructionsheet").contains("FeatureFiles")){
RunCukesTest runCukes = new RunCukesTest();
}
How can I execute the Cucumber runner class if it's an empty class?, do I just create an instance of the runner class for cucumber? or do I have to spacify something else
#RunWith(Cucumber.class) is used to integrate cucumber with the existing infrastructure for running JUnit tests. If you start to roll out your own framework, it is worth to look other places. cucumber.api.cli.Main could be a good place to start your journey.
The class RunCukesTest is a class that will be executed by the JUnit runner Cucumber.class since it is annotated with #RunWith(Cucumber.class).
In other words, it will be executed when you run the tests the same way as any other JUnit class.
The class is empty, and has to be empty, to force a separation of the execution of Cucumber and the steps supporting the execution.
If you want to execute Cucumber from another context, say your own framework, look at the command line interface. You are able to run its main() from any Java class, just call the static method and wait for the result.

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