2017-09-15 89 views
1

我有一个测试套件与pytest.fixture依赖于其他附着物,像这样的测试:Pytest参数化使用夹具

@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) 

对于一个正常的测试,我只是通过在client和正常工作:

但是对于参数化测试,使用该夹具非常尴尬。你必须直接调用所有的夹具方法,这在某种程度上破坏了目的。 (我应该注意,paramsconfig固定装置在其他地方使用,所以我不想把它们折成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 

有什么办法可以让这个更清洁吗?我不确定这个混乱的代码比重复的类似测试更好。

回答

1

如何参数化参数而不是方法调用的结果?

例如

@pytest.mark.parametrize('args,expected', [ 
    ((arg1, arg2), 100), 
    ((arg2, arg4), 200), 
]) 
def test_parameters(client, args, expected): 
    assert client.method_with_args(*args) == expected