Skip to content

Commit 0606093

Browse files
committed
allow to override parametrized fixtures with non-parametrized ones and vice versa
--HG-- branch : parametrized-fixture-override
1 parent bae0f7f commit 0606093

File tree

5 files changed

+129
-6
lines changed

5 files changed

+129
-6
lines changed

CHANGELOG

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
2.7.0.dev (compared to 2.6.4)
22
-----------------------------
33

4-
- fix issue616: conftest.py files and their contained fixutres are now
4+
- fix issue616: conftest.py files and their contained fixutres are now
55
properly considered for visibility, independently from the exact
66
current working directory and test arguments that are used.
7-
Many thanks to Eric Siegerman and his PR235 which contains
7+
Many thanks to Eric Siegerman and his PR235 which contains
88
systematic tests for conftest visibility and now passes.
99
This change also introduces the concept of a ``rootdir`` which
1010
is printed as a new pytest header and documented in the pytest
1111
customize web page.
1212

1313
- change reporting of "diverted" tests, i.e. tests that are collected
1414
in one file but actually come from another (e.g. when tests in a test class
15-
come from a base class in a different file). We now show the nodeid
15+
come from a base class in a different file). We now show the nodeid
1616
and indicate via a postfix the other file.
1717

1818
- add ability to set command line options by environment variable PYTEST_ADDOPTS.
@@ -24,7 +24,7 @@
2424
- fix issue650: new option ``--docttest-ignore-import-errors`` which
2525
will turn import errors in doctests into skips. Thanks Charles Cloud
2626
for the complete PR.
27-
27+
2828
- fix issue655: work around different ways that cause python2/3
2929
to leak sys.exc_info into fixtures/tests causing failures in 3rd party code
3030

@@ -55,6 +55,7 @@
5555
- "python_classes" and "python_functions" options now support glob-patterns
5656
for test discovery, as discussed in issue600. Thanks Ldiary Translations.
5757

58+
- allow to override parametrized fixtures with non-parametrized ones and vice versa (bubenkoff).
5859

5960
2.6.4
6061
----------

Makefile

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# create virtual environment
2+
PYTHON = python2.7
3+
4+
.env:
5+
virtualenv .env -p $(PYTHON)
6+
7+
# install all needed for development
8+
develop: .env
9+
.env/bin/pip install -e .[test] tox
10+
11+
# clean the development envrironment
12+
clean:
13+
-rm -rf .env

_pytest/python.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1714,7 +1714,7 @@ def pytest_generate_tests(self, metafunc):
17141714
faclist = metafunc._arg2fixturedefs.get(argname)
17151715
if faclist is None:
17161716
continue # will raise FixtureLookupError at setup time
1717-
for fixturedef in faclist:
1717+
for fixturedef in faclist[-1:]:
17181718
if fixturedef.params is not None:
17191719
metafunc.parametrize(argname, fixturedef.params,
17201720
indirect=True, scope=fixturedef.scope,

setup.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
('Programming Language :: Python :: %s' % x) for x in
1414
'2 2.6 2.7 3 3.2 3.3 3.4'.split()]
1515

16-
long_description = open('README.rst').read()
16+
with open('README.rst') as fd:
17+
long_description = fd.read()
1718

1819

1920
def main():

testing/python/fixture.py

+108
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,114 @@ def test_foo(foo):
226226
result = testdir.runpytest()
227227
assert result.ret == 0
228228

229+
def test_override_parametrized_fixture_conftest_module(self, testdir):
230+
"""Test override of the parametrized fixture with non-parametrized one on the test module level."""
231+
testdir.makeconftest("""
232+
import pytest
233+
234+
@pytest.fixture(params=[1, 2, 3])
235+
def spam(request):
236+
return request.param
237+
""")
238+
testfile = testdir.makepyfile("""
239+
import pytest
240+
241+
@pytest.fixture
242+
def spam():
243+
return 'spam'
244+
245+
def test_spam(spam):
246+
assert spam == 'spam'
247+
""")
248+
result = testdir.runpytest()
249+
result.stdout.fnmatch_lines(["*1 passed*"])
250+
result = testdir.runpytest(testfile)
251+
result.stdout.fnmatch_lines(["*1 passed*"])
252+
253+
def test_override_parametrized_fixture_conftest_conftest(self, testdir):
254+
"""Test override of the parametrized fixture with non-parametrized one on the conftest level."""
255+
testdir.makeconftest("""
256+
import pytest
257+
258+
@pytest.fixture(params=[1, 2, 3])
259+
def spam(request):
260+
return request.param
261+
""")
262+
subdir = testdir.mkpydir('subdir')
263+
subdir.join("conftest.py").write(py.code.Source("""
264+
import pytest
265+
266+
@pytest.fixture
267+
def spam():
268+
return 'spam'
269+
"""))
270+
testfile = subdir.join("test_spam.py")
271+
testfile.write(py.code.Source("""
272+
def test_spam(spam):
273+
assert spam == "spam"
274+
"""))
275+
result = testdir.runpytest()
276+
result.stdout.fnmatch_lines(["*1 passed*"])
277+
result = testdir.runpytest(testfile)
278+
result.stdout.fnmatch_lines(["*1 passed*"])
279+
280+
def test_override_non_parametrized_fixture_conftest_module(self, testdir):
281+
"""Test override of the non-parametrized fixture with parametrized one on the test module level."""
282+
testdir.makeconftest("""
283+
import pytest
284+
285+
@pytest.fixture
286+
def spam():
287+
return 'spam'
288+
""")
289+
testfile = testdir.makepyfile("""
290+
import pytest
291+
292+
@pytest.fixture(params=[1, 2, 3])
293+
def spam(request):
294+
return request.param
295+
296+
params = {'spam': 1}
297+
298+
def test_spam(spam):
299+
assert spam == params['spam']
300+
params['spam'] += 1
301+
""")
302+
result = testdir.runpytest()
303+
result.stdout.fnmatch_lines(["*3 passed*"])
304+
result = testdir.runpytest(testfile)
305+
result.stdout.fnmatch_lines(["*3 passed*"])
306+
307+
def test_override_non_parametrized_fixture_conftest_conftest(self, testdir):
308+
"""Test override of the non-parametrized fixture with parametrized one on the conftest level."""
309+
testdir.makeconftest("""
310+
import pytest
311+
312+
@pytest.fixture
313+
def spam():
314+
return 'spam'
315+
""")
316+
subdir = testdir.mkpydir('subdir')
317+
subdir.join("conftest.py").write(py.code.Source("""
318+
import pytest
319+
320+
@pytest.fixture(params=[1, 2, 3])
321+
def spam(request):
322+
return request.param
323+
"""))
324+
testfile = subdir.join("test_spam.py")
325+
testfile.write(py.code.Source("""
326+
params = {'spam': 1}
327+
328+
def test_spam(spam):
329+
assert spam == params['spam']
330+
params['spam'] += 1
331+
"""))
332+
result = testdir.runpytest()
333+
result.stdout.fnmatch_lines(["*3 passed*"])
334+
result = testdir.runpytest(testfile)
335+
result.stdout.fnmatch_lines(["*3 passed*"])
336+
229337
def test_autouse_fixture_plugin(self, testdir):
230338
# A fixture from a plugin has no baseid set, which screwed up
231339
# the autouse fixture handling.

0 commit comments

Comments
 (0)