如何实现pytest xfail标记与pytest-bdd?



我正在使用pytest bdd自动化api。我需要实现@pytest.mark.xfail定义为我的一个步骤。但是在添加了这个装饰器之后,它并没有像预期的那样工作。

例子比;

example.feature
Scenario: Validate the API response where availableLicense should not be greater than the totalLicense.
Given Send valid input
Then validate the availableLicense count should not be greater than totalLicense.
test_example.py
@given('Send valid input')
def valid_data(context, url,token_url, api_key):
context.response = api_req('get',url, token_url, api_key)
@then('validate the availableLicense count should not be greater than totalLicense.')
@pytest.mark.xfail
def validate_license_count(context):
<some logic>
assert avail_license <= total_license

还是我测试用例显示为失败当上面的断言失败。我该怎么做呢?

您需要将@pytest.mark.xfail添加到具有@scenario装饰符的pytest-bdd场景测试函数中,而不是特定的pytest-bdd步骤实现。

那么对于你的例子,它将是如下所示:

Scenario: Validate the API response where available License should not be greater than the total License.
Given Send valid input
Then validate the available License count should not be greater than total License.
@pytest.mark.xfail
@scenario('Validate the API response where available License should not be greater than the total License.')
def test_scenario():
@given('Send valid input')
def valid_data(context, url,token_url, api_key):
context.response = api_req('get',url, token_url, api_key)
@then('validate the availableLicense count should not be greater than totalLicense.')
@pytest.mark.xfail
def validate_license_count(context):
<some logic>
assert avail_license <= total_license

注意默认情况下,xfail不会让测试套件失败,即使已修饰的测试成功。为了使测试套件在这种情况下失败,您必须使用pytest.mark.xfail(strict=True)(请参阅xfail文档)。

最新更新