Search code examples
cucumberbddgherkincucumber-javaserenity-bdd

Assertions in BDD scenario


I am trying to understand how to correctly create my BDD REST testing scenarios.

From what I have read online, we should have only one WHEN-THEN pair and the assertions should be done in the THEN step.

What if we had a situation such as

Given user searches for flight

And user picks seat

And user adds bags to flight

When user purchases flight

Then flight is successfully booked

What happens if we get a 500 Status error when we try to add the bag to flight. Should we at the very least do basic assertions in all steps?


Solution

  • You can add assertion at all steps.

    From what I have read online, we should have only one WHEN-THEN pair and the assertions should be done in the THEN step

    The main assertion of scenario can be done at Then step. Below example is from my current project and you can see how I am writing steps,

    @TC_CUE_Search_for_all_templates_and_validate_particular_template_response
    Scenario: Validate json details for given template
    Given Get template key and Application key from db
    And Load Datasheet for Iteration
    And Execute the request to fetch all the templates of an application
    Then Get and verify the response of given template from template result list
    
    Given Get template key and Application key from db
    

    I validate whether DB is returning valid response or not. If I receive valid response then I fetch the required details from DB and write it in excel.

    And Load Datasheet for Iteration
    

    Reads the excel sheet.

    And Execute the request to fetch all the templates of an application
    

    GET request will be executed and I validate the response code and store the result.

    Then Get and verify the response of given template from template result list
    

    Here I do the main validation. In earlier step, it returns huge response and I fetch the particular response based on the parameter and verify all the required fields.

    Purpose of this example is to clarify how you can automate REST scenarios.