Skip to content

Commit 7d53b5b

Browse files
committed
xunit-style functions and methods are invoked by autouse fixtures
Fix #3094, Fix #517
1 parent 0da5531 commit 7d53b5b

File tree

6 files changed

+241
-68
lines changed

6 files changed

+241
-68
lines changed

changelog/3094.feature.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
`Class xunit-style <https://docs.pytest.org/en/latest/xunit_setup.html>`__ functions and methods
2+
now obey the scope of *autouse* fixtures.
3+
4+
This fixes a number of surprising issues like ``setup_method`` being called before session-scoped
5+
autouse fixtures (see `#517 <https://github.com/pytest-dev/pytest/issues/517>`__ for an example).
6+
7+

doc/en/xunit_setup.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,15 @@ Remarks:
9393

9494
* It is possible for setup/teardown pairs to be invoked multiple times
9595
per testing process.
96+
9697
* teardown functions are not called if the corresponding setup function existed
9798
and failed/was skipped.
9899

100+
* Prior to pytest-4.2, xunit-style functions did not obey the scope rules of fixtures, so
101+
it was possible, for example, for a ``setup_method`` to be called before a
102+
session-scoped autouse fixture.
103+
104+
Now the xunit-style functions are integrated with the fixture mechanism and obey the proper
105+
scope rules of fixtures involved in the call.
106+
99107
.. _`unittest.py module`: http://docs.python.org/library/unittest.html

src/_pytest/python.py

Lines changed: 142 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import os
1010
import sys
1111
import warnings
12+
from functools import partial
1213
from textwrap import dedent
1314

1415
import py
@@ -435,9 +436,66 @@ def _getobj(self):
435436
return self._importtestmodule()
436437

437438
def collect(self):
439+
self._inject_setup_module_fixture()
440+
self._inject_setup_function_fixture()
438441
self.session._fixturemanager.parsefactories(self)
439442
return super(Module, self).collect()
440443

444+
def _inject_setup_module_fixture(self):
445+
"""Injects a hidden autouse, module scoped fixture into the collected module object
446+
that invokes setUpModule/tearDownModule if either or both are available.
447+
448+
Using a fixture to invoke this methods ensures we play nicely and unsurprisingly with
449+
other fixtures (#517).
450+
"""
451+
setup_module = _get_non_fixture_func(self.obj, "setUpModule")
452+
if setup_module is None:
453+
setup_module = _get_non_fixture_func(self.obj, "setup_module")
454+
455+
teardown_module = _get_non_fixture_func(self.obj, "tearDownModule")
456+
if teardown_module is None:
457+
teardown_module = _get_non_fixture_func(self.obj, "teardown_module")
458+
459+
if setup_module is None and teardown_module is None:
460+
return
461+
462+
@fixtures.fixture(autouse=True, scope="module")
463+
def xunit_setup_module_fixture(request):
464+
if setup_module is not None:
465+
_call_with_optional_argument(setup_module, request.module)
466+
yield
467+
if teardown_module is not None:
468+
_call_with_optional_argument(teardown_module, request.module)
469+
470+
self.obj.__pytest_setup_module = xunit_setup_module_fixture
471+
472+
def _inject_setup_function_fixture(self):
473+
"""Injects a hidden autouse, function scoped fixture into the collected module object
474+
that invokes setup_function/teardown_function if either or both are available.
475+
476+
Using a fixture to invoke this methods ensures we play nicely and unsurprisingly with
477+
other fixtures (#517).
478+
"""
479+
setup_function = _get_non_fixture_func(self.obj, "setup_function")
480+
teardown_function = _get_non_fixture_func(self.obj, "teardown_function")
481+
if setup_function is None and teardown_function is None:
482+
return
483+
484+
@fixtures.fixture(autouse=True, scope="function")
485+
def xunit_setup_function_fixture(request):
486+
if request.instance is not None:
487+
# in this case we are bound to an instance, so we need to let
488+
# setup_method handle this
489+
yield
490+
return
491+
if setup_function is not None:
492+
_call_with_optional_argument(setup_function, request.function)
493+
yield
494+
if teardown_function is not None:
495+
_call_with_optional_argument(teardown_function, request.function)
496+
497+
self.obj.__pytest_setup_function = xunit_setup_function_fixture
498+
441499
def _importtestmodule(self):
442500
# we assume we are only called once per module
443501
importmode = self.config.getoption("--import-mode")
@@ -488,19 +546,6 @@ def _importtestmodule(self):
488546
self.config.pluginmanager.consider_module(mod)
489547
return mod
490548

491-
def setup(self):
492-
setup_module = _get_xunit_setup_teardown(self.obj, "setUpModule")
493-
if setup_module is None:
494-
setup_module = _get_xunit_setup_teardown(self.obj, "setup_module")
495-
if setup_module is not None:
496-
setup_module()
497-
498-
teardown_module = _get_xunit_setup_teardown(self.obj, "tearDownModule")
499-
if teardown_module is None:
500-
teardown_module = _get_xunit_setup_teardown(self.obj, "teardown_module")
501-
if teardown_module is not None:
502-
self.addfinalizer(teardown_module)
503-
504549

505550
class Package(Module):
506551
def __init__(self, fspath, parent=None, config=None, session=None, nodeid=None):
@@ -513,6 +558,22 @@ def __init__(self, fspath, parent=None, config=None, session=None, nodeid=None):
513558
self._norecursepatterns = session._norecursepatterns
514559
self.fspath = fspath
515560

561+
def setup(self):
562+
# not using fixtures to call setup_module here because autouse fixtures
563+
# from packages are not called automatically (#4085)
564+
setup_module = _get_non_fixture_func(self.obj, "setUpModule")
565+
if setup_module is None:
566+
setup_module = _get_non_fixture_func(self.obj, "setup_module")
567+
if setup_module is not None:
568+
_call_with_optional_argument(setup_module, self.obj)
569+
570+
teardown_module = _get_non_fixture_func(self.obj, "tearDownModule")
571+
if teardown_module is None:
572+
teardown_module = _get_non_fixture_func(self.obj, "teardown_module")
573+
if teardown_module is not None:
574+
func = partial(_call_with_optional_argument, teardown_module, self.obj)
575+
self.addfinalizer(func)
576+
516577
def _recurse(self, dirpath):
517578
if dirpath.basename == "__pycache__":
518579
return False
@@ -599,8 +660,9 @@ def _get_xunit_setup_teardown(holder, attr_name, param_obj=None):
599660
when the callable is called without arguments, defaults to the ``holder`` object.
600661
Return ``None`` if a suitable callable is not found.
601662
"""
663+
# TODO: only needed because of Package!
602664
param_obj = param_obj if param_obj is not None else holder
603-
result = _get_xunit_func(holder, attr_name)
665+
result = _get_non_fixture_func(holder, attr_name)
604666
if result is not None:
605667
arg_count = result.__code__.co_argcount
606668
if inspect.ismethod(result):
@@ -611,7 +673,19 @@ def _get_xunit_setup_teardown(holder, attr_name, param_obj=None):
611673
return result
612674

613675

614-
def _get_xunit_func(obj, name):
676+
def _call_with_optional_argument(func, arg):
677+
"""Call the given function with the given argument if func accepts one argument, otherwise
678+
calls func without arguments"""
679+
arg_count = func.__code__.co_argcount
680+
if inspect.ismethod(func):
681+
arg_count -= 1
682+
if arg_count:
683+
func(arg)
684+
else:
685+
func()
686+
687+
688+
def _get_non_fixture_func(obj, name):
615689
"""Return the attribute from the given object to be used as a setup/teardown
616690
xunit-style function, but only if not marked as a fixture to
617691
avoid calling it twice.
@@ -643,18 +717,60 @@ def collect(self):
643717
)
644718
)
645719
return []
720+
721+
self._inject_setup_class_fixture()
722+
self._inject_setup_method_fixture()
723+
646724
return [Instance(name="()", parent=self)]
647725

648-
def setup(self):
649-
setup_class = _get_xunit_func(self.obj, "setup_class")
650-
if setup_class is not None:
651-
setup_class = getimfunc(setup_class)
652-
setup_class(self.obj)
726+
def _inject_setup_class_fixture(self):
727+
"""Injects a hidden autouse, class scoped fixture into the collected class object
728+
that invokes setup_class/teardown_class if either or both are available.
653729
654-
fin_class = getattr(self.obj, "teardown_class", None)
655-
if fin_class is not None:
656-
fin_class = getimfunc(fin_class)
657-
self.addfinalizer(lambda: fin_class(self.obj))
730+
Using a fixture to invoke this methods ensures we play nicely and unsurprisingly with
731+
other fixtures (#517).
732+
"""
733+
setup_class = _get_non_fixture_func(self.obj, "setup_class")
734+
teardown_class = getattr(self.obj, "teardown_class", None)
735+
if setup_class is None and teardown_class is None:
736+
return
737+
738+
@fixtures.fixture(autouse=True, scope="class")
739+
def xunit_setup_class_fixture(cls):
740+
if setup_class is not None:
741+
func = getimfunc(setup_class)
742+
_call_with_optional_argument(func, self.obj)
743+
yield
744+
if teardown_class is not None:
745+
func = getimfunc(teardown_class)
746+
_call_with_optional_argument(func, self.obj)
747+
748+
self.obj.__pytest_setup_class = xunit_setup_class_fixture
749+
750+
def _inject_setup_method_fixture(self):
751+
"""Injects a hidden autouse, function scoped fixture into the collected class object
752+
that invokes setup_method/teardown_method if either or both are available.
753+
754+
Using a fixture to invoke this methods ensures we play nicely and unsurprisingly with
755+
other fixtures (#517).
756+
"""
757+
setup_method = _get_non_fixture_func(self.obj, "setup_method")
758+
teardown_method = getattr(self.obj, "teardown_method", None)
759+
if setup_method is None and teardown_method is None:
760+
return
761+
762+
@fixtures.fixture(autouse=True, scope="function")
763+
def xunit_setup_method_fixture(self, request):
764+
method = request.function
765+
if setup_method is not None:
766+
func = getattr(self, "setup_method")
767+
_call_with_optional_argument(func, method)
768+
yield
769+
if teardown_method is not None:
770+
func = getattr(self, "teardown_method")
771+
_call_with_optional_argument(func, method)
772+
773+
self.obj.__pytest_setup_method = xunit_setup_method_fixture
658774

659775

660776
class Instance(PyCollector):
@@ -681,29 +797,9 @@ class FunctionMixin(PyobjMixin):
681797

682798
def setup(self):
683799
""" perform setup for this test function. """
684-
if hasattr(self, "_preservedparent"):
685-
obj = self._preservedparent
686-
elif isinstance(self.parent, Instance):
687-
obj = self.parent.newinstance()
800+
if isinstance(self.parent, Instance):
801+
self.parent.newinstance()
688802
self.obj = self._getobj()
689-
else:
690-
obj = self.parent.obj
691-
if inspect.ismethod(self.obj):
692-
setup_name = "setup_method"
693-
teardown_name = "teardown_method"
694-
else:
695-
setup_name = "setup_function"
696-
teardown_name = "teardown_function"
697-
setup_func_or_method = _get_xunit_setup_teardown(
698-
obj, setup_name, param_obj=self.obj
699-
)
700-
if setup_func_or_method is not None:
701-
setup_func_or_method()
702-
teardown_func_or_method = _get_xunit_setup_teardown(
703-
obj, teardown_name, param_obj=self.obj
704-
)
705-
if teardown_func_or_method is not None:
706-
self.addfinalizer(teardown_func_or_method)
707803

708804
def _prunetraceback(self, excinfo):
709805
if hasattr(self, "_obj") and not self.config.option.fulltrace:

src/_pytest/unittest.py

Lines changed: 45 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import traceback
88

99
import _pytest._code
10+
import pytest
1011
from _pytest.compat import getimfunc
1112
from _pytest.config import hookimpl
1213
from _pytest.outcomes import fail
@@ -32,24 +33,18 @@ class UnitTestCase(Class):
3233
# to declare that our children do not support funcargs
3334
nofuncargs = True
3435

35-
def setup(self):
36-
cls = self.obj
37-
if getattr(cls, "__unittest_skip__", False):
38-
return # skipped
39-
setup = getattr(cls, "setUpClass", None)
40-
if setup is not None:
41-
setup()
42-
teardown = getattr(cls, "tearDownClass", None)
43-
if teardown is not None:
44-
self.addfinalizer(teardown)
45-
super(UnitTestCase, self).setup()
46-
4736
def collect(self):
4837
from unittest import TestLoader
4938

5039
cls = self.obj
5140
if not getattr(cls, "__test__", True):
5241
return
42+
43+
skipped = getattr(cls, "__unittest_skip__", False)
44+
if not skipped:
45+
self._inject_setup_teardown_fixtures(cls)
46+
self._inject_setup_class_fixture()
47+
5348
self.session._fixturemanager.parsefactories(self, unittest=True)
5449
loader = TestLoader()
5550
foundsomething = False
@@ -68,6 +63,44 @@ def collect(self):
6863
if ut is None or runtest != ut.TestCase.runTest:
6964
yield TestCaseFunction("runTest", parent=self)
7065

66+
def _inject_setup_teardown_fixtures(self, cls):
67+
"""Injects a hidden auto-use fixture to invoke setUpClass/setup_method and corresponding
68+
teardown functions (#517)"""
69+
class_fixture = _make_xunit_fixture(
70+
cls, "setUpClass", "tearDownClass", scope="class", pass_self=False
71+
)
72+
if class_fixture:
73+
cls.__pytest_class_setup = class_fixture
74+
75+
method_fixture = _make_xunit_fixture(
76+
cls, "setup_method", "teardown_method", scope="function", pass_self=True
77+
)
78+
if method_fixture:
79+
cls.__pytest_method_setup = method_fixture
80+
81+
82+
def _make_xunit_fixture(obj, setup_name, teardown_name, scope, pass_self):
83+
setup = getattr(obj, setup_name, None)
84+
teardown = getattr(obj, teardown_name, None)
85+
if setup is None and teardown is None:
86+
return None
87+
88+
@pytest.fixture(scope=scope, autouse=True)
89+
def fixture(self, request):
90+
if setup is not None:
91+
if pass_self:
92+
setup(self, request.function)
93+
else:
94+
setup()
95+
yield
96+
if teardown is not None:
97+
if pass_self:
98+
teardown(self, request.function)
99+
else:
100+
teardown()
101+
102+
return fixture
103+
71104

72105
class TestCaseFunction(Function):
73106
nofuncargs = True
@@ -77,9 +110,6 @@ class TestCaseFunction(Function):
77110
def setup(self):
78111
self._testcase = self.parent.obj(self.name)
79112
self._fix_unittest_skip_decorator()
80-
self._obj = getattr(self._testcase, self.name)
81-
if hasattr(self._testcase, "setup_method"):
82-
self._testcase.setup_method(self._obj)
83113
if hasattr(self, "_request"):
84114
self._request._fillfixtures()
85115

@@ -97,11 +127,7 @@ def _fix_unittest_skip_decorator(self):
97127
setattr(self._testcase, "__name__", self.name)
98128

99129
def teardown(self):
100-
if hasattr(self._testcase, "teardown_method"):
101-
self._testcase.teardown_method(self._obj)
102-
# Allow garbage collection on TestCase instance attributes.
103130
self._testcase = None
104-
self._obj = None
105131

106132
def startTest(self, testcase):
107133
pass

testing/python/collect.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,6 @@ def prop(self):
240240
assert result.ret == EXIT_NOTESTSCOLLECTED
241241

242242

243-
@pytest.mark.filterwarnings(
244-
"ignore:usage of Generator.Function is deprecated, please use pytest.Function instead"
245-
)
246243
class TestFunction(object):
247244
def test_getmodulecollector(self, testdir):
248245
item = testdir.getitem("def test_func(): pass")

0 commit comments

Comments
 (0)