繁体   English   中英

使用夹具的Pytest参数化测试

[英]Pytest parametrized test that uses fixture

我有一个测试套件与pytest.fixture依赖于其他附着物,就像这样:

@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 ,它运行良好:

def test_foo(client):
    assert client.method_with_args(arg1, arg2)

但是对于参数化测试,使用该夹具确实很尴尬。 您必须直接调用所有的fixture方法,这在一定程度上违反了此目的。 (我应该注意, 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

有什么方法可以使这种清洁剂更清洁吗? 我不确定这个凌乱的代码是否比重复的类似测试更好。

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

例如

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM