Skip to content

unittest: help with setUpClass not being a classmethod #544

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

Merged
merged 3 commits into from
Dec 25, 2017
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
18 changes: 14 additions & 4 deletions pytest_django/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
CONFIGURATION_ENV = 'DJANGO_CONFIGURATION'
INVALID_TEMPLATE_VARS_ENV = 'FAIL_INVALID_TEMPLATE_VARS'

PY2 = sys.version_info[0] == 2


# ############### pytest hooks ################

Expand Down Expand Up @@ -258,7 +260,7 @@ def pytest_configure():
_setup_django()


def _method_is_defined_at_leaf(cls, method_name):
def _classmethod_is_defined_at_leaf(cls, method_name):
super_method = None

for base_cls in cls.__bases__:
Expand All @@ -268,7 +270,15 @@ def _method_is_defined_at_leaf(cls, method_name):
assert super_method is not None, (
'%s could not be found in base class' % method_name)

return getattr(cls, method_name).__func__ is not super_method.__func__
method = getattr(cls, method_name)

try:
f = method.__func__
except AttributeError:
pytest.fail('%s.%s should be a classmethod' % (cls, method_name))
if PY2 and not (inspect.ismethod(method) and method.__self__ is cls):
pytest.fail('%s.%s should be a classmethod' % (cls, method_name))
return f is not super_method.__func__


_disabled_classmethods = {}
Expand All @@ -280,9 +290,9 @@ def _disable_class_methods(cls):

_disabled_classmethods[cls] = (
cls.setUpClass,
_method_is_defined_at_leaf(cls, 'setUpClass'),
_classmethod_is_defined_at_leaf(cls, 'setUpClass'),
cls.tearDownClass,
_method_is_defined_at_leaf(cls, 'tearDownClass'),
_classmethod_is_defined_at_leaf(cls, 'tearDownClass'),
)

cls.setUpClass = types.MethodType(lambda cls: None, cls)
Expand Down
19 changes: 19 additions & 0 deletions tests/test_unittest.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,25 @@ def test_pass(self):
])
assert result.ret == 0

def test_setUpClass_not_being_a_classmethod(self, django_testdir):
django_testdir.create_test_module('''
from django.test import TestCase

class TestFoo(TestCase):
def setUpClass(self):
pass

def test_pass(self):
pass
''')

result = django_testdir.runpytest_subprocess('-v', '-s')
result.stdout.fnmatch_lines([
"* ERROR at setup of TestFoo.test_pass *",
"E *Failed: <class 'tpkg.test_the_test.TestFoo'>.setUpClass should be a classmethod", # noqa:E501
])
assert result.ret == 1

def test_multi_inheritance_setUpClass(self, django_testdir):
django_testdir.create_test_module('''
from django.test import TestCase
Expand Down