Fix `pytest.mark.parametrize` when the argvalue is an iterator

This commit is contained in:
Anthony Sottile 2019-06-01 13:50:15 -07:00
parent 917195ea8e
commit cafb13c95f
3 changed files with 30 additions and 3 deletions

View File

@ -0,0 +1 @@
Fix ``pytest.mark.parametrize`` when the argvalues is an iterator.

View File

@ -113,14 +113,18 @@ class ParameterSet(namedtuple("ParameterSet", "values, marks, id")):
force_tuple = len(argnames) == 1 force_tuple = len(argnames) == 1
else: else:
force_tuple = False force_tuple = False
parameters = [ return argnames, force_tuple
@staticmethod
def _parse_parametrize_parameters(argvalues, force_tuple):
return [
ParameterSet.extract_from(x, force_tuple=force_tuple) for x in argvalues ParameterSet.extract_from(x, force_tuple=force_tuple) for x in argvalues
] ]
return argnames, parameters
@classmethod @classmethod
def _for_parametrize(cls, argnames, argvalues, func, config, function_definition): def _for_parametrize(cls, argnames, argvalues, func, config, function_definition):
argnames, parameters = cls._parse_parametrize_args(argnames, argvalues) argnames, force_tuple = cls._parse_parametrize_args(argnames, argvalues)
parameters = cls._parse_parametrize_parameters(argvalues, force_tuple)
del argvalues del argvalues
if parameters: if parameters:

View File

@ -413,6 +413,28 @@ def test_parametrized_with_kwargs(testdir):
assert result.ret == 0 assert result.ret == 0
def test_parametrize_iterator(testdir):
"""parametrize should work with generators (#5354)."""
py_file = testdir.makepyfile(
"""\
import pytest
def gen():
yield 1
yield 2
yield 3
@pytest.mark.parametrize('a', gen())
def test(a):
assert a >= 1
"""
)
result = testdir.runpytest(py_file)
assert result.ret == 0
# should not skip any tests
result.stdout.fnmatch_lines(["*3 passed*"])
class TestFunctional(object): class TestFunctional(object):
def test_merging_markers_deep(self, testdir): def test_merging_markers_deep(self, testdir):
# issue 199 - propagate markers into nested classes # issue 199 - propagate markers into nested classes