Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set the path to my Cucumber features using cucumber-junit?

Tags:

I try to build my first executable specifications with Java and Maven. I created a simple project with this structure:

specification
|-src
  |-test
    |-java
      |-mypackage
        |-MyFeatureTest.java
    |-resources
      |-MyFeature.feature

In the junit test MyFeatureTest.java I have this:

import org.junit.runner.RunWith;
import cucumber.junit.Cucumber;

@RunWith(Cucumber.class)
public class HomepageTest {
}

Now https://github.com/cucumber/cucumber-jvm/wiki/IDE-support says that I should add the following line:

@Cucumber.Options(paths={"my/super.feature:34"})

I tried to modify that to

@Cucumber.Options(paths={"src/test/resources/"})

but the annotation @Cucumber.Options isn't available at all. My pom.xml has this dependencies:

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.10</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>info.cukes</groupId>
  <artifactId>cucumber-java</artifactId>
  <version>1.0.0.RC20</version>
  <scope>test</scope>
</dependency>
<dependency>
  <groupId>info.cukes</groupId>
  <artifactId>cucumber-junit</artifactId>
  <version>1.0.0.RC20</version>
  <scope>test</scope>
</dependency>

Am I missing something?

Update I was missing something: The cucumber feature file has to be in a subdirectory src/test/resources/mypackage/. Otherwise it won't be picked up by the junit test.

I can run my feature tests when I put them in the same directory src/main/test/, so it's not a blocker for me. But I'd like to understand the whole setup.

like image 273
cringe Avatar asked Mar 12 '12 07:03

cringe


People also ask

How do you give a feature file path in Cucumber?

there is the @Cucumber. Options annotation, among setting the report output format and location, it also allows setting the location for the feature files.

Does Cucumber use JUnit?

Now that we have defined the test its time to run our test. But before we do that we have to add a class for running our tests. Cucumber uses Junit framework to run.


2 Answers

Take a look at my question here:

You can specify a location on the classpath by setting the feature attribute in the options annotation like

@Cucumber.Options(features="src/test/resources")

Edit:

in new versions code is

@CucumberOptions(features="src/test/resources")
like image 139
lanoxx Avatar answered Sep 28 '22 01:09

lanoxx


The classpath option is not obvious in the Cucumber documentation (it is not in the JavaDoc either), I ended up inferring it from the CLI documentation (edit: link is dead, can't find an equivalent), which has other location options documented. See the List configuration options section in the docs. It is also not obvious how to get the feature defintions from another module of a multi module Maven projectg.

This is what got me going (running from IDE and command line) in a Maven multi module project.

@CucumberOptions(
        features = {"classpath:product"},
        //...
)
public class RunCukesTest extends AbstractTestNGSpringContextTests {

where my feature files were located in

main-project
    sub-module-1
        src/test/java/com/foo/
            RunCukesTest.java
        src/test/resources/product/
            feature_1.feature
            feature_2.feature
    sub-module-2
        ...

It pleases me not to see src/test/resources in the path. Note that there is no leading / in the path. Using the classpath is less brittle, because the classpath (rather than the current working directory) must be well defined.

like image 28
markdsievers Avatar answered Sep 28 '22 02:09

markdsievers