python - 如何使用 pytest-bdd 实现 pytest xfail 标记?
问题描述
我正在使用 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 文档)。
推荐阅读
- spring - Spring Batch - 如何以更快的方式读取 500 万条记录?
- jquery - JQ函数hover()
- python - 将附加数据合并到我的 TFIDF 数组中
- javascript - 我希望 javascript 在页面中心弹出
- javascript - ASP,输入后聚焦文本框
- python - 如何使用python将一个变量放入另一个变量数组
- css - 混合混合模式似乎无法在边缘工作
- python - 使用 Discord.py 创建机器人时,如何让我的音乐机器人在观众离开时离开?
- flutter - 使文本在点击时出现
- c++ - C++ 构造函数需要一个“:”(冒号)