Catalog / Cucumber Cheat Sheet
Cucumber Cheat Sheet
A comprehensive guide to Cucumber, covering Gherkin syntax, step definitions, configuration, and best practices for writing effective and maintainable automated tests.
Gherkin Syntax Essentials
Feature and Scenario Structure
|
Example:
|
Keywords
|
Sets up the initial context of the scenario. |
|
Describes an event or action performed by the user. |
|
Specifies the expected outcome or result. |
|
Used to chain multiple |
|
A set of steps that run before each scenario in a feature. |
Data Tables and Doc Strings
|
Data Table Example:
|
Doc String Example:
|
Step Definitions
Basic Step Definition Structure
Step definitions link Gherkin steps to code that executes those steps.
|
Step definitions typically use regular expressions to match the Gherkin step text. |
Regular Expression Usage
|
Matches any character (except newline) zero or more times. |
|
Matches one or more digits and captures the value. |
|
Matches any character except a double quote, zero or more times, and captures the value. |
^(.*)$ |
Matches the entire line and captures it. |
Step Definition with Arguments
|
|
Configuration and Hooks
Cucumber Configuration
Cucumber is typically configured using a Key configuration options include:
|
Example
|
Hooks
|
Runs before each scenario or a tagged scenario. |
|
Runs after each scenario or a tagged scenario. |
|
Wraps around each scenario, allowing you to perform actions before and after the scenario. |
|
Runs after each step. |
Hook Examples
|
Advanced Cucumber Techniques
Tagged Hooks and Scenarios
Tags are used to organize and filter scenarios and hooks. Scenarios can be tagged directly in the feature file:
Hooks can be tagged to run only for specific scenarios:
|
Parallel Execution
Cucumber can be configured to run scenarios in parallel, significantly reducing test execution time. This often involves using a gem like Configuration typically involves specifying the number of parallel processes to use. |
Best Practices
|