【发布时间】:2019-08-24 19:51:46
【问题描述】:
我正在尝试为一个测试用例的整个参数化运行拆卸功能/夹具。所以我想在我的 conftest.py 文件中有这样的东西
@pytest.fixture(autouse=True, scope="parametrization")
def parametrization_scope():
print("First run of parametrization fixture")
# Executing 100 parametrizations
yield
print("Yield run of parametrization fixture")
在我的 test.py 中类似于
@pytest.mark.parametrize(n-params)
# run initiate fixture here of parametrization
def test():
# executing n number of times
# run yield fixture here of parametrization
【问题讨论】:
-
将拆解代码放在夹具中
yield之后。 -
是的,如果有范围“参数化”,那将起作用。 “function”范围在每个测试用例运行之前/之后运行,“module”范围在整个模块/文件运行之前/之后运行,“session”范围用于在整个会话运行之前/之后运行。我需要的只是参数化之前/之后
-
parametrization甚至是有效范围吗? -
不是。我只是举了一个例子来说明我想在这里取得成功。因此,与其他夹具范围类似的行为。我已经对其进行了一些解决方法,以便为我的最后一个配置(列表 [-1])提供一个名为“last_run”且其值设置为“True”的键,并在测试用例中检查此类键的存在,如果'last_run' 是 True....
-
您可以做的是在钩子中实现您的自定义逻辑:
pytest_sessionstart在测试收集(以及参数化)运行之前调用,pytest_runtestloop在测试收集完成后调用(并且所有测试都是参数化的),但在实际测试执行开始之前。
标签: python parameters automated-tests pytest fixtures