在 python.mark.parametrized 中使用 cli 参数

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

我需要使用 run cli 命令中的参数

--license
来排除某些测试用例的执行。

我想用

mark.skipif
条件,但是怎么能称为
--license
的值呢?

我尝试了

mark.parametrized
的下一个变体,但据我所知,它与版本pytest 7.4.4无关:

@mark.parametrize(
    "language, id",
    [
        ("en", 1),
        param(
            "fr", 2,
            marks=mark.skipif(
                pytest.config.getoption("--license"),
            )
        ),
        ("qwer", 3),
    ],
)
python python-3.x automated-tests pytest
1个回答
0
投票

--license
添加到命令行有点复杂。最简单的方法是使用自定义标记来标记测试(例如“未经许可”):

@pytest.mark.parametrize(
    "language, lang_id",
    [
        ("en", 1),
        pytest.param("fr", 2, marks=pytest.mark.unlicensed),
        ("qwer", 3),
    ],
)

为了避免针对自定义标记发出警告,请将以下行添加到

pyproject.toml

# pyproject.toml
[tool.pytest.ini_options]
markers = [
    "unlicensed",
]

调用测试:

pytest                     # Run all tests
pytest -m 'not unlicensed' # Skip those with 'unlicensed' marker
© www.soinside.com 2019 - 2024. All rights reserved.