Skip to content

Commit 48215fd

Browse files
authored
Merge pull request #3500 from RonnyPfannschmidt/fix-3498-unittest-marks
fix #3498 - correctly consider marks on unittest classes
2 parents afde9f0 + bc25d51 commit 48215fd

File tree

5 files changed

+66
-3
lines changed

5 files changed

+66
-3
lines changed

_pytest/fixtures.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,7 @@ def __init__(self, session):
984984
session.config.pluginmanager.register(self, "funcmanage")
985985

986986
def getfixtureinfo(self, node, func, cls, funcargs=True):
987-
if funcargs and not hasattr(node, "nofuncargs"):
987+
if funcargs and not getattr(node, "nofuncargs", False):
988988
argnames = getfuncargnames(func, cls=cls)
989989
else:
990990
argnames = ()

_pytest/python.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1155,7 +1155,7 @@ def __init__(self, name, parent, args=None, config=None,
11551155

11561156
if fixtureinfo is None:
11571157
fixtureinfo = self.session._fixturemanager.getfixtureinfo(
1158-
self.parent, self.obj, self.cls,
1158+
self, self.obj, self.cls,
11591159
funcargs=not self._isyieldedfunction())
11601160
self._fixtureinfo = fixtureinfo
11611161
self.fixturenames = fixtureinfo.names_closure

_pytest/unittest.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def collect(self):
5454
continue
5555
funcobj = getattr(x, 'im_func', x)
5656
transfer_markers(funcobj, cls, module)
57-
yield TestCaseFunction(name, parent=self)
57+
yield TestCaseFunction(name, parent=self, callobj=funcobj)
5858
foundsomething = True
5959

6060
if not foundsomething:
@@ -66,6 +66,7 @@ def collect(self):
6666

6767

6868
class TestCaseFunction(Function):
69+
nofuncargs = True
6970
_excinfo = None
7071

7172
def setup(self):

changelog/3498.bugfix

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix ``usefixtures`` mark applyed to unittest tests by correctly instantiating ``FixtureInfo``.

testing/test_unittest.py

+61
Original file line numberDiff line numberDiff line change
@@ -828,3 +828,64 @@ def test_should_not_run(self):
828828
""")
829829
reprec = testdir.inline_run()
830830
reprec.assertoutcome(passed=1)
831+
832+
833+
@pytest.mark.issue(3498)
834+
@pytest.mark.parametrize("base", [
835+
'six.moves.builtins.object',
836+
'unittest.TestCase',
837+
'unittest2.TestCase',
838+
])
839+
def test_usefixtures_marker_on_unittest(base, testdir):
840+
module = base.rsplit('.', 1)[0]
841+
pytest.importorskip(module)
842+
testdir.makepyfile(conftest="""
843+
import pytest
844+
845+
@pytest.fixture(scope='function')
846+
def fixture1(request, monkeypatch):
847+
monkeypatch.setattr(request.instance, 'fixture1', True )
848+
849+
850+
@pytest.fixture(scope='function')
851+
def fixture2(request, monkeypatch):
852+
monkeypatch.setattr(request.instance, 'fixture2', True )
853+
854+
def node_and_marks(item):
855+
print(item.nodeid)
856+
for mark in item.iter_markers():
857+
print(" ", mark)
858+
859+
@pytest.fixture(autouse=True)
860+
def my_marks(request):
861+
node_and_marks(request.node)
862+
863+
def pytest_collection_modifyitems(items):
864+
for item in items:
865+
node_and_marks(item)
866+
867+
""")
868+
869+
testdir.makepyfile("""
870+
import pytest
871+
import {module}
872+
873+
class Tests({base}):
874+
fixture1 = False
875+
fixture2 = False
876+
877+
@pytest.mark.usefixtures("fixture1")
878+
def test_one(self):
879+
assert self.fixture1
880+
assert not self.fixture2
881+
882+
@pytest.mark.usefixtures("fixture1", "fixture2")
883+
def test_two(self):
884+
assert self.fixture1
885+
assert self.fixture2
886+
887+
888+
""".format(module=module, base=base))
889+
890+
result = testdir.runpytest('-s')
891+
result.assert_outcomes(passed=2)

0 commit comments

Comments
 (0)