我有很多通过pytest管理的参数化的固定装置。
有时,我希望使用夹具的测试不必担心参数的应用。
是否可以制作一个参数化另一个夹具的夹具?
import pytest
class Foo:
def __init__(self, a: int, b: int):
pass
@pytest.fixture
def foo(a: int, b: int) -> Foo:
return Foo(a, b)
@pytest.fixture
@pytest.mark.parametrize("a, b", [(2, 3)]) # How can I do this?
def fixture_parametrizing_another_fixture(foo: Foo) -> Foo:
return foo
# I don't want to parametrize here, I want the fixture already set up
def test_with_second_fixture(fixture_parametrizing_another_fixture: Foo):
pass发布于 2021-01-21 17:55:10
我不认为你可以用你想做的方式来做这件事,但也许结合正常函数使用夹具参数就足够了,例如:
...
def foo(a: int, b: int) -> Foo:
return Foo(a, b)
@pytest.fixture(params=[(3, 2)])
def parametrized_fixture1(request) -> Foo:
yield foo(request.param[0], request.param[1])
@pytest.fixture(params=[(5, 6), (7, 8)])
def parametrized_fixture2(request) -> Foo:
yield foo(request.param[0], request.param[1])
def test_with_second_fixture1(parametrized_fixture1: Foo):
# one test with (3,2)
pass
def test_with_second_fixture2(parametrized_fixture2: Foo):
# two tests
pass当然,只有当您想要在多个测试中使用相同的参数时,这才有意义。
https://stackoverflow.com/questions/65819690
复制相似问题