BDD Cucumber Basics
BDD Cucumber Basics
2. Gherkin Syntax:
Gherkin is a plain-text language with a simple structure. It is designed to be easy to learn
by non-programmers, yet structured enough to allow concise descriptions of test
scenarios. Every feature in Gherkin is specified as <name>.feature file and adheres to a
strict syntax.
a. Feature:
b. Scenario:
c. Given-When-Then:
Syntax:
Feature: [Title]
Scenario: [Title]
Given [Precondition]
When [Action]
Then [Expected Outcome]
d. Keywords:
And, But: Additional steps (depends on the version of cucumber dependencies)
Syntax:
Given [Condition]
And [Additional Condition]
But [Additional Condition]
e. Background:
Syntax:
Background:
3. Step Definitions:
Gherkin Example:
Remember, BDD is about collaboration, clarity, and automated tests to ensure that the
software behaves as expected. Adjust the cheat sheet based on your specific BDD tool or
framework.
Data tables and scenario outlines allow you to provide dynamic input to your scenarios.
Scenario Outline:
The Scenario Outline keyword can be used to run the same Scenario multiple times, with
different combinations of values.
Copying and pasting scenarios to use different values quickly becomes tedious and
repetitive:
Gherkin Example:
Scenario outlines allow us to more concisely express these scenarios through the use of a
template with < >-delimited parameters:
Gherkin Example:
Examples:
| start | eat | left |
| 12 | 5 | 7 |
| 20 | 5 | 15 |
Anshul Agarwal
Gherkin Example:
@tag
Scenario: Tagged Scenario
Given [Precondition]
When [Action]
Then [Expected Outcome]
Usage: Tags can be used to group scenarios, filter which tests to run, or specify
preconditions.
Anshul Agarwal
Hooks are blocks of code that can run at various points in the test execution cycle.
● Types of Hooks-
● @Before: Runs before each scenario.
● @After: Runs after each scenario.
● @BeforeStep: Runs before each step.
● @AfterStep: Runs after each step.
@Before
public void beforeScenario() {
// code to execute before each scenario
}
@After
public void afterScenario() {
// code to execute after each scenario
}
Gherkin Example:
Cucumber allows the creation of custom parameter types to handle complex data
transformations.
ParameterType(
"currency",
"USD|EUR|GBP",
(String currency) -> Currency.getInstance(currency)
)
Gherkin Example:
Cucumber Configuration:
Cucumber Configuration Files: Use cucumber.properties or cucumber.yml for
configuration settings.
cucumber.properties file
# cucumber.properties
cucumber.format=pretty
cucumber.output=target/cucumber-report
cucumber.yml file
# cucumber.yml
default:
format:
- pretty
- html:target/cucumber-html-report
- json:target/cucumber.json
- xml:target/cucumber.xml
Anshul Agarwal
IDE Integration: Running tests directly from an IDE like IntelliJ IDEA or Eclipse.
Continuous Integration: Integrate Cucumber tests with CI/CD pipelines using tools like
Jenkins, GitLab CI, etc.
@CucumberOptions(
plugin = {"pretty", "json:target/cucumber-report.json",
"junit:target/cucumber-report.xml"}
)
Third-party Tools: Integrate with tools like Cucumber Reports, Allure, etc., for enhanced
reporting capabilities.
Gherkin Example:
The guide covers fundamental concepts, such as writing feature files, step definitions, and
best practices, while also delving into advanced topics like data tables, scenario
outlines, and custom parameter types. By following these guidelines, teams can
enhance their test automation, streamline their development processes, and achieve more
reliable and maintainable software.
Thank you