Skip to content

Commit d911bfc

Browse files
committed
Merge branch 'issue634-scopes' of https://github.com/Stranger6667/pytest
# Conflicts: # CHANGELOG.rst
2 parents 8f29ce2 + d72afe7 commit d911bfc

File tree

3 files changed

+57
-2
lines changed

3 files changed

+57
-2
lines changed

CHANGELOG.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,18 @@
5858
* Fixed collection of classes with custom ``__new__`` method.
5959
Fixes `#1579`_. Thanks to `@Stranger6667`_ for the PR.
6060

61+
* Fixed scope overriding inside metafunc.parametrize (`#634`_).
62+
Thanks to `@Stranger6667`_ for the PR.
63+
64+
*
65+
66+
*
67+
68+
*
69+
70+
*
71+
72+
.. _#634: https://github.com/pytest-dev/pytest/issues/634
6173
.. _#717: https://github.com/pytest-dev/pytest/issues/717
6274
.. _#1579: https://github.com/pytest-dev/pytest/issues/1579
6375
.. _#1580: https://github.com/pytest-dev/pytest/pull/1580

_pytest/python.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,9 +1005,15 @@ def parametrize(self, argnames, argvalues, indirect=False, ids=None,
10051005
newmarks = newkeywords.setdefault(0, {})
10061006
newmarks[newmark.markname] = newmark
10071007

1008-
10091008
if scope is None:
1010-
scope = "function"
1009+
if self._arg2fixturedefs:
1010+
# Takes the most narrow scope from used fixtures
1011+
fixtures_scopes = [fixturedef[0].scope for fixturedef in self._arg2fixturedefs.values()]
1012+
for scope in reversed(scopes):
1013+
if scope in fixtures_scopes:
1014+
break
1015+
else:
1016+
scope = 'function'
10111017
scopenum = scopes.index(scope)
10121018
valtypes = {}
10131019
for arg in argnames:

testing/python/metafunc.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -819,6 +819,43 @@ def test_checklength():
819819
reprec = testdir.inline_run()
820820
reprec.assertoutcome(passed=5)
821821

822+
def test_parametrize_issue634(self, testdir):
823+
testdir.makepyfile('''
824+
import pytest
825+
826+
@pytest.fixture(scope='module')
827+
def foo(request):
828+
print('preparing foo-%d' % request.param)
829+
return 'foo-%d' % request.param
830+
831+
832+
def test_one(foo):
833+
pass
834+
835+
836+
def test_two(foo):
837+
pass
838+
839+
840+
test_two.test_with = (2, 3)
841+
842+
843+
def pytest_generate_tests(metafunc):
844+
params = (1, 2, 3, 4)
845+
if not 'foo' in metafunc.fixturenames:
846+
return
847+
848+
test_with = getattr(metafunc.function, 'test_with', None)
849+
if test_with:
850+
params = test_with
851+
metafunc.parametrize('foo', params, indirect=True)
852+
853+
''')
854+
result = testdir.runpytest("-s")
855+
output = result.stdout.str()
856+
assert output.count('preparing foo-2') == 1
857+
assert output.count('preparing foo-3') == 1
858+
822859
def test_parametrize_issue323(self, testdir):
823860
testdir.makepyfile("""
824861
import pytest

0 commit comments

Comments
 (0)