8
8
import unittest
9
9
import warnings
10
10
11
- # Note - don't import nose up here - import it only as needed in functions.
12
- # This allows other functions here to be used by pytest-based testing suites
13
- # without requiring nose to be installed.
14
-
15
-
16
11
import matplotlib as mpl
17
12
import matplotlib .style
18
13
import matplotlib .units
19
14
import matplotlib .testing
20
15
from matplotlib import cbook
21
- from matplotlib import ticker
22
- from matplotlib import pyplot as plt
23
16
from matplotlib import ft2font
24
- from matplotlib . testing . compare import (
25
- comparable_formats , compare_images , make_test_filename )
17
+ from matplotlib import pyplot as plt
18
+ from matplotlib import ticker
26
19
from . import is_called_from_pytest
20
+ from .compare import comparable_formats , compare_images , make_test_filename
27
21
from .exceptions import ImageComparisonFailure
28
22
29
23
30
- def _do_cleanup (original_units_registry , original_settings ):
31
- plt .close ('all' )
32
-
33
- mpl .rcParams .clear ()
34
- mpl .rcParams .update (original_settings )
35
- matplotlib .units .registry .clear ()
36
- matplotlib .units .registry .update (original_units_registry )
37
- warnings .resetwarnings () # reset any warning filters set in tests
38
-
39
-
40
- class CleanupTest (object ):
41
- @classmethod
42
- def setup_class (cls ):
43
- cls .original_units_registry = matplotlib .units .registry .copy ()
44
- cls .original_settings = mpl .rcParams .copy ()
45
- matplotlib .testing .setup ()
46
-
47
- @classmethod
48
- def teardown_class (cls ):
49
- _do_cleanup (cls .original_units_registry ,
50
- cls .original_settings )
51
-
52
- def test (self ):
53
- self ._func ()
24
+ @contextlib .contextmanager
25
+ def _cleanup_cm ():
26
+ orig_units_registry = matplotlib .units .registry .copy ()
27
+ try :
28
+ with warnings .catch_warnings (), matplotlib .style .context (style ):
29
+ yield
30
+ finally :
31
+ matplotlib .units .registry .clear ()
32
+ matplotlib .units .registry .update (orig_units_registry )
33
+ plt .close ("all" )
54
34
55
35
56
36
class CleanupTestCase (unittest .TestCase ):
57
- ''' A wrapper for unittest.TestCase that includes cleanup operations'''
37
+ """ A wrapper for unittest.TestCase that includes cleanup operations."""
58
38
@classmethod
59
39
def setUpClass (cls ):
60
- import matplotlib .units
61
- cls .original_units_registry = matplotlib .units .registry .copy ()
62
- cls .original_settings = mpl .rcParams .copy ()
40
+ cls ._cm = _cleanup_cm ().__enter__ ()
63
41
64
42
@classmethod
65
43
def tearDownClass (cls ):
66
- _do_cleanup (cls .original_units_registry ,
67
- cls .original_settings )
44
+ cls ._cm .__exit__ (None , None , None )
45
+
46
+
47
+ @cbook .deprecated ("3.0" )
48
+ class CleanupTest (object ):
49
+ setup_class = classmethod (CleanupTestCase .setUpClass .__func__ )
50
+ teardown_class = classmethod (CleanupTestCase .tearDownClass .__func__ )
51
+
52
+ def test (self ):
53
+ self ._func ()
68
54
69
55
70
56
def cleanup (style = None ):
@@ -78,34 +64,23 @@ def cleanup(style=None):
78
64
The name of the style to apply.
79
65
"""
80
66
81
- # If cleanup is used without arguments, `style` will be a
82
- # callable, and we pass it directly to the wrapper generator. If
83
- # cleanup if called with an argument, it is a string naming a
84
- # style, and the function will be passed as an argument to what we
85
- # return. This is a confusing, but somewhat standard, pattern for
86
- # writing a decorator with optional arguments.
67
+ # If cleanup is used without arguments, `style` will be a callable, and we
68
+ # pass it directly to the wrapper generator. If cleanup if called with an
69
+ # argument, it is a string naming a style, and the function will be passed
70
+ # as an argument to what we return. This is a confusing, but somewhat
71
+ # standard, pattern for writing a decorator with optional arguments.
87
72
88
73
def make_cleanup (func ):
89
74
if inspect .isgeneratorfunction (func ):
90
75
@functools .wraps (func )
91
76
def wrapped_callable (* args , ** kwargs ):
92
- original_units_registry = matplotlib .units .registry .copy ()
93
- original_settings = mpl .rcParams .copy ()
94
- matplotlib .style .use (style )
95
- try :
77
+ with _cleanup_cm ():
96
78
yield from func (* args , ** kwargs )
97
- finally :
98
- _do_cleanup (original_units_registry , original_settings )
99
79
else :
100
80
@functools .wraps (func )
101
81
def wrapped_callable (* args , ** kwargs ):
102
- original_units_registry = matplotlib .units .registry .copy ()
103
- original_settings = mpl .rcParams .copy ()
104
- matplotlib .style .use (style )
105
- try :
82
+ with _cleanup_cm ():
106
83
func (* args , ** kwargs )
107
- finally :
108
- _do_cleanup (original_units_registry , original_settings )
109
84
110
85
return wrapped_callable
111
86
0 commit comments