Fix autouse fixtures defined in a TestCase subclass
This commit is contained in:
parent
23aaa8a62c
commit
31cfbac1f4
|
@ -12,6 +12,10 @@
|
||||||
- preserve warning functions after call to pytest.deprecated_call. Thanks
|
- preserve warning functions after call to pytest.deprecated_call. Thanks
|
||||||
Pieter Mulder for PR.
|
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
|
- fix issue833: --fixtures now shows all fixtures of collected test files, instead of just the
|
||||||
fixtures declared on the first one.
|
fixtures declared on the first one.
|
||||||
Thanks Florian Bruhin for reporting and Bruno Oliveira for the PR.
|
Thanks Florian Bruhin for reporting and Bruno Oliveira for the PR.
|
||||||
|
|
|
@ -1900,10 +1900,13 @@ class FixtureDef:
|
||||||
self.finish()
|
self.finish()
|
||||||
assert not hasattr(self, "cached_result")
|
assert not hasattr(self, "cached_result")
|
||||||
|
|
||||||
if self.unittest:
|
|
||||||
result = self.func(request.instance, **kwargs)
|
|
||||||
else:
|
|
||||||
fixturefunc = self.func
|
fixturefunc = self.func
|
||||||
|
|
||||||
|
if self.unittest:
|
||||||
|
if request.instance is not None:
|
||||||
|
# bind the unbound method to the TestCase instance
|
||||||
|
fixturefunc = self.func.__get__(request.instance)
|
||||||
|
else:
|
||||||
# the fixture function needs to be bound to the actual
|
# the fixture function needs to be bound to the actual
|
||||||
# request.instance so that code working with "self" behaves
|
# request.instance so that code working with "self" behaves
|
||||||
# as expected.
|
# as expected.
|
||||||
|
@ -1911,6 +1914,7 @@ class FixtureDef:
|
||||||
fixturefunc = getimfunc(self.func)
|
fixturefunc = getimfunc(self.func)
|
||||||
if fixturefunc != self.func:
|
if fixturefunc != self.func:
|
||||||
fixturefunc = fixturefunc.__get__(request.instance)
|
fixturefunc = fixturefunc.__get__(request.instance)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
result = call_fixture_func(fixturefunc, request, kwargs,
|
result = call_fixture_func(fixturefunc, request, kwargs,
|
||||||
self.yieldctx)
|
self.yieldctx)
|
||||||
|
|
|
@ -607,18 +607,23 @@ def test_unittest_unexpected_failure(testdir):
|
||||||
])
|
])
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize('fix_type, stmt', [
|
||||||
def test_unittest_setup_interaction(testdir):
|
('fixture', 'return'),
|
||||||
|
('yield_fixture', 'yield'),
|
||||||
|
])
|
||||||
|
def test_unittest_setup_interaction(testdir, fix_type, stmt):
|
||||||
testdir.makepyfile("""
|
testdir.makepyfile("""
|
||||||
import unittest
|
import unittest
|
||||||
import pytest
|
import pytest
|
||||||
class MyTestCase(unittest.TestCase):
|
class MyTestCase(unittest.TestCase):
|
||||||
@pytest.fixture(scope="class", autouse=True)
|
@pytest.{fix_type}(scope="class", autouse=True)
|
||||||
def perclass(self, request):
|
def perclass(self, request):
|
||||||
request.cls.hello = "world"
|
request.cls.hello = "world"
|
||||||
@pytest.fixture(scope="function", autouse=True)
|
{stmt}
|
||||||
|
@pytest.{fix_type}(scope="function", autouse=True)
|
||||||
def perfunction(self, request):
|
def perfunction(self, request):
|
||||||
request.instance.funcname = request.function.__name__
|
request.instance.funcname = request.function.__name__
|
||||||
|
{stmt}
|
||||||
|
|
||||||
def test_method1(self):
|
def test_method1(self):
|
||||||
assert self.funcname == "test_method1"
|
assert self.funcname == "test_method1"
|
||||||
|
@ -629,7 +634,7 @@ def test_unittest_setup_interaction(testdir):
|
||||||
|
|
||||||
def test_classattr(self):
|
def test_classattr(self):
|
||||||
assert self.__class__.hello == "world"
|
assert self.__class__.hello == "world"
|
||||||
""")
|
""".format(fix_type=fix_type, stmt=stmt))
|
||||||
result = testdir.runpytest()
|
result = testdir.runpytest()
|
||||||
result.stdout.fnmatch_lines("*3 passed*")
|
result.stdout.fnmatch_lines("*3 passed*")
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue