Skip to content

Fix another unittest regression (#624) #625

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

Closed
wants to merge 2 commits into from
Closed
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
8 changes: 4 additions & 4 deletions pytest_django/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,12 +268,12 @@ def _classmethod_is_defined_at_leaf(cls, method_name):
super_method = None

for base_cls in cls.__mro__[1:]: # pragma: no branch
if hasattr(base_cls, method_name):
super_method = getattr(base_cls, method_name)
super_method = base_cls.__dict__.get(method_name)
if super_method is not None:
break

assert super_method is not None, (
'%s could not be found in base class' % method_name)
'%s could not be found in base classes' % method_name)

method = getattr(cls, method_name)

Expand All @@ -297,7 +297,7 @@ def _disable_class_methods(cls):

_disabled_classmethods[cls] = (
# Get the classmethod object (not the resulting bound method),
# otherwise inheritence will be broken when restoring.
# otherwise inheritance will be broken when restoring.
cls.__dict__.get('setUpClass'),
_classmethod_is_defined_at_leaf(cls, 'setUpClass'),
cls.__dict__.get('tearDownClass'),
Expand Down
55 changes: 40 additions & 15 deletions tests/test_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,12 @@ def test_bar21(self):
pass
''')

result = django_testdir.runpytest_subprocess('-v', '-s')
result = django_testdir.runpytest_subprocess('-v')
result.stdout.fnmatch_lines([
"*TestFoo::test_shared Creating test database for*",
"PASSED",
"*TestBar::test_bar1 PASSED",
"*TestBar::test_shared PASSED",
"*TestBar2::test_bar21 PASSED",
"*TestFoo::test_shared PASSED*",
"*TestBar::test_bar1 PASSED*",
"*TestBar::test_shared PASSED*",
"*TestBar2::test_bar21 PASSED*",
"*TestBar2::test_shared PASSED*",
])
assert result.ret == 0
Expand All @@ -196,10 +195,9 @@ def test_bar(self):
pass
''')

result = django_testdir.runpytest_subprocess('-v', '-s', '--pdb')
result = django_testdir.runpytest_subprocess('-v')
result.stdout.fnmatch_lines([
"*TestFoo::test_foo Creating test database for*",
"PASSED",
"*TestFoo::test_foo PASSED*",
"*TestBar::test_bar PASSED*",
])
assert result.ret == 0
Expand Down Expand Up @@ -231,13 +229,12 @@ def test_bar21(self):
pass
''')

result = django_testdir.runpytest_subprocess('-v', '-s')
result = django_testdir.runpytest_subprocess('-v')
result.stdout.fnmatch_lines([
"*TestFoo::test_shared Creating test database for*",
"SKIPPED",
"*TestBar::test_bar1 PASSED",
"*TestBar::test_shared PASSED",
"*TestBar2::test_bar21 PASSED",
"*TestFoo::test_shared SKIPPED*",
"*TestBar::test_bar1 PASSED*",
"*TestBar::test_shared PASSED*",
"*TestBar2::test_bar21 PASSED*",
"*TestBar2::test_shared PASSED*",
])
assert result.ret == 0
Expand Down Expand Up @@ -340,6 +337,34 @@ def test_pass(self):
])
assert result.ret == 0

def test_setUpClass_leaf_but_not_in_dunder_dict(self, django_testdir):
django_testdir.create_test_module('''
from django.test import testcases

class CMSTestCase(testcases.TestCase):
pass

class FooBarTestCase(testcases.TestCase):

@classmethod
def setUpClass(cls):
print('FooBarTestCase.setUpClass')
super(FooBarTestCase, cls).setUpClass()

class TestContact(CMSTestCase, FooBarTestCase):

def test_noop(self):
print('test_noop')
''')

result = django_testdir.runpytest_subprocess('-q', '-s')
result.stdout.fnmatch_lines([
"*FooBarTestCase.setUpClass*",
"*test_noop*",
"1 passed*",
])
assert result.ret == 0


class TestCaseWithDbFixture(TestCase):
pytestmark = pytest.mark.usefixtures('db')
Expand Down