Merge pull request #867 from nicoddemus/autouse-yield-fixture-class

Fix autouse fixtures defined in a TestCase subclass
This commit is contained in:
Floris Bruynooghe 2015-07-22 01:25:21 +02:00
commit 0e26de2218
3 changed files with 26 additions and 13 deletions

View File

@ -12,6 +12,10 @@
- preserve warning functions after call to pytest.deprecated_call. Thanks
Pieter Mulder for PR.
- fix issue854: autouse yield_fixtures defined as class members of
unittest.TestCase subclasses now work as expected.
Thannks xmo-odoo for the report and Bruno Oliveira for the PR.
- fix issue833: --fixtures now shows all fixtures of collected test files, instead of just the
fixtures declared on the first one.
Thanks Florian Bruhin for reporting and Bruno Oliveira for the PR.

View File

@ -1900,10 +1900,13 @@ class FixtureDef:
self.finish()
assert not hasattr(self, "cached_result")
fixturefunc = self.func
if self.unittest:
result = self.func(request.instance, **kwargs)
if request.instance is not None:
# bind the unbound method to the TestCase instance
fixturefunc = self.func.__get__(request.instance)
else:
fixturefunc = self.func
# the fixture function needs to be bound to the actual
# request.instance so that code working with "self" behaves
# as expected.
@ -1911,12 +1914,13 @@ class FixtureDef:
fixturefunc = getimfunc(self.func)
if fixturefunc != self.func:
fixturefunc = fixturefunc.__get__(request.instance)
try:
result = call_fixture_func(fixturefunc, request, kwargs,
self.yieldctx)
except Exception:
self.cached_result = (None, my_cache_key, sys.exc_info())
raise
try:
result = call_fixture_func(fixturefunc, request, kwargs,
self.yieldctx)
except Exception:
self.cached_result = (None, my_cache_key, sys.exc_info())
raise
self.cached_result = (result, my_cache_key, None)
return result

View File

@ -607,18 +607,23 @@ def test_unittest_unexpected_failure(testdir):
])
def test_unittest_setup_interaction(testdir):
@pytest.mark.parametrize('fix_type, stmt', [
('fixture', 'return'),
('yield_fixture', 'yield'),
])
def test_unittest_setup_interaction(testdir, fix_type, stmt):
testdir.makepyfile("""
import unittest
import pytest
class MyTestCase(unittest.TestCase):
@pytest.fixture(scope="class", autouse=True)
@pytest.{fix_type}(scope="class", autouse=True)
def perclass(self, request):
request.cls.hello = "world"
@pytest.fixture(scope="function", autouse=True)
{stmt}
@pytest.{fix_type}(scope="function", autouse=True)
def perfunction(self, request):
request.instance.funcname = request.function.__name__
{stmt}
def test_method1(self):
assert self.funcname == "test_method1"
@ -629,7 +634,7 @@ def test_unittest_setup_interaction(testdir):
def test_classattr(self):
assert self.__class__.hello == "world"
""")
""".format(fix_type=fix_type, stmt=stmt))
result = testdir.runpytest()
result.stdout.fnmatch_lines("*3 passed*")