Pytest Bdd:即使失败,如何继续执行 BDD 中的步骤

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

我在 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

在这里我们可以看到该步骤(然后购物车中有“3”“西红柿”)将失败并且测试执行将停止在那里并且不会执行其余步骤。 那么在pytest bdd中即使一个或多个步骤失败,有什么方法可以继续测试执行吗?

我看不出保留单一场景的方法 运行,但每次测试只使用一个断言是一种很好的做法。在这种情况下会导致大量重复,但您可以使用 backgrounds 删除它:

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

我同意这有点不自然,但这是我能想到的最好的。也许其他人有更好的主意。