Skip to content

Commit d96e105

Browse files
committed
unittest: _disable_class_methods: fall back to bound methods
Fixes #624.
1 parent 3705a66 commit d96e105

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

pytest_django/plugin.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,12 +291,16 @@ def _disable_class_methods(cls):
291291
if cls in _disabled_classmethods:
292292
return
293293

294+
# Get the classmethod object (not the resulting bound method),
295+
# otherwise inheritence will be broken when restoring.
296+
# But fall back to bound method in case it is None in __dict__.
297+
setUpClass = cls.__dict__.get('setUpClass', cls.setUpClass)
298+
tearDownClass = cls.__dict__.get('tearDownClass', cls.tearDownClass)
299+
294300
_disabled_classmethods[cls] = (
295-
# Get the classmethod object (not the resulting bound method),
296-
# otherwise inheritence will be broken when restoring.
297-
cls.__dict__.get('setUpClass'),
301+
setUpClass,
298302
_classmethod_is_defined_at_leaf(cls, 'setUpClass'),
299-
cls.__dict__.get('tearDownClass'),
303+
tearDownClass,
300304
_classmethod_is_defined_at_leaf(cls, 'tearDownClass'),
301305
)
302306

tests/test_unittest.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,34 @@ def test_pass(self):
337337
])
338338
assert result.ret == 0
339339

340+
def test_setUpClass_leaf_but_not_in_dunder_dict(self, django_testdir):
341+
django_testdir.create_test_module('''
342+
from django.test import testcases
343+
344+
class CMSTestCase(testcases.TestCase):
345+
pass
346+
347+
class FooBarTestCase(testcases.TestCase):
348+
349+
@classmethod
350+
def setUpClass(cls):
351+
print('FooBarTestCase.setUpClass')
352+
super(FooBarTestCase, cls).setUpClass()
353+
354+
class TestContact(CMSTestCase, FooBarTestCase):
355+
356+
def test_noop(self):
357+
print('test_noop')
358+
''')
359+
360+
result = django_testdir.runpytest_subprocess('-q', '-s')
361+
result.stdout.fnmatch_lines([
362+
"*FooBarTestCase.setUpClass*",
363+
"*test_noop*",
364+
"1 passed in *",
365+
])
366+
assert result.ret == 0
367+
340368

341369
class TestCaseWithDbFixture(TestCase):
342370
pytestmark = pytest.mark.usefixtures('db')

0 commit comments

Comments
 (0)