8000 TST: get coverage information out of isolated tk tests · matplotlib/matplotlib@4a2f554 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4a2f554

Browse files
committed
TST: get coverage information out of isolated tk tests
1 parent 507bff4 commit 4a2f554

File tree

1 file changed

+23
-28
lines changed

1 file changed

+23
-28
lines changed

lib/matplotlib/tests/test_backend_tk.py

Lines changed: 23 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import functools
2-
import inspect
2+
import importlib
33
import os
44
import platform
5-
import re
65
import subprocess
76
import sys
87

98
import pytest
109

10+
from matplotlib.testing import subprocess_run_helper
11+
from matplotlib import _c_internal_utils
12+
1113
_test_timeout = 60 # A reasonably safe value for slower architectures.
1214

1315

@@ -20,28 +22,24 @@ def _isolated_tk_test(success_count, func=None):
2022
in a subprocess. See GH#18261
2123
2224
The decorated function must be fully self-contained, and thus perform
23-
all the imports it needs. Because its source is extracted and run by
24-
itself, coverage will consider it as not being run, so it should be marked
25-
with ``# pragma: no cover``
25+
all the imports it needs.
2626
"""
2727

2828
if func is None:
2929
return functools.partial(_isolated_tk_test, success_count)
3030

31-
# Remove decorators.
32-
source = re.search(r"(?ms)^def .*", inspect.getsource(func)).group(0)
33-
3431
@functools.wraps(func)
3532
def test_func():
33+
if (sys.platform == "linux" and
34+
not _c_internal_utils.display_is_valid()):
35+
pytest.skip("$DISPLAY and $WAYLAND_DISPLAY are unset")
36+
elif not importlib.util.find_spec('tkinter'):
37+
pytest.skip("missing tkinter")
3638
try:
37-
proc = subprocess.run(
38-
[sys.executable, "-c", f"{source}\n{func.__name__}()"],
39-
env={**os.environ, "MPLBACKEND": "TkAgg"},
40-
timeout=_test_timeout,
41-
stdout=subprocess.PIPE,
42-
stderr=subprocess.PIPE,
43-
check=True,
44-
universal_newlines=True,
39+
proc = subprocess_run_helper(
40+
__name__, func, timeout=_test_timeout,
41+
MPLBACKEND="TkAgg",
42+
MPL_TEST_ESCAPE_HATCH="1"
4543
)
4644
except subprocess.TimeoutExpired:
4745
pytest.fail("Subprocess timed out")
@@ -55,13 +53,14 @@ def test_func():
5553
assert not [line for line in proc.stderr.splitlines()
5654
if "OpenGL" not in line]
5755
assert proc.stdout.count("success") == success_count
58-
59-
return test_func
56+
if "MPL_TEST_ESCAPE_HATCH" in os.environ:
57+
return func
58+
else:
59+
return test_func
6060

6161

62-
@pytest.mark.backend('TkAgg', skip_on_importerror=True)
6362
@_isolated_tk_test(success_count=6) # len(bad_boxes)
64-
def test_blit(): # pragma: no cover
63+
def test_blit():
6564
import matplotlib.pyplot as plt
6665
import numpy as np
6766
import matplotlib.backends.backend_tkagg # noqa
@@ -88,9 +87,8 @@ def test_blit(): # pragma: no cover
8887
print("success")
8988

9089

91-
@pytest.mark.backend('TkAgg', skip_on_importerror=True)
9290
@_isolated_tk_test(success_count=1)
93-
def test_figuremanager_preserves_host_mainloop(): # pragma: no cover
91+
def test_figuremanager_preserves_host_mainloop():
9492
import tkinter
9593
import matplotlib.pyplot as plt
9694
success = []
@@ -116,10 +114,9 @@ def legitimate_quit():
116114
@pytest.mark.skipif(platform.python_implementation() != 'CPython',
117115
reason='PyPy does not support Tkinter threading: '
118116
'https://foss.heptapod.net/pypy/pypy/-/issues/1929')
119-
@pytest.mark.backend('TkAgg', skip_on_importerror=True)
120117
@pytest.mark.flaky(reruns=3)
121118
@_isolated_tk_test(success_count=1)
122-
def test_figuremanager_cleans_own_mainloop(): # pragma: no cover
119+
def test_figuremanager_cleans_own_mainloop():
123120
import tkinter
124121
import time
125122
import matplotlib.pyplot as plt
@@ -144,10 +141,9 @@ def target():
144141
thread.join()
145142

146143

147-
@pytest.mark.backend('TkAgg', skip_on_importerror=True)
148144
@pytest.mark.flaky(reruns=3)
149145
@_isolated_tk_test(success_count=0)
150-
def test_never_update(): # pragma: no cover
146+
def test_never_update():
151147
import tkinter
152148
del tkinter.Misc.update
153149
del tkinter.Misc.update_idletasks
@@ -171,9 +167,8 @@ def test_never_update(): # pragma: no cover
171167
# checks them.
172168

173169

174-
@pytest.mark.backend('TkAgg', skip_on_importerror=True)
175170
@_isolated_tk_test(success_count=2)
176-
def test_missing_back_button(): # pragma: no cover
171+
def test_missing_back_button():
177172
import matplotlib.pyplot as plt
178173
from matplotlib.backends.backend_tkagg import NavigationToolbar2Tk
179174

0 commit comments

Comments
 (0)
0