Index
- Introduction
1.1 Imperative
1.2 Declarative
- Tools
2.1 Imperative
2.2 Declarative
1. Introduction
1.1 Imperative
Imperative is when you specify the scenario using very descriptive steps, looking for response codes, different fields, header values and parameters.
Example of gherkin written in the imperative style:
Given I visit "/login"
When I enter "Bob" in the "user name" field
And I enter "tester" in the "password" field
And I press the "login" button
Then I should see the "welcome" page
1.2 Declarative
Declarative is focused on more end-to-end behavior, calling services and expect what’s in the system and what’s in the database.
Describing it more in natural language, in other words, it should describe what, not how.
Example of gherkin written in the declarative style:
When "Bob" logs in
2. Tools
2.1 Imperative
For an imperative style a BDD framework would not be the best option, in this case it would be better to use the api testing frameworks below:
- Tavern (Uses pytest under the hood).
- Karate (Can mix api and UI test-automation).
- Rest-Assured (It tests and validates rest services).
2.2 Declarative
Is preferable to be declarative with the tools below:
- Behave (Similar to cucumber).
- Radish (More friendly for programmatic testing).
- Lettuce (Similar to cucumber, but the community is not very much active anymore).
- Pytest-bdd (Plugin for pytest).
3. Conclusion
Which one is the best?
It depends.
If you want to do simple individual requests and responses validating if the data was saved in the database in a more data-driven test, being imperative is better because it’s usually very programmatically and low level.
If you want to do api testing in a declarative style you should write the scenarios in a more end-to-end behavior.
4. Thanks
- This guide was created based on the course “Behavior Driven Python with pytest-bdd” by Andrew Knight.
- Course available at “Testing Automation University”.
5. Bibliography
- https://testautomationu.applitools.com/behavior-driven-python-with-pytest-bdd/
- https://community.saucelabs.com/forum-45/best-practice-imperative-v-declarative-testing-scenarios-160
- https://cucumber.io/docs/bdd/better-gherkin/
- https://tavern.readthedocs.io/en/latest/
- https://github.com/karatelabs/karate
- https://rest-assured.io/
- https://luizdeaguiar.com.br/2022/06/21/bdd-vs-gherkin-2/