【发布时间】:2019-02-09 03:44:04
【问题描述】:
我正在尝试参数化从 conftest.py 中的命令行选项。
#!/usr/bin/env python
import pytest
import test
def pytest_addoption(parser):
parser.addoption("--low", action="store", type=int, help="low")
parser.addoption("--high", action="store",type=int, help="high")
@pytest.fixture(scope="session", autouse=True)
def user(request):
return request.config.getoption("low")
@pytest.fixture(scope="session", autouse=True)
def rang(request):
return request.config.getoption("high")
#test_file.py
def data(low, high):
return list(range(low, high))
@pytest.mark.parametrize("num", data(10, 20))
def test(num):
assert num < 1000
我想运行类似“pytest --low=10 --high=100 test_file.py”的命令。对于 x 和 y 之间的值范围,代码与 @pytest.mark.parametrize("num", data(x, y)) 一起工作正常。除了低和高之外,我不想提供任何参数化值。如果我编写 @pytest.mark.parametrize("num", data(low, high)) 之类的代码,则会引发错误。有什么办法可以让这个参数化工作?我知道当我们在方法之外生成列表时代码有效。但我想编写一个生成列表的方法,并在参数化中使用该列表。
有什么方法可以在 test_file.py 的任何地方访问这些低和高 cmdline 选项?
【问题讨论】:
标签: python-3.x pytest