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

问题描述 投票:0回答:2

我在 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 中一个或多个步骤失败,有没有办法继续测试执行呢?

python pytest bdd gherkin pytest-bdd
2个回答
1
投票

我没有找到一种方法来保持单个场景的运行,但每个测试只有一个断言是一个很好的做法。在这种情况下,这会导致大量重复,但您可以使用 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

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


0
投票

你可以使用软断言来避免破坏。

安装 pytest-check :

pip install pytest-check

将其导入步骤定义文件:

from pytest_check import check

在断言之前添加以下内容:

with check: assert "3 Tomatoes" in cart

这将运行后续步骤并在输出中显示失败的检查

© www.soinside.com 2019 - 2024. All rights reserved.