I have the following project structure:
MyProject
--src
--test
--acceptance
--step_definitions
--features
--unit
I would like to be able to run my cucumber tests (in test/acceptance) separately in Maven from the unit tests declared in test/unit, so that they can be run in different CI build plans etc. I am using cucumber-junit so the 'runners' for each acceptance test are written with JUnit.
Is this possible?
In that case we can create a Class that defines the runner configurations which then able to run cucumber test in java. Here’s how: First, create a Class that ends with Test in name. For example, CucumberTest.java. I noticed that if your class name doesn’t end with Test, maven doesn’t call that Class.
The 2 most commonly used ways to execute tests in Cucumber Framework are by running the tests using JUnit and Maven. To execute tests using JUnit, we need to create a JUnit Test Runner. Whereas, we need a Maven project to execute Cucumber tests from Command-Line. Create a Maven project and add the below-mentioned dependencies to your Maven project.
All feature files should be in src/test/resources and create the Cucumber Runner class as CucumberRunnerTest. mvn test runs Cucumber Features using Cucumber’s JUnit Runner. The @RunWith (Cucumber.class) annotation on the TestRunner class tells JUnit to start Cucumber.
Here’s how: First, create a Class that ends with Test in name. For example, CucumberTest.java. I noticed that if your class name doesn’t end with Test, maven doesn’t call that Class. Next, add a @RunWith annotation in your class and put Cucumber.class inside. It defines how JUnit can run your class.
Is this possible?
Yes, it is possible. I believe you should separate your unit from the acceptance/integration tests having:
Slightly modified folders structure for both of these, placing your integration test files in the standard location of src/it
:
MyProject/
src/main/java/
(SUT)src/test/
(unit test code)
java/
resources/
src/it/
(acceptance/integration tests)
java/
(steps definitions)resources/
(feature files)Moreover, by design, different Maven plugins are intended for unit and integration tests:
maven-surefire-plugin
maven-failsafe-plugin
You must also bind execution of maven-failsafe-pulgin
. To run the integration tests separately, you can define a new profile:
<profiles>
<profile>
<id>acceptance-tests</id>
<build>
<plugins>
<plugin>
<artifactId>maven-failsafe-plugin</artifactId>
<version>2.12</version>
<executions>
<execution>
<goals>
<goal>integration-test</goal>
<goal>verify</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>
You will also need to configure the plugin to search the src/it
directory tree for test cases.
The acceptance tests can be run afterwards using:
mvn clean verify -Pacceptance-tests
For complete sample, I'd suggest you to follow http://www.weblogism.com/item/334/integration-tests-with-cucumber-jvm-selenium-and-maven
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With