Reputation: 3317
I have a test suite with a pytest.fixture
that depends on other fixtures, like this:
@pytest.fixture
def params():
return {'foo': 'bar', 'baz': 1}
@pytest.fixture
def config():
return ['foo', 'bar', 'baz']
@pytest.client
def client(params, config):
return MockClient(params, config)
For a normal test, I just pass in client
and it works fine:
def test_foo(client):
assert client.method_with_args(arg1, arg2)
But for a parametrized test, using that fixture is really awkward. You have to invoke all the fixture methods directly, which defeats the purpose to some extent. (I should note that the params
and config
fixtures are used elsewhere, so I don't want to just collapse them into client
).
@pytest.mark.parametrize('thing,expected', [
(client(params(), config()).method_with_args(arg1, arg2), 100),
(client(params(), config()).method_with_args(arg2, arg4), 200),
])
def test_parameters(thing, expected):
assert thing == expected
Is there any way to make this cleaner? I'm not sure this messy code is any better than repeated, similar tests.
Upvotes: 1
Views: 376
Reputation: 9056
How about parametrizing the arguments instead of the result of the method call?
e.g.
@pytest.mark.parametrize('args,expected', [
((arg1, arg2), 100),
((arg2, arg4), 200),
])
def test_parameters(client, args, expected):
assert client.method_with_args(*args) == expected
Upvotes: 2