diff --git a/CHANGELOG b/CHANGELOG index 1707b62e4..3ba08a485 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -64,6 +64,9 @@ Unreleased - fix issue380 by making --resultlog only rely on longrepr instead of the "reprcrash" attribute which only exists sometimes. +- address issue122: allow @pytest.fixture(params=iterator) by exploding + into a list early on. + - fix pexpect-3.0 compatibility for pytest's own tests. (fixes issue386) diff --git a/_pytest/python.py b/_pytest/python.py index 8085ac238..d4ef14180 100644 --- a/_pytest/python.py +++ b/_pytest/python.py @@ -76,8 +76,9 @@ def fixture(scope="function", params=None, autouse=False): # direct decoration return FixtureFunctionMarker( "function", params, autouse)(scope) - else: - return FixtureFunctionMarker(scope, params, autouse) + if params is not None and not isinstance(params, (list, tuple)): + params = list(params) + return FixtureFunctionMarker(scope, params, autouse) def yield_fixture(scope="function", params=None, autouse=False): """ (return a) decorator to mark a yield-fixture factory function diff --git a/testing/python/fixture.py b/testing/python/fixture.py index ac6258fea..60bbba342 100644 --- a/testing/python/fixture.py +++ b/testing/python/fixture.py @@ -914,6 +914,34 @@ class TestFixtureUsages: reprec = testdir.inline_run() reprec.assertoutcome(passed=1) + def test_fixture_parametrized_with_iterator(self, testdir): + testdir.makepyfile(""" + import pytest + + l = [] + def f(): + yield 1 + yield 2 + dec = pytest.fixture(scope="module", params=f()) + + @dec + def arg(request): + return request.param + @dec + def arg2(request): + return request.param + + def test_1(arg): + l.append(arg) + def test_2(arg2): + l.append(arg2*10) + """) + reprec = testdir.inline_run("-v") + reprec.assertoutcome(passed=4) + l = reprec.getcalls("pytest_runtest_call")[0].item.module.l + assert l == [1,2, 10,20] + + class TestFixtureManagerParseFactories: def pytest_funcarg__testdir(self, request): testdir = request.getfuncargvalue("testdir")