参数化固定装置不适用于 Pytest
Parameterizing fixtures not working on Pytest
我已经在 fixtures
(@pytest.fixture(params=params)
) 上使用 params
到 运行 具有多个测试用例的相同测试。在这种情况下,我想我做的完全一样,但这次夹具没有 return 任何东西。奇怪的是,测试 运行 是我传递给函数的参数数量的两倍。
所以,我的示例代码:
def double(n):
return 2 * n
TEST_CASES_DOUBLE = [
#(input, expected)
(1, 2),
(2, 4),
(3, 6),
]
# Fixtures
@pytest.fixture(params=TEST_CASES_DOUBLE)
def params_function_double(request):
request.param
# Tests
def test_double(params_function_double):
param, expected = params_function_double
result = double(param)
assert result == expected
我收到错误:
======================================================================================= FAILURES ========================================================================================
_________________________________________________________________________ test_double[params_function_double0] __________________________________________________________________________
params_function_double = None
def test_double(params_function_double):
> param, expected = params_function_double
E TypeError: cannot unpack non-iterable NoneType object
test.py:56: TypeError
_________________________________________________________________________ test_double[params_function_double1] __________________________________________________________________________
params_function_double = None
def test_double(params_function_double):
> param, expected = params_function_double
E TypeError: cannot unpack non-iterable NoneType object
test.py:56: TypeError
_________________________________________________________________________ test_double[params_function_double2] __________________________________________________________________________
params_function_double = None
def test_double(params_function_double):
> param, expected = params_function_double
E TypeError: cannot unpack non-iterable NoneType object
test.py:56: TypeError
================================================================================ short test summary info ================================================================================
FAILED test.py::test_double[params_function_double0] - TypeError: cannot unpack non-iterable NoneType object
FAILED test.py::test_double[params_function_double1] - TypeError: cannot unpack non-iterable NoneType object
FAILED test.py::test_double[params_function_double2] - TypeError: cannot unpack non-iterable NoneType object
主要问题是您的 params_function_double
没有返回任何内容,因此您会收到 NoneType 错误。
您可能正在寻找的不是 fixture 装饰器,而是 pytest.mark.parametrize
装饰器(参见 this page)。这是测试实现的样子
def double(n):
return 2 * n
# Tests
@pytest.mark.parametrize(
"input_, expected",
[(1, 2), (2, 4), (3, 6)]
)
def test_double(input_, expected):
result = double(input_)
assert result == expected
我已经在 fixtures
(@pytest.fixture(params=params)
) 上使用 params
到 运行 具有多个测试用例的相同测试。在这种情况下,我想我做的完全一样,但这次夹具没有 return 任何东西。奇怪的是,测试 运行 是我传递给函数的参数数量的两倍。
所以,我的示例代码:
def double(n):
return 2 * n
TEST_CASES_DOUBLE = [
#(input, expected)
(1, 2),
(2, 4),
(3, 6),
]
# Fixtures
@pytest.fixture(params=TEST_CASES_DOUBLE)
def params_function_double(request):
request.param
# Tests
def test_double(params_function_double):
param, expected = params_function_double
result = double(param)
assert result == expected
我收到错误:
======================================================================================= FAILURES ========================================================================================
_________________________________________________________________________ test_double[params_function_double0] __________________________________________________________________________
params_function_double = None
def test_double(params_function_double):
> param, expected = params_function_double
E TypeError: cannot unpack non-iterable NoneType object
test.py:56: TypeError
_________________________________________________________________________ test_double[params_function_double1] __________________________________________________________________________
params_function_double = None
def test_double(params_function_double):
> param, expected = params_function_double
E TypeError: cannot unpack non-iterable NoneType object
test.py:56: TypeError
_________________________________________________________________________ test_double[params_function_double2] __________________________________________________________________________
params_function_double = None
def test_double(params_function_double):
> param, expected = params_function_double
E TypeError: cannot unpack non-iterable NoneType object
test.py:56: TypeError
================================================================================ short test summary info ================================================================================
FAILED test.py::test_double[params_function_double0] - TypeError: cannot unpack non-iterable NoneType object
FAILED test.py::test_double[params_function_double1] - TypeError: cannot unpack non-iterable NoneType object
FAILED test.py::test_double[params_function_double2] - TypeError: cannot unpack non-iterable NoneType object
主要问题是您的 params_function_double
没有返回任何内容,因此您会收到 NoneType 错误。
您可能正在寻找的不是 fixture 装饰器,而是 pytest.mark.parametrize
装饰器(参见 this page)。这是测试实现的样子
def double(n):
return 2 * n
# Tests
@pytest.mark.parametrize(
"input_, expected",
[(1, 2), (2, 4), (3, 6)]
)
def test_double(input_, expected):
result = double(input_)
assert result == expected