1
1
import os
2
+ from pathlib import Path
2
3
3
4
import pytest
4
5
from unittest import mock
5
6
6
7
import matplotlib as mpl
7
8
import matplotlib .pyplot as plt
8
- try :
9
- from matplotlib .backends import _macosx
10
- except ImportError :
11
- pytest .skip ("These are mac only tests" , allow_module_level = True )
9
+ from matplotlib .testing import subprocess_run_helper
12
10
13
11
14
- @pytest .mark .backend ('macosx' )
15
- def test_cached_renderer ():
12
+ _test_timeout = 60
13
+
14
+
15
+ def _test_cached_renderer ():
16
16
# Make sure that figures have an associated renderer after
17
17
# a fig.canvas.draw() call
18
18
fig = plt .figure (1 )
@@ -24,8 +24,14 @@ def test_cached_renderer():
24
24
assert fig .canvas .get_renderer ()._renderer is not None
25
25
26
26
27
- @pytest .mark .backend ('macosx' )
28
- def test_savefig_rcparam (monkeypatch , tmp_path ):
27
+ @pytest .mark .backend ('macosx' , skip_on_importerror = True )
28
+ def test_cached_renderer ():
29
+ subprocess_run_helper (_test_cached_renderer , timeout = _test_timeout ,
30
+ extra_env = {"MPLBACKEND" : "macosx" })
31
+
32
+
33
+ def _test_savefig_rcparam ():
34
+ tmp_path = Path (os .environ ["TEST_SAVEFIG_PATH" ])
29
35
30
36
def new_choose_save_file (title , directory , filename ):
31
37
# Replacement function instead of opening a GUI window
@@ -34,7 +40,7 @@ def new_choose_save_file(title, directory, filename):
34
40
os .makedirs (f"{ directory } /test" )
35
41
return f"{ directory } /test/{ filename } "
36
42
37
- monkeypatch . setattr ( _macosx , " choose_save_file" , new_choose_save_file )
43
+ mock . patch ( "matplotlib.backends._macosx. choose_save_file" , new_choose_save_file )
38
44
fig = plt .figure ()
39
45
with mpl .rc_context ({"savefig.directory" : tmp_path }):
40
46
fig .canvas .toolbar .save_figure ()
@@ -47,14 +53,20 @@ def new_choose_save_file(title, directory, filename):
47
53
assert mpl .rcParams ["savefig.directory" ] == f"{ tmp_path } /test"
48
54
49
55
50
- @pytest .mark .backend ('macosx' )
56
+ @pytest .mark .backend ('macosx' , skip_on_importerror = True )
57
+ def test_savefig_rcparam (tmp_path ):
58
+ subprocess_run_helper (
59
+ _test_savefig_rcparam , timeout = _test_timeout ,
60
+ extra_env = {"MPLBACKEND" : "macosx" , "TEST_SAVEFIG_PATH" : tmp_path })
61
+
62
+
63
+ @pytest .mark .backend ('macosx' , skip_on_importerror = True )
51
64
def test_ipython ():
52
65
from matplotlib .testing import ipython_in_subprocess
53
66
ipython_in_subprocess ("osx" , {(8 , 24 ): "macosx" , (7 , 0 ): "MacOSX" })
54
67
55
68
56
- @pytest .mark .backend ('macosx' )
57
- def test_save_figure_return ():
69
+ def _test_save_figure_return ():
58
70
fig , ax = plt .subplots ()
59
71
ax .imshow ([[1 ]])
60
72
prop = "matplotlib.backends._macosx.choose_save_file"
@@ -65,3 +77,9 @@ def test_save_figure_return():
65
77
with mock .patch (prop , return_value = None ):
66
78
fname = fig .canvas .manager .toolbar .save_figure ()
67
79
assert fname is None
80
+
81
+
82
+ @pytest .mark .backend ('macosx' , skip_on_importerror = True )
83
+ def test_save_figure_return ():
84
+ subprocess_run_helper (_test_save_figure_return , timeout = _test_timeout ,
85
+ extra_env = {"MPLBACKEND" : "macosx" })
0 commit comments