将标签从 CLI 传递到 Pytest-bdd 并执行特定场景

Pass tags from CLI to Pytest-bdd and execute specific scenarios

要从 CLI 传递并在 conftest.py 中解释以执行满足标签的场景的标签 (smoke/regression)。

我查看了 pytest-bdd 文档here,但未能找到连接。

场景大纲有:(因为 python 装饰器可以堆叠)

@pytest.mark.smoke
Scenario Outline: "VALID" Test

@pytest.mark.smoke
@pytest.mark.regression
  Scenario Outline: "INVALID" Test

@pytest.mark.regression
  Scenario Outline: "MIXED" Test

conftest.py

def pytest_bdd_apply_tag(tag, function):    
    if 'smoke' not in tag:  #what should I use to take values from CLI and execute those
        marker = pytest.mark.skip # skips scenario where 'smoke' is not marked
        marker(function)
        return True
    return None

上面conftest.py中的代码跳过了所有场景。 CLI 输入:

pytest --env='qa' -m 'smoke'

其中 pytest_addoption 用于 --env='qa'pytest_bdd_apply_tag 用于 -m

当我通过烟雾时,我们只想执行标记为烟雾(VALID & INVALID)的场景;当我通过回归和默认烟雾时标记为回归(无效和混合)的场景,当我没有通过 -m 选项在 CLI 中传递任何参数时。

糟糕,我想知道我是如何被文档中提到的带有场景标记行的 pytest 标记误导的。

我对功能文件所做的更改是

@smoke
Scenario Outline: "VALID" Test

@smoke @regression
  Scenario Outline: "INVALID" Test

@regression
  Scenario Outline: "MIXED" Test

我已经从 conftest.py 中删除了 pytest_bdd_apply_tag 方法。

在命令行中给出这个对我有用

pytest -m "regression" --env="uat"