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

985 Views Asked by At

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?

2

There are 2 best solutions below

0
On BEST ANSWER

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.

0
On

You can use Soft Assert for not to break.

Install pytest-check with :

pip install pytest-check

Import this in the step def file :

from pytest_check import check

Before the Assert, add this :

with check: assert "3 Tomatoes" in cart

This will run the next steps and show failed checks in the output