Skip to content

Commit af4afe2

Browse files
committed
Add support of nose setup/teardown to pytest collector
1 parent e5ea9ee commit af4afe2

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

conftest.py

+23
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,19 @@
33

44
import inspect
55
import pytest
6+
import unittest
67

78
import matplotlib
89
matplotlib.use('agg')
910

1011
from matplotlib.testing.decorators import ImageComparisonTest
1112

1213

14+
def is_nose_class(cls):
15+
return any(name in ['setUp', 'tearDown']
16+
for name, _ in inspect.getmembers(cls))
17+
18+
1319
def pytest_configure(config):
1420
matplotlib._called_from_pytest = True
1521

@@ -25,3 +31,20 @@ def pytest_pycollect_makeitem(collector, name, obj):
2531
# instead of function and this confuses pytest because it crawls
2632
# original names and sees 'test_*', but not 'Test*' in that case
2733
return pytest.Class(name, parent=collector)
34+
35+
if is_nose_class(obj) and not issubclass(obj, unittest.TestCase):
36+
# Workaround unittest-like setup/teardown names in pure classes
37+
setup = getattr(obj, 'setUp', None)
38+
if setup is not None:
39+
obj.setup_method = lambda self, _: obj.setUp(self)
40+
tearDown = getattr(obj, 'tearDown', None)
41+
if tearDown is not None:
42+
obj.teardown_method = lambda self, _: obj.tearDown(self)
43+
setUpClass = getattr(obj, 'setUpClass', None)
44+
if setUpClass is not None:
45+
obj.setup_class = obj.setUpClass
46+
tearDownClass = getattr(obj, 'tearDownClass', None)
47+
if tearDownClass is not None:
48+
obj.teardown_class = obj.tearDownClass
49+
50+
return pytest.Class(name, parent=collector)

0 commit comments

Comments
 (0)