Skip to content

Commit 27f2170

Browse files
committed
TST: Run macosx backends in a subprocess
In the macosx backend, `event_loop_is_running` returns true after `lazy_init` is called, which happens in the `new` constructor for _any_ class. This means it is "running" as soon as a figure is created, and this will block any other backend tests.
1 parent 183b04f commit 27f2170

File tree

1 file changed

+30
-12
lines changed

1 file changed

+30
-12
lines changed

lib/matplotlib/tests/test_backend_macosx.py

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import os
2+
from pathlib import Path
23

34
import pytest
45
from unittest import mock
56

67
import matplotlib as mpl
78
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
1210

1311

14-
@pytest.mark.backend('macosx')
15-
def test_cached_renderer():
12+
_test_timeout = 60
13+
14+
15+
def _test_cached_renderer():
1616
# Make sure that figures have an associated renderer after
1717
# a fig.canvas.draw() call
1818
fig = plt.figure(1)
@@ -24,8 +24,14 @@ def test_cached_renderer():
2424
assert fig.canvas.get_renderer()._renderer is not None
2525

2626

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"])
2935

3036
def new_choose_save_file(title, directory, filename):
3137
# Replacement function instead of opening a GUI window
@@ -34,7 +40,7 @@ def new_choose_save_file(title, directory, filename):
3440
os.makedirs(f"{directory}/test")
3541
return f"{directory}/test/{filename}"
3642

37-
monkeypatch.setattr(_macosx, "choose_save_file", new_choose_save_file)
43+
mock.patch("matplotlib.backends._macosx.choose_save_file", new_choose_save_file)
3844
fig = plt.figure()
3945
with mpl.rc_context({"savefig.directory": tmp_path}):
4046
fig.canvas.toolbar.save_figure()
@@ -47,14 +53,20 @@ def new_choose_save_file(title, directory, filename):
4753
assert mpl.rcParams["savefig.directory"] == f"{tmp_path}/test"
4854

4955

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)
5164
def test_ipython():
5265
from matplotlib.testing import ipython_in_subprocess
5366
ipython_in_subprocess("osx", {(8, 24): "macosx", (7, 0): "MacOSX"})
5467

5568

56-
@pytest.mark.backend('macosx')
57-
def test_save_figure_return():
69+
def _test_save_figure_return():
5870
fig, ax = plt.subplots()
5971
ax.imshow([[1]])
6072
prop = "matplotlib.backends._macosx.choose_save_file"
@@ -65,3 +77,9 @@ def test_save_figure_return():
6577
with mock.patch(prop, return_value=None):
6678
fname = fig.canvas.manager.toolbar.save_figure()
6779
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

Comments
 (0)