BDD Best Practices
- BDD scenarios should be written in collaboration with product owners, developers and testers.
- Specify the intent and the behavior of the system when defining scenarios.
- Include the intent of the scenario in the scenario title.
- Describe the behavior that is wanted from the system using Given/When/Then (GWT) statements. Do not provide technical implementation of the behavior.
- Use realistic user names/personas when writing scenarios.
- Write scenarios in such a way that it's easy to read and understand by anyone in the team.
- Every scenario should be independent of each other. There should not be any coupling between scenarios.
- Write scenarios for edge cases and failure cases as well. Don’t just focus on happy path scenarios.
-
Always think about your stakeholders when defining scenarios.
- Write Steps Declaratively. Avoid writing brittle Scenarios. Don't provide any UI interaction details like button clicks in GWT scenarios. These details should be pushed into automation implementation. Example:
Brittle Scenario |
Readable Scenario |
Scenario: Successful login Given a user “user1" with password "xyz" And I am on the login page And I fill in "User name" with “user1" And I fill in "Password" with "xyz" When I click "Log in“ button Then I should see "Welcome, User1"
|
Scenario: Successful login Given Jane Smith has an account When she logs in with valid credentials Then she should see the welcome message
|
Feature file and Step file Implementation
- There is no One-To-One mapping between Feature File and Step File.
- Multiple feature file can be implemented by single Step File having methods with matching pattern for each of the steps in those Feature Files.
- Single Feature File can be implemented by multiple Step Files as the Step Methods can be distributed among those Step Files.
- Only restriction is that all those Step Files are to be in the same package.
- Only restriction is that all those Step Files are to be in the same package.
- Matching Step Methods are searched across the specified package. Hence even if we specify two differently named methods with same regex pattern in two different Step Files in same package, then a runtime error will occur.
- Step Methods are reusable. Meaning, for same Steps written in different Step Scenarios or even different Feature Files (generally Given steps can be like that) we need to write only one Step Method.