Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Your specification is made using Gherkin (i.e. Given, When, That) statements in Scenario(s) or Scenario Outline(s), eventually complemented with a Background. Implementation of each Gherkin statement (i.e. "step") is done in code; the Cucumber framework finds the code based on regular or cucumber expressions.


Note
iconfalse
titleSource-code for this tutorial
typeInfo

Code is available in GiHub; the repo contains some auxiliary scripts.

Usage scenarios

Cucumber is used in diverse scenarios. Next you may find some usage patterns, even though Cucumber usage is mostly recommended only if you are adopting BDD.

...

Code Block
languagejava
titlesrc/test/java/calculator/StepDefinitions.java
collapsetrue
package calculator;

import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import com.xray.tutorials.Calculator;

import static org.junit.Assert.*;

public class StepDefinitions {
    private Integer int1;
    private Integer int2;
    private Integer result;


    @Given("I have entered {int} into the calculator")
    public void i_have_entered_into_the_calculator(Integer int1) {
        this.int2 = this.int1;
        this.int1 = int1;
    }
    
    
    @When("I press add")
    public void i_press_add() {
        this.result =  Calculator.Add(this.int1, this.int2);
    }

    @When("I press multiply")
    public void i_press_multiply() {
        this.result =  Calculator.Multiply(this.int1, this.int2);
    }


    @Then("the result should be {int} on the screen")
    public void the_result_should_be_on_the_screen(Integer int1value) {
        // Write code here that turns the phrase above into concrete actions
        assertEquals((Integer)(this.int1 + this.int2)value, this.result);
    }
    

}
  • the test runner is defined in the RunCucumberTest class. Cucumber options can be overriden from the command line, whenever executing Maven.

...

Code Block
languagebash
mvn compile test -Dcucumber.plugin="json:report.json" -Dcucumber.features="features/"

This will produce one Cucumber JSON report with all results.

...

Info
titlePlease note

As the report format in Cucumber JSON is being deprecated in favour of Cucumber Messages, a protocol buffer based implementation, the previous command needs to be adapted slightly.

The report starts by being generated in Cucumber Messages, using "-f message" argument, and then converted to the legacy Cucumber JSON report using the tool cucumber-json-formatter.


Code Block
languagebash
titleexample of a Bash script to import results using the standard Cucumber endpoint
collapsetrue
curl -H "Content-Type: application/json" -X POST -u admin:admin --data @"report.json" http://jiraserver.example.com/rest/raven/1.0/import/execution/cucumber

Image Removed

mvn compile test -Dcucumber.plugin="json:report.ndjson" -Dcucumber.features="features/"
cat report.ndjson | cucumber-json-formatter --format ndjson > report.json


This will produce one Cucumber JSON report with all results.

After running the tests, results can be imported to Xray via the REST API, or the Import Execution Results action within an existing Test Execution, or by using one of the available CI/CD plugins (e.g. see an example of Integration with Jenkins).

Code Block
languagebash
titleexample of a Bash script to import results using the standard Cucumber endpoint
collapsetrue
curl -H "Content-Type: application/json" -X POST -u admin:admin --data @"report.json" http://jiraserver.example.com/rest/raven/1.0/import/execution/cucumber

Image Added


Info
titleWhich Cucumber endpoint to use?

To import results, you can use two different endpoints/"formats" (endpoints described in Import Execution Results - REST):

  1. the "standard cucumber" endpoint
  2. the "multipart cucumber" endpoint

The standard cucumber endpoint (i.e. /import/execution/cucumber) is simpler but more restrictive: you cannot specify values for custom fields on the Test Execution

Info
titleWhich Cucumber endpoint to use?

To import results, you can use two different endpoints/"formats" (endpoints described in Import Execution Results - REST):

  1. the "standard cucumber" endpoint
  2. the "multipart cucumber" endpoint

The standard cucumber endpoint (i.e. /import/execution/cucumber) is simpler but more restrictive: you cannot specify values for custom fields on the Test Execution that will be created.  This endpoint creates new Test Execution issues unless the Feature contains a tag having an issue key of an existing Test Execution.

The multipart cucumber endpoint will allow you to customize fields (e.g. Fix Version, Test Plan), if you wish to do so, on the Test Execution that will be created. Note that this endpoint always creates new Test Executions (as of Xray v4.2).


In sum, if you want to customize the Fix Version, Test Plan and/or Test Environment of the Test Execution issue that will be created, you'll have to use the "multipart cucumber" endpoint.

...

Code Block
languagejava
titlebuggy Multiply() method in Calculator.java
collapsetrue
// Multiply two integers and retuns the result... this code is buggy on purpose
public static int Multiply(int num1, int num2 )
{
    if ((num1==10) || (num2==1)) {
        return num2;
    } else if (num2==0) {
        return 0num1;
    } else {
        return num1 * num2;
    }
}

...

Code Block
languagejava
titlesrc/test/java/calculator/StepDefinitions.java
collapsetrue
package calculator;

import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import com.xray.tutorials.Calculator;

import static org.junit.Assert.*;

public class StepDefinitions {
    private Integer int1;
    private Integer int2;
    private Integer result;


    @Given("I have entered {int} into the calculator")
    public void i_have_entered_into_the_calculator(Integer int1) {
        this.int2 = this.int1;
        this.int1 = int1;
    }
    
    
    @When("I press add")
    public void i_press_add() {
        this.result =  Calculator.Add(this.int1, this.int2);
    }

    @When("I press multiply")
    public void i_press_multiply() {
        this.result =  Calculator.Multiply(this.int1, this.int2);
    }


    @Then("the result should be {int} on the screen")
    public void the_result_should_be_on_the_screen(Integer int1value) {
        // Write code here that turns the phrase above into concrete actions
        assertEquals((Integer)(this.int1 + this.int2)value, this.result);
    }
    

}
  • the test runner is defined in the RunCucumberTest class. Cucumber options can be overriden from the command line, whenever executing Maven.

...

Code Block
titlefeatures/2_CALC-7935.feature
collapsetrue
@REQ_CALC-7935
Feature: As a user, I can multiply two numbers
	#As a user, I can multiply two numbers

	#simple integer multiplication
	@TEST_CALC-7936
	Scenario: simple integer multiplication
		Given I have entered 3 into the calculator
		And I have entered 0 into the calculator
		When I press multiply
		Then the result should be 0 on the screen


To run the tests and produce a Cucumber JSON report, we can run Maven and specify that we want a report in Cucumber JSON format and that it should process .features from the features/ directoryproduce a Cucumber JSON report, we can run Maven and specify that we want a report in Cucumber JSON format and that it should process .features from the features/ directory.


Code Block
languagebash
mvn compile test -Dcucumber.plugin="json:report.json" -Dcucumber.features="features/"
Info
titlePlease note

As the report format in Cucumber JSON is being deprecated in favour of Cucumber Messages, a protocol buffer based implementation, the previous command needs to be adapted slightly.

The report starts by being generated in Cucumber Messages, using "-f message" argument, and then converted to the legacy Cucumber JSON report using the tool cucumber-json-formatter.


Code Block
languagebash
mvn compile test -Dcucumber.plugin="
json:report.json" -Dcucumber.features="features/"
json:report.ndjson" -Dcucumber.features="features/"
cat report.ndjson | cucumber-json-formatter --format ndjson > report.json


This will produce one Cucumber JSON report with all results.

...

Code Block
languagejava
titlebuggy Multiply() method in Calculator.java
collapsetrue
// Multiply two integers and retuns the result... this code is buggy on purpose
public static int Multiply(int num1, int num2 )
{
    if ((num1==1) ||0) {
        return num2;
    } else if (num2==10)) {
        return 0num1;
    } else {
        return num1 * num2;
    }
}

...

Code Block
languagejava
titlefix of Multiply() method in Calculator.java
collapsetrue
public static int Multiply(int num1, int num2 )
{
	return num1 * num2;
}

FAQ and Recommendations

Please see this page.

References

...