双参数化 [英] Double parameterization

查看:42
本文介绍了双参数化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一组测试,我想测试一个解决方案的多个版本.目前我有

So I have a set of tests where I'd like to test multiple versions of a solution. Currently I have

import pytest

import product_not_at_index


functions_to_test = [
    product_not_at_index.product_not_at_index_n_squared,
    product_not_at_index.product_not_at_index,
]

def run_test(function_input, expected_result, test_func):
    actual_result = test_func(function_input)
    assert actual_result == expected_result

@pytest.mark.parametrize("test_func", functions_to_test)
def test_empty_list(test_func):
    input_data = []
    expected_result = []
    run_test(input_data, expected_result, test_func)

@pytest.mark.parametrize("test_func", functions_to_test)
def test_single_item(test_func):
    input_data = [1]
    expected_result = [1]
    run_test(input_data, expected_result, test_func)

@pytest.mark.parametrize("test_func", functions_to_test)
def test_one_times_one(test_func):
    input_data = [1, 1]
    expected_result = [1, 1]
    run_test(input_data, expected_result, test_func)

@pytest.mark.parametrize("test_func", functions_to_test)
def test_normal_use_case(test_func):
    input_data = [1, 7, 3, 4]
    expected_result = [84, 12, 28, 21]
    run_test(input_data, expected_result, test_func)

这很好用.但是查看我的解决方案,我发现我的所有测试都具有相同的基本代码集.我怎样才能两次参数化一个函数,这样我就可以只有一个测试函数而不再重复自己?

And this works great. But looking at my solution I see that all of my tests have the same basic set of code. How can I parameterize a function twice so that I can just have a single test function and stop repeating myself?

我认为我可以做类似的事情

I thought that I could do something like

import pytest

import product_not_at_index


functions_to_test = [product_not_at_index.product_not_at_index_n_squared]
test_data = [
    [], [],
    [1], [1],
    [1, 1], [1, 1],
    [1, 7, 3, 4],  [84, 12, 28, 21],
]

@pytest.mark.parametrize("function_input,expected_result", test_data)
@pytest.mark.parametrize("test_func", functions_to_test)
def test_run(function_input, expected_result, test_func):
    actual_result = test_func(function_input)
    assert actual_result == expected_result

但这只会返回这个错误

E   assert 0 == 2
E    +  where 0 = len([])
E    +  and   2 = len(['function_input', 'expected_result'])

推荐答案

我最终使用的解决方案是这个

The solution I ended up using is this one

import pytest

import product_not_at_index


functions_to_test = [product_not_at_index.product_not_at_index_n_squared]
test_data = [
    ([], []),
    ([1], [1]),
    ([1, 1], [1, 1]),
    ([1, 7, 3, 4],  [84, 12, 28, 21]),
]

# TODO: turn into a list comprehension.
test_paramaters = []
for func in functions_to_test:
    for test_input, expected_result in test_data:
        test_paramaters.append([test_input, expected_result, func])

@pytest.mark.parametrize("function_input,expected_result,test_func", test_paramaters)
def test_run(function_input, expected_result, test_func):
    actual_result = test_func(function_input)
    assert actual_result == expected_result

这篇关于双参数化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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