测试.使用堆叠参数化装饰器时定义预期结果的最佳方法? [英] Pytest. Best way to define expected result when using stacked parametrize decorators?

查看:43
本文介绍了测试.使用堆叠参数化装饰器时定义预期结果的最佳方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类似的测试用例,每个测试用例都有超过 100 行的代码,它们为测试创建辅助对象,这些行非常相似,实际上,测试设置之间只有 2 行不同,我不想摆脱代码重复.我认为多重参数化可能会帮助我完成这项任务.使用多个参数化,我可以将设置组合在一起,实际上为测试提供更好的条件.Bun 当每个组合都会产生不同的结果时,我无法确定在使用多个参数化时指定预期结果的最佳方法是什么?

I Have two similar testcases with each having more than 100 lines of code which creates aux objects for the test, those lines are very similar, in fact, only 2 lines differ between the tests setups and I wan't to get rid of the code repetition. I think that multiple parameterization might help me with this task. Using multiple parametrization I can combine setups in the one and actually provide better conditions for the test. Bun I can't wrap my head around of what is the best way to specify the expected result while using multiple parametrization when each combination will yield different results?

考虑这个测试用例

@pytest.mark.parametrize('country', ['US', 'DE', 'FR', 'IT'])
@pytest.mark.parametrize('number', ['12345', '54321'])
def test_correct_record_is_selected_for_number(country, number):
    # 100 line long setup of different objects.
    record = get_record(country, number)
    assert record = expected_record

我希望对于 (country, number) get_record 函数的不同组合将返回不同的结果,我认为提供预期结果的唯一方法是在测试本身中重新实现 get_record 函数逻辑的一部分,以便根据提供的 countrynumber 确定期望的结果,这对我来说似乎不合适.

I expect that for a different combinations of (country, number) get_record function will return different results, and the only way I see to provide the expected result - is to reimplement the part of the get_record function logic in the test itself in order to determine which result to expect based on provided country and number, which doesn't seem right to me.

有没有办法为堆叠的 parametrize 装饰器很好地指定预期输出?或者我最好将设置代码移动到多个不同的装置,并将其保留为使用不同设置装置的 2 个独立测试?

Is there a way to nicely specify expected output for stacked parametrize decorators? Or I'm better off with moving setup code to multiple different fixtures and leaving it as 2 separate tests that use different setup fixtures?

推荐答案

通常我将结果放在一个单独的夹具中,该夹具根据其他参数选择预期值.示例:

Usually I put the results in a separate fixture that selects the expected value based on other arguments. Example:

expected_records = {'US': {'12345': 'fizz', '54321': 'buzz'}, 'DE': ...}


@pytest.fixture
def expected_record(request):
    country = request.node.funcargs['country']
    number = request.node.funcargs['number']
    return expected_records.get(country, dict()).get(number, None)


@pytest.mark.parametrize('country', ['US', 'DE', 'FR', 'IT'])
@pytest.mark.parametrize('number', ['12345', '54321'])
def test_correct_record_is_selected_for_number(country, number, expected_record):
    record = get_record(country, number)
    assert record == expected_record

然而,在实践中很少将数据硬编码在脚本中,因此expected_record的逻辑通常是数据驱动的,例如:

However, in practice the data is rarely hardcoded in the script, so the logic of expected_record is usually data-driven, for example:

@pytest.fixture
def expected_record(request):
    country = request.node.funcargs['country']
    number = request.node.funcargs['number']
    file = pathlib.Path(request.config.rootdir, 'data', country).with_suffix('.json') # data/DE.json
    data = json.loads(file.read_text())  # {12345: "fizz", 54321: "buzz"}
    return data[number]

这篇关于测试.使用堆叠参数化装饰器时定义预期结果的最佳方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆