使用函数参数化 pytest
Parameterize a pytest with functions
我有许多不同的自定义转换器函数,并且想 运行 对所有这些函数进行简单测试(例如打印出转换器)。我可以为每个函数编写单独的测试,但认为一定有更简单的方法。
我看过参数化测试但得到:TypeError: 'function' object is not iterable
@pytest.fixture
def list_transformers(self):
list_transformers = [
TransformerOne(column='a'),
TransformerTwo(column='a')
]
return list_trasformers
@pytest.mark.parametrize("transformer", list_transformers)
def test_print(self, transformer):
tf_type = transformer[0]
params = transformer[1]
tf = tf_type(**params)
print(tf)
我认为这是因为转换器函数没有返回可迭代的东西。我应该以不同的方式解决这个问题吗?
为什么不直接将列表传递给装饰器:
@pytest.mark.parametrize("transformer", [TransformerOne(column='a'), TransformerTwo(column='a')])
def test_print(self, transformer):
...
Using fixtures in pytest.mark.parametrize 尚未得到完全支持。
除了使用 @pytest.mark.parametrize
,您还可以使用另一种类型的参数化测试,即通过 pytest_generate_tests.
import pytest
def pytest_generate_tests(metafunc):
if "transformer" in metafunc.fixturenames:
list_transformers = [
'This is a',
'While that is b',
]
metafunc.parametrize("transformer", list_transformers)
def test_print(transformer):
print(transformer)
输出:
$ pytest -q -rP
.. [100%]
================================================================================================= PASSES ==================================================================================================
__________________________________________________________________________________________ test_print[This is a] __________________________________________________________________________________________
------------------------------------------------------------------------------------------ Captured stdout call -------------------------------------------------------------------------------------------
This is a
_______________________________________________________________________________________ test_print[While that is b] _______________________________________________________________________________________
------------------------------------------------------------------------------------------ Captured stdout call -------------------------------------------------------------------------------------------
While that is b
2 passed in 0.07s
pytest.mark.parametrize()
的第二个参数必须是迭代器而不是函数引用。
像这样:
@pytest.mark.parametrize("transformer", list_transformers())
或
@pytest.mark.parametrize("transformer", [TransformerOne(column='a'), TransformerTwo(column='a')])
我有许多不同的自定义转换器函数,并且想 运行 对所有这些函数进行简单测试(例如打印出转换器)。我可以为每个函数编写单独的测试,但认为一定有更简单的方法。
我看过参数化测试但得到:TypeError: 'function' object is not iterable
@pytest.fixture
def list_transformers(self):
list_transformers = [
TransformerOne(column='a'),
TransformerTwo(column='a')
]
return list_trasformers
@pytest.mark.parametrize("transformer", list_transformers)
def test_print(self, transformer):
tf_type = transformer[0]
params = transformer[1]
tf = tf_type(**params)
print(tf)
我认为这是因为转换器函数没有返回可迭代的东西。我应该以不同的方式解决这个问题吗?
为什么不直接将列表传递给装饰器:
@pytest.mark.parametrize("transformer", [TransformerOne(column='a'), TransformerTwo(column='a')])
def test_print(self, transformer):
...
Using fixtures in pytest.mark.parametrize 尚未得到完全支持。
除了使用 @pytest.mark.parametrize
,您还可以使用另一种类型的参数化测试,即通过 pytest_generate_tests.
import pytest
def pytest_generate_tests(metafunc):
if "transformer" in metafunc.fixturenames:
list_transformers = [
'This is a',
'While that is b',
]
metafunc.parametrize("transformer", list_transformers)
def test_print(transformer):
print(transformer)
输出:
$ pytest -q -rP
.. [100%]
================================================================================================= PASSES ==================================================================================================
__________________________________________________________________________________________ test_print[This is a] __________________________________________________________________________________________
------------------------------------------------------------------------------------------ Captured stdout call -------------------------------------------------------------------------------------------
This is a
_______________________________________________________________________________________ test_print[While that is b] _______________________________________________________________________________________
------------------------------------------------------------------------------------------ Captured stdout call -------------------------------------------------------------------------------------------
While that is b
2 passed in 0.07s
pytest.mark.parametrize()
的第二个参数必须是迭代器而不是函数引用。
像这样:
@pytest.mark.parametrize("transformer", list_transformers())
或
@pytest.mark.parametrize("transformer", [TransformerOne(column='a'), TransformerTwo(column='a')])