1
1
import importlib
2
2
import os
3
- from subprocess import Popen
3
+ import signal
4
+ import subprocess
4
5
import sys
6
+ import time
7
+ import urllib .request
5
8
6
9
import pytest
7
10
11
+ import matplotlib as mpl
12
+
8
13
9
14
# Minimal smoke-testing of the backends for which the dependencies are
10
15
# PyPI-installable on Travis. They are not available for all tested Python
@@ -17,6 +22,7 @@ def _get_testable_interactive_backends():
17
22
(["PyQt5" ], "qt5agg" ),
18
23
(["cairocffi" , "PyQt5" ], "qt5cairo" ),
19
24
(["tkinter" ], "tkagg" ),
25
+ (["wx" ], "wx" ),
20
26
(["wx" ], "wxagg" )]:
21
27
reason = None
22
28
if not os .environ .get ("DISPLAY" ):
@@ -30,20 +36,48 @@ def _get_testable_interactive_backends():
30
36
31
37
_test_script = """\
32
38
import sys
33
- from matplotlib import pyplot as plt
39
+ from matplotlib import pyplot as plt, rcParams
40
+ rcParams.update({
41
+ "webagg.open_in_browser": False,
42
+ "webagg.port_retries": 1,
43
+ })
34
44
35
45
fig = plt.figure()
36
46
ax = fig.add_subplot(111)
37
- ax.plot([1,2,3 ], [1,3,1 ])
47
+ ax.plot([1, 2 ], [2, 3 ])
38
48
fig.canvas.mpl_connect("draw_event", lambda event: sys.exit())
39
49
plt.show()
40
50
"""
51
+ _test_timeout = 10 # Empirically, 1s is not enough on Travis.
41
52
42
53
43
54
@pytest .mark .parametrize ("backend" , _get_testable_interactive_backends ())
44
55
@pytest .mark .flaky (reruns = 3 )
45
- def test_backend (backend ):
46
- proc = Popen ([sys .executable , "-c" , _test_script ],
47
- env = {** os .environ , "MPLBACKEND" : backend })
48
- # Empirically, 1s is not enough on Travis.
49
- assert proc .wait (timeout = 10 ) == 0
56
+ def test_interactive_backend (backend ):
57
+ subprocess .run ([sys .executable , "-c" , _test_script ],
58
+ env = {** os .environ , "MPLBACKEND" : backend },
59
+ check = True , # Throw on failure.
60
+ timeout = _test_timeout )
61
+
62
+
63
+ @pytest .mark .skipif (
64
+ os .name == "win32" , reason = "Cannot send SIGINT on Windows." )
65
+ def test_webagg ():
66
+ pytest .importorskip ("tornado" )
67
+ proc = subprocess .Popen ([sys .executable , "-c" , _test_script ],
68
+ env = {** os .environ , "MPLBACKEND" : "webagg" })
69
+ url = "http://{}:{}" .format (
70
+ mpl .rcParams ["webagg.address" ], mpl .rcParams ["webagg.port" ])
71
+ timeout = time .perf_counter () + _test_timeout
72
+ while True :
73
+ try :
74
+ conn = urllib .request .urlopen (url )
75
+ break
76
+ except urllib .error .URLError :
77
+ if time .perf_counter () > timeout :
78
+ pytest .fail ("Failed to connect to the webagg server." )
79
+ else :
80
+ continue
81
+ conn .close ()
82
+ proc .send_signal (signal .SIGINT )
83
+ assert proc .wait (timeout = _test_timeout ) == 0
0 commit comments