0

我正在使用 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

当上述断言失败时,我的测试用例仍然显示为失败。我应该在这里做什么?

4

1 回答 1

0

您需要添加@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 文档)。

于 2021-12-29T11:51:02.030 回答