3
3
4
4
import inspect
5
5
import pytest
6
+ import unittest
6
7
7
8
import matplotlib
8
9
matplotlib .use ('agg' )
9
10
10
11
from matplotlib .testing .decorators import ImageComparisonTest
11
12
12
13
14
+ def is_nose_class (cls ):
15
+ return any (name in ['setUp' , 'tearDown' ]
16
+ for name , _ in inspect .getmembers (cls ))
17
+
18
+
13
19
def pytest_configure (config ):
14
20
matplotlib ._called_from_pytest = True
15
21
@@ -25,3 +31,20 @@ def pytest_pycollect_makeitem(collector, name, obj):
25
31
# instead of function and this confuses pytest because it crawls
26
32
# original names and sees 'test_*', but not 'Test*' in that case
27
33
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