Search code examples
pythonpytestbddgherkinpytest-bdd

Pytest Bdd : How to continue execution of steps in BDD even if one failed


I have a scenario like this implemented on pytest-bdd

Scenario: Shopping Cart Verification
  Given I am out for shopping shopping and took a cart
  Given I added "2" "Tomatoes" to the cart
  Given I added "3" "Bread" to the cart
  Then there is "3" "Tomatoes" in the cart
  Then there is "3" "Bread" in the cart
  Then there are "5" items in the cart

Here we can see that the step (Then there is "3" "Tomatoes" in the cart) will fail and the the test execution will stop there and the rest of the steps wont be executed. So is there any way to continue the test execution even if one or more steps fails in pytest bdd?


Solution

  • I don't see a way to keep a single scenario running, but it's good practice to have only one assertion per test. In this case that would result in a lot of duplication, but you can use backgrounds to remove this:

    Feature: Shopping cart
    
      Background:
        Given I am out for shopping shopping and took a cart
        Given I added "2" "Tomatoes" to the cart
        Given I added "3" "Bread" to the cart
    
      Scenario: The tomatoes are in the cart
        Then there is "3" "Tomatoes" in the cart
    
      Scenario: The bread is in the cart
        Then there is "3" "Bread" in the cart
    
      Scenario: The items are in the cart
        Then there are "5" items in the cart
    

    I agree it's a bit unnatural, but this is the best I can come up with. Maybe someone else has a better idea.