Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #3498 - correctly consider marks on unittest classes #3500

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion _pytest/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -984,7 +984,7 @@ def __init__(self, session):
session.config.pluginmanager.register(self, "funcmanage")

def getfixtureinfo(self, node, func, cls, funcargs=True):
if funcargs and not hasattr(node, "nofuncargs"):
if funcargs and not getattr(node, "nofuncargs", False):
argnames = getfuncargnames(func, cls=cls)
else:
argnames = ()
Expand Down
2 changes: 1 addition & 1 deletion _pytest/python.py
Original file line number Diff line number Diff line change
Expand Up @@ -1155,7 +1155,7 @@ def __init__(self, name, parent, args=None, config=None,

if fixtureinfo is None:
fixtureinfo = self.session._fixturemanager.getfixtureinfo(
self.parent, self.obj, self.cls,
self, self.obj, self.cls,
funcargs=not self._isyieldedfunction())
self._fixtureinfo = fixtureinfo
self.fixturenames = fixtureinfo.names_closure
Expand Down
3 changes: 2 additions & 1 deletion _pytest/unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def collect(self):
continue
funcobj = getattr(x, 'im_func', x)
transfer_markers(funcobj, cls, module)
yield TestCaseFunction(name, parent=self)
yield TestCaseFunction(name, parent=self, callobj=funcobj)
foundsomething = True

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


class TestCaseFunction(Function):
nofuncargs = True
_excinfo = None

def setup(self):
Expand Down
1 change: 1 addition & 0 deletions changelog/3498.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix ``usefixtures`` mark applyed to unittest tests by correctly instantiating ``FixtureInfo``.
61 changes: 61 additions & 0 deletions testing/test_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -828,3 +828,64 @@ def test_should_not_run(self):
""")
reprec = testdir.inline_run()
reprec.assertoutcome(passed=1)


@pytest.mark.issue(3498)
@pytest.mark.parametrize("base", [
'six.moves.builtins.object',
'unittest.TestCase',
'unittest2.TestCase',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We never install unittest2, should we add it to our testing dependencies in tox.ini?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

atm i dont think thats necessary

])
def test_usefixtures_marker_on_unittest(base, testdir):
module = base.rsplit('.', 1)[0]
pytest.importorskip(module)
testdir.makepyfile(conftest="""
import pytest

@pytest.fixture(scope='function')
def fixture1(request, monkeypatch):
monkeypatch.setattr(request.instance, 'fixture1', True )


@pytest.fixture(scope='function')
def fixture2(request, monkeypatch):
monkeypatch.setattr(request.instance, 'fixture2', True )

def node_and_marks(item):
print(item.nodeid)
for mark in item.iter_markers():
print(" ", mark)

@pytest.fixture(autouse=True)
def my_marks(request):
node_and_marks(request.node)

def pytest_collection_modifyitems(items):
for item in items:
node_and_marks(item)

""")

testdir.makepyfile("""
import pytest
import {module}

class Tests({base}):
fixture1 = False
fixture2 = False

@pytest.mark.usefixtures("fixture1")
def test_one(self):
assert self.fixture1
assert not self.fixture2

@pytest.mark.usefixtures("fixture1", "fixture2")
def test_two(self):
assert self.fixture1
assert self.fixture2


""".format(module=module, base=base))

result = testdir.runpytest('-s')
result.assert_outcomes(passed=2)