Imperative VS Declarative Testing


Index

  1. Introduction
    1.1 Imperative
    1.2 Declarative
  1. Tools
    2.1 Imperative
    2.2 Declarative
  1. Conclusion
  1. Thanks
  1. Bibliography


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


5. Bibliography

Leave a Comment

Your email address will not be published. Required fields are marked *