假设我有一个这样的参数化 fixture :
@pytest.fixture(params=[1, 2, 800]):
def resource(request):
return Resource(capacity=request.param)
当我在测试函数中使用 fixture 作为参数时,Pytest 对所有三个版本运行测试:
def test_resource(resource): # Runs for capacities 1, 2, and 800.
assert resource.is_okay()
但是,对于某些测试,我想更改构建 fixture 的参数:
def test_worker(resource, worker): # Please run this for capacities 1 and 5.
worker.use(resource)
assert worker.is_okay()
我如何指定只接收特定版本的指定 fixture ?
最佳答案
如果您想对不同的测试使用不同的参数集,那么 pytest.mark.parametrize
会很有帮助。
@pytest.mark.parametrize("resource", [1, 2, 800], indirect=True)
def test_resource(resource):
assert resource.is_okay()
@pytest.mark.parametrize("resource", [1, 5], indirect=True)
def test_resource_other(resource):
assert resource.is_okay()
关于python - 如何在 Pytest 中使用 fixture 的覆盖参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39069551/