Hot questions for Using Cucumber in windows

Question:

In Cucumber(mvn based java project) we need to specify the location of files (e.g) feature file location in the TestRunner as below

@CucumberOptions(features=("src\\test\\resources\\features"),
                 glue= {"com.testing.stepdefinitions"},
                 strict = true,
                 plugin= {"pretty","html:target/cucumber",
                         "com.aventstack.extentreports.cucumber.adapter.ExtentCucumberAdapter:"}
                 )

public class MyRunner extends BaseClass{
}

How to specific the file path such that it works properly in Windows and MAC ?


Answer:

Please try the following instead of src\\test\\resources\\features. This will work in both windows and Mac.

@CucumberOptions(features=("./src/test/resources/features"),

Question:

I'm working on a project which uses a combination of Windows, Java, Groovy, Gradle and Cucumber. This combination gives me some problems on my Windows machine that my *NIX colleagues are not experiencing. Upon running the gradle build, gradle wants to output some reports. The location and filename of these reports is apparently determined by the definition or output of the Cucumber tests. The name used is unfortunately not something that can be used as a filename, so I'm getting an IOException for each test report.

For the Cucumber test, we use the following structure:

  Scenario Outline: Receive and parse ReturnItem from Service
    Given The message from service return item outlined in <messagePath>
    When We process the message
    Then XXX posted a message to YYY on topic <topic> with event <eventType>
    And payload matches <resultPath>

 | messagePath               | topic      | eventType  | resultPath                 |
 | /test/testxml.xml         | test_topic | EVENT_TYPE | /result/result.json        |

After running this, I receive the following exception:

Caused by: org.gradle.api.UncheckedIOException: Could not write to file 'C:\xxx\project\build\reports\tests\test\packages\| \test\testxml.xml | test_topic | EVENT_TYPE | \result\result.html'.
        at org.gradle.internal.IoActions$TextFileWriterIoAction.execute(IoActions.java:151)
        at org.gradle.internal.IoActions$TextFileWriterIoAction.execute(IoActions.java:127)
        at org.gradle.internal.IoActions.writeTextFile(IoActions.java:45)
        at org.gradle.reporting.HtmlReportRenderer$DefaultHtmlReportContext.renderHtmlPage(HtmlReportRenderer.java:118)
        at org.gradle.api.internal.tasks.testing.report.DefaultTestReport$HtmlReportFileGenerator.run(DefaultTestReport.java:147)
        at org.gradle.internal.operations.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:300)
        at org.gradle.internal.operations.DefaultBuildOperationExecutor$RunnableBuildOperationWorker.execute(DefaultBuildOperationExecutor.java:292)
        at org.gradle.internal.operations.DefaultBuildOperationExecutor.execute(DefaultBuildOperationExecutor.java:174)
        at org.gradle.internal.operations.DefaultBuildOperationExecutor.access$900(DefaultBuildOperationExecutor.java:48)
        at org.gradle.internal.operations.DefaultBuildOperationExecutor$ParentPreservingQueueWorker.execute(DefaultBuildOperationExecutor.java:342)
        at org.gradle.internal.operations.DefaultBuildOperationQueue$WorkerRunnable.runOperation(DefaultBuildOperationQueue.java:230)
        at org.gradle.internal.operations.DefaultBuildOperationQueue$WorkerRunnable.access$600(DefaultBuildOperationQueue.java:172)
        at org.gradle.internal.operations.DefaultBuildOperationQueue$WorkerRunnable$1.call(DefaultBuildOperationQueue.java:209)
        at org.gradle.internal.operations.DefaultBuildOperationQueue$WorkerRunnable$1.call(DefaultBuildOperationQueue.java:203)
        at org.gradle.internal.work.DefaultWorkerLeaseService.withLocks(DefaultWorkerLeaseService.java:154)
        at org.gradle.internal.operations.DefaultBuildOperationQueue$WorkerRunnable.runBatch(DefaultBuildOperationQueue.java:203)
        at org.gradle.internal.operations.DefaultBuildOperationQueue$WorkerRunnable.run(DefaultBuildOperationQueue.java:177)
        ... 3 more
Caused by: java.io.IOException: Unable to create directory 'C:\xxx\project\project-test\build\reports\tests\test\packages\| \test\testxml.xml | test_topic | EVENT_TYPE | \result'
        at org.gradle.internal.IoActions$TextFileWriterIoAction.execute(IoActions.java:141)
        ... 19 more

Does anybody know how to fix this? The only 'solution' I could come up with is disabling the reports, which works, but is more a workaround than a solution. For disabling I used the following configuration in the gradle.build for this:

    apply plugin: 'java'
    apply plugin: 'groovy'

    test {
        reports {
            junitXml.enabled = false
            html.enabled = false
        }
    }

(Inspired by: How to make Gradle build produce HTML test report instead of XML default?)


Answer:

Based upon the information provided, it looks like it's trying to create a directory called:

'C:\xxx\project\project-test\build\reports\tests\test\packages\| \test\testxml.xml | test_topic | EVENT_TYPE | \result'

Can you show the code around passing in messagePath? I suspect you are passing in the entire row of data rather than just the messagePath (I'm going to take a wild guess that you are performing a .toString() on an array instead of passing in the first element of the array)

Question:

I'm running through the examples in the Cucumber For Java book on the Windows command line. I can execute it fine from a Windows batch file, but when attempting to do it from Maven, I'm getting "No sources to compile" and "No tests to run". What is needed in the pom to point to the same classes/features as in the batch?

Full output:

[C:\data_jeffy\code\java\cucumber\learning_cucumber\first_taste\checkout]mvn clean test
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Checkout Groceries Example 1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- maven-clean-plugin:2.5:clean (default-clean) @ checkout-groceries-example ---
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ checkout-groceries-example --
-
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platf
orm dependent!
[INFO] skip non existing resourceDirectory C:\data_jeffy\code\java\cucumber\learning_cucumber\first
_taste\checkout\src\main\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ checkout-groceries-example ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ checkout-groceries-ex
ample ---
[WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platf
orm dependent!
[INFO] skip non existing resourceDirectory C:\data_jeffy\code\java\cucumber\learning_cucumber\first
_taste\checkout\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ checkout-groceries-example
 ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.12.2:test (default-test) @ checkout-groceries-example ---
[INFO] No tests to run.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.727 s
[INFO] Finished at: 2015-05-10T13:45:48-04:00
[INFO] Final Memory: 8M/155M
[INFO] ------------------------------------------------------------------------

checkout/pom.xml:

<!--
 ! Excerpted from "The Cucumber for Java Book",
 ! published by The Pragmatic Bookshelf.
 ! Copyrights apply to this code. It may not be used to create training material,
 ! courses, books, articles, and the like. Contact us if you are in doubt.
 ! We make no guarantees that this code is fit for any purpose.
 ! Visit http://www.pragmaticprogrammer.com/titles/srjcuc for more book information.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>uk.co.claysnow</groupId>
    <version>1.0</version>
    <packaging>jar</packaging>
    <name>Checkout Groceries Example</name>
    <artifactId>checkout-groceries-example</artifactId>

    <properties>
        <cucumber.version>1.2.0</cucumber.version>
        <junit.version>4.11</junit.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>${cucumber.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>${cucumber.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
             <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.12.2</version>
                <configuration>
                    <argLine>-Duser.language=en</argLine>
                    <argLine>-Xmx1024m</argLine>
                    <argLine>-XX:MaxPermSize=256m</argLine>
                    <argLine>-Dfile.encoding=UTF-8</argLine>
                    <useFile>false</useFile>
                </configuration>
             </plugin>
        </plugins>
    </build>

</project>

checkout/features/checkout.feature:

Feature: Checkout
    Scenario: Checkout a banana
        Given the price of a "banana" is 40c
        When I checkout 1 "banana"
        Then the total price should be 40c
    Scenario Outline: Checkout bananas
        Given the price of a "banana" is 40c
        When I checkout <count> "banana"
        Then the total price should be <total>c
        Examples:
        | count | total |
        | 1     | 40    |
        | 2     | 80    |
    Scenario: Two bananas scanned separately
        Given the price of a "banana" is 40c
        When I checkout 1 "banana"
        And I checkout 1 "banana"
        Then the total price should be 80c
    Scenario: A banana and an apple
        Given the price of a "banana" is 40c
        When I checkout 1 "banana"
        And I checkout 1 "apple"
        Then the total price should be 65c

checkout/checkout.bat:

@(
set JAR_DIR=C:\data_jeffy\code\java\cucumber\learning_cucumber\first_taste\checkout\jars\
set CLASSPATH=%JAR_DIR%gherkin-2.12.2.jar;%JAR_DIR%cucumber-jvm-deps-1.0.3.jar;%JAR_DIR%cucumber-java-1.2.2.jar;%JAR_DIR%cucumber-core-1.2.2.jar;%JAR_DIR%junit-4.11.jar;.
)
javac -cp %CLASSPATH% step_definitions/CheckoutSteps.java implementation/Checkout.java
@(
REM java -cp %CLASSPATH% cucumber.api.cli.Main --help
)
java -cp %CLASSPATH% cucumber.api.cli.Main -p progress --snippets camelcase -g step_definitions features

Answer:

When using cucumber-jvm with Maven, the project needs to respect the Maven conventions so that cucumber-jvm can pick up the different files. If using maven-surefire-plugin:

  • The feature files need to be stored in src/test/resources;
  • The step definitions need to be stored in src/test/java;
  • The unit test(s) annotated with the @RunWith(Cucumber.class) annotation need to be stored in src/test/java.

Tests can then be executed running mvn test. You can check out this example I have put on github.