Fixed scope override inside metafunc.parametrize. Fixes #634
This commit is contained in:
parent
a2b04d02c2
commit
d72afe7e08
|
@ -54,6 +54,10 @@
|
||||||
* Fixed collection of classes with custom ``__new__`` method.
|
* Fixed collection of classes with custom ``__new__`` method.
|
||||||
Fixes `#1579`_. Thanks to `@Stranger6667`_ for the PR.
|
Fixes `#1579`_. Thanks to `@Stranger6667`_ for the PR.
|
||||||
|
|
||||||
|
* Fixed scope overriding inside metafunc.parametrize (`#634`_).
|
||||||
|
Thanks to `@Stranger6667`_ for the PR.
|
||||||
|
|
||||||
|
.. _#634: https://github.com/pytest-dev/pytest/issues/634
|
||||||
.. _#1579: https://github.com/pytest-dev/pytest/issues/1579
|
.. _#1579: https://github.com/pytest-dev/pytest/issues/1579
|
||||||
.. _#1580: https://github.com/pytest-dev/pytest/pull/1580
|
.. _#1580: https://github.com/pytest-dev/pytest/pull/1580
|
||||||
.. _#1605: https://github.com/pytest-dev/pytest/issues/1605
|
.. _#1605: https://github.com/pytest-dev/pytest/issues/1605
|
||||||
|
|
|
@ -1005,9 +1005,15 @@ class Metafunc(FuncargnamesCompatAttr):
|
||||||
newmarks = newkeywords.setdefault(0, {})
|
newmarks = newkeywords.setdefault(0, {})
|
||||||
newmarks[newmark.markname] = newmark
|
newmarks[newmark.markname] = newmark
|
||||||
|
|
||||||
|
|
||||||
if scope is None:
|
if scope is None:
|
||||||
scope = "function"
|
if self._arg2fixturedefs:
|
||||||
|
# Takes the most narrow scope from used fixtures
|
||||||
|
fixtures_scopes = [fixturedef[0].scope for fixturedef in self._arg2fixturedefs.values()]
|
||||||
|
for scope in reversed(scopes):
|
||||||
|
if scope in fixtures_scopes:
|
||||||
|
break
|
||||||
|
else:
|
||||||
|
scope = 'function'
|
||||||
scopenum = scopes.index(scope)
|
scopenum = scopes.index(scope)
|
||||||
valtypes = {}
|
valtypes = {}
|
||||||
for arg in argnames:
|
for arg in argnames:
|
||||||
|
|
|
@ -819,6 +819,43 @@ class TestMetafuncFunctional:
|
||||||
reprec = testdir.inline_run()
|
reprec = testdir.inline_run()
|
||||||
reprec.assertoutcome(passed=5)
|
reprec.assertoutcome(passed=5)
|
||||||
|
|
||||||
|
def test_parametrize_issue634(self, testdir):
|
||||||
|
testdir.makepyfile('''
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
@pytest.fixture(scope='module')
|
||||||
|
def foo(request):
|
||||||
|
print('preparing foo-%d' % request.param)
|
||||||
|
return 'foo-%d' % request.param
|
||||||
|
|
||||||
|
|
||||||
|
def test_one(foo):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
def test_two(foo):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
test_two.test_with = (2, 3)
|
||||||
|
|
||||||
|
|
||||||
|
def pytest_generate_tests(metafunc):
|
||||||
|
params = (1, 2, 3, 4)
|
||||||
|
if not 'foo' in metafunc.fixturenames:
|
||||||
|
return
|
||||||
|
|
||||||
|
test_with = getattr(metafunc.function, 'test_with', None)
|
||||||
|
if test_with:
|
||||||
|
params = test_with
|
||||||
|
metafunc.parametrize('foo', params, indirect=True)
|
||||||
|
|
||||||
|
''')
|
||||||
|
result = testdir.runpytest("-s")
|
||||||
|
output = result.stdout.str()
|
||||||
|
assert output.count('preparing foo-2') == 1
|
||||||
|
assert output.count('preparing foo-3') == 1
|
||||||
|
|
||||||
def test_parametrize_issue323(self, testdir):
|
def test_parametrize_issue323(self, testdir):
|
||||||
testdir.makepyfile("""
|
testdir.makepyfile("""
|
||||||
import pytest
|
import pytest
|
||||||
|
|
Loading…
Reference in New Issue