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)
Related
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.
Hi i'm using i'm currently using Cucumber version 6.8.1 and i'm trying to figure out how to solve rerunning of failed tests. I've hacked together my own solution but it feels incorrect. In order for
maven clean install
to detect the test runners i appended Test to their names:
#RunWith(Cucumber.class)
#CucumberOptions(plugin = { "de.monochromata.cucumber.report.PrettyReports:target/cucumber", "rerun:target/rerun.txt" })
public class CucumberRunnerTest {
}
and
#RunWith(Cucumber.class)
#CucumberOptions(
features = "#target/rerun.txt",
plugin = {"pretty", "html:target/site/cucumber-pretty",
"json:target/cucumber.json"}
)
public class RerunningTest {
}
Now this works but it seems a bit hacky. since commands like
mvn clean install -Dcucumber.glue="cucumber" \
-Dcucumber.plugin="de.monochromata.cucumber.report.PrettyReports:target/cucumber" \
-Dcucumber.plugin="rerun:target/rerun.txt"
should work, but don't work. The build is successful but no tests run. It's like Dcucumber is being ignored by maven. However, after appending "Test" to the runners and then running the command above uses both runners since they're treated as Tests which feels wrong.
So my question is how can i make this work correctly with Dcucumber?
If you don't name your test class *Test then Surefire won't recognize it. Naming convention wise, try using RunCucumberTest instead. It isn't too ugly.
https://maven.apache.org/surefire/maven-surefire-plugin/examples/inclusion-exclusion.html
Inclusions and Exclusions of Tests
Inclusions
By default, the Surefire Plugin will automatically include all test classes with the following wildcard patterns:
"**/Test*.java" - includes all of its subdirectories and all Java filenames that start with "Test".
"**/*Test.java" - includes all of its subdirectories and all Java filenames that end with "Test".
"**/*Tests.java" - includes all of its subdirectories and all Java filenames that end with "Tests".
"**/*TestCase.java" - includes all of its subdirectories and all Java filenames that end with "TestCase".
If the test classes do not follow any of these naming conventions, then configure Surefire Plugin and specify the tests you want to include.
I am trying to run Cucumber test scenarios (java project) through Gradle by following reference link-https://docs.cucumber.io/tools/java/#build-tools
This is my Build.gradle file
.output of command gradle build
this is showing scenarios are being recognized but WebDriver is not invoking and reports(build/tests/test/index.html) are showing zero.
I am very new to both gradle and cucumber and stuck in my first gradle project.
help will highly appreciated.
Change the glue parameter in task from "gradle.cucumber" to the classpath where your stepdefinitions are present. Switch to org.projectName.appname.tests and try
gradle.cucumber was the location of the package of stepdefinitions specific to the article.
Note- I got it with second another way is --> when we are using TestNG framework, in the build.gradle file we just need to add the following
test{
useTestNG()
}
I have to following project structure:
The directory src/test/java/ic/tests contains junit tests and the directory src/test/features/ic contains cucumber test (feature files).
But when I do a maven run (mvn test -Dcucumber.options="src/test/features/ic --tags #IC-115") to execute a single cucumber test the executor starts the junit tests in the src/test/java/ic/tests directory...
Only the corresponding feature file is annotated with #IC-115.
Even the absolute version mvn test -Dcucumber.options="C:\Users_Clemens_\Documents\test-ic\src\test\resources\features\ic\IC-115-LogOut.feature" does not execute my test.
How can I execute the single cucumber test that I want to execute?
Try to run the command with the name of the feature (exact way to the feature).
mvn test -Dcucumber.options="src/test/features/ic/FeatureName.feature"
Or if the feature is composed by more than one test you could set a specific(not used for others scenarios) tag to the test and run with
mvn verify -Dcucumber.options="--tags #specifictag"
Could solve it by adding a runner class AND moved feature files to "src/test/resources" AND added the maven-surefire-plugin with adding an exclusion to the runner class. Seems like these 3 steps are all necessary.
package kiwigrid;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
#RunWith(Cucumber.class)
#CucumberOptions(features="src/test/resources")
public class Runner {
}
I am trying to find an approach that will allow me to run a single test from a JUnit class using only command-line and java.
I can run the whole set of tests from the class using the following:
java -cp .... org.junit.runner.JUnitCore org.package.classname
What I really want to do is something like this:
java -cp .... org.junit.runner.JUnitCore org.package.classname.method
or:
java -cp .... org.junit.runner.JUnitCore org.package.classname#method
I noticed that there might be ways to do this using JUnit annotations, but I would prefer to not modify the source of my test classes by hand (attempting to automate this). I did also see that Maven might have a way to do this, but if possible I would like to avoid depending on Maven.
So I am wondering if there is any way to do this?
Key points I'm looking for:
Ability to run a single test from a JUnit test class
Command Line (using JUnit)
Avoid modifying the test source
Avoid using additional tools
You can make a custom, barebones JUnit runner fairly easily. Here's one that will run a single test method in the form com.package.TestClass#methodName:
import org.junit.runner.JUnitCore;
import org.junit.runner.Request;
import org.junit.runner.Result;
public class SingleJUnitTestRunner {
public static void main(String... args) throws ClassNotFoundException {
String[] classAndMethod = args[0].split("#");
Request request = Request.method(Class.forName(classAndMethod[0]),
classAndMethod[1]);
Result result = new JUnitCore().run(request);
System.exit(result.wasSuccessful() ? 0 : 1);
}
}
You can invoke it like this:
> java -cp path/to/testclasses:path/to/junit-4.8.2.jar SingleJUnitTestRunner
com.mycompany.product.MyTest#testB
After a quick look in the JUnit source I came to the same conclusion as you that JUnit does not support this natively. This has never been a problem for me since IDEs all have custom JUnit integrations that allow you to run the test method under the cursor, among other actions. I have never run JUnit tests from the command line directly; I have always let either the IDE or build tool (Ant, Maven) take care of it. Especially since the default CLI entry point (JUnitCore) doesn't produce any result output other than a non-zero exit code on test failure(s).
NOTE:
for JUnit version >= 4.9 you need hamcrest library in classpath
I use Maven to build my project, and use SureFire maven plugin to run junit tests.
Provided you have this setup, then you could do:
mvn -Dtest=GreatTestClass#testMethod test
In this example, we just run a test method named "testMethod" within Class "GreatTestClass".
For more details, check out http://maven.apache.org/surefire/maven-surefire-plugin/examples/single-test.html
The following command works fine.
mvn -Dtest=SqsConsumerTest -DfailIfNoTests=false test
We used IntelliJ, and spent quite a bit of time trying to figure it out too.
Basically, it involves 2 steps:
Step 1: Compile the Test Class
% javac -cp .:"/Applications/IntelliJ IDEA 13 CE.app/Contents/lib/*" SetTest.java
Step 2: Run the Test
% java -cp .:"/Applications/IntelliJ IDEA 13 CE.app/Contents/lib/*" org.junit.runner.JUnitCore SetTest