Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 35171f8

Browse files
committed
Protect matplotlib import in tests and tag.
We tag matplotlib tests with `@attr(‘matplotlib’)` so we can skip them. However, we still need to test the imports.
1 parent b6db33c commit 35171f8

File tree

13 files changed

+162
-61
lines changed

13 files changed

+162
-61
lines changed

plotly/matplotlylib/mplexporter/tests/test_basic.py

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,34 @@
1-
from ..exporter import Exporter
2-
from ..renderers import FakeRenderer, FullFakeRenderer
1+
# TODO: matplotlib-build-wip
2+
from nose.plugins.attrib import attr
3+
from plotly.tools import _matplotlylib_imported
34

4-
import matplotlib
5-
matplotlib.use('Agg')
6-
import matplotlib.pyplot as plt
5+
if _matplotlylib_imported:
6+
from ..exporter import Exporter
7+
from ..renderers import FakeRenderer, FullFakeRenderer
78

8-
import numpy as np
9-
from numpy.testing import assert_warns
9+
import matplotlib
10+
matplotlib.use('Agg')
11+
import matplotlib.pyplot as plt
1012

13+
import numpy as np
14+
from numpy.testing import assert_warns
1115

16+
17+
@attr('matplotlib')
1218
def fake_renderer_output(fig, Renderer):
1319
renderer = Renderer()
1420
exporter = Exporter(renderer)
1521
exporter.run(fig)
1622
return renderer.output
1723

1824

25+
@attr('matplotlib')
1926
def _assert_output_equal(text1, text2):
2027
for line1, line2 in zip(text1.strip().split(), text2.strip().split()):
2128
assert line1 == line2
2229

2330

31+
@attr('matplotlib')
2432
def test_lines():
2533
fig, ax = plt.subplots()
2634
ax.plot(range(20), '-k')
@@ -44,6 +52,7 @@ def test_lines():
4452
""")
4553

4654

55+
@attr('matplotlib')
4756
def test_markers():
4857
fig, ax = plt.subplots()
4958
ax.plot(range(2), 'ok')
@@ -68,6 +77,7 @@ def test_markers():
6877
""")
6978

7079

80+
@attr('matplotlib')
7181
def test_path_collection():
7282
fig, ax = plt.subplots()
7383
ax.scatter(range(3), range(3))
@@ -93,6 +103,7 @@ def test_path_collection():
93103
""")
94104

95105

106+
@attr('matplotlib')
96107
def test_text():
97108
fig, ax = plt.subplots()
98109
ax.set_xlabel("my x label")
@@ -113,6 +124,7 @@ def test_text():
113124
""")
114125

115126

127+
@attr('matplotlib')
116128
def test_path():
117129
fig, ax = plt.subplots()
118130
ax.add_patch(plt.Circle((0, 0), 1))
@@ -129,6 +141,7 @@ def test_path():
129141
""")
130142

131143

144+
@attr('matplotlib')
132145
def test_multiaxes():
133146
fig, ax = plt.subplots(2)
134147
ax[0].plot(range(4))
@@ -147,6 +160,7 @@ def test_multiaxes():
147160
""")
148161

149162

163+
@attr('matplotlib')
150164
def test_image():
151165
np.random.seed(0) # image size depends on the seed
152166
fig, ax = plt.subplots()
@@ -163,6 +177,7 @@ def test_image():
163177
""")
164178

165179

180+
@attr('matplotlib')
166181
def test_legend():
167182
fig, ax = plt.subplots()
168183
ax.plot([1,2,3], label='label')
@@ -178,6 +193,8 @@ def test_legend():
178193
closing figure
179194
""")
180195

196+
197+
@attr('matplotlib')
181198
def test_legend_dots():
182199
fig, ax = plt.subplots()
183200
ax.plot([1,2,3], label='label')
@@ -200,8 +217,9 @@ def test_legend_dots():
200217
closing figure
201218
""")
202219

220+
221+
@attr('matplotlib')
203222
def test_blended():
204223
fig, ax = plt.subplots()
205224
ax.axvline(0)
206225
assert_warns(UserWarning, fake_renderer_output, fig, FakeRenderer)
207-

plotly/matplotlylib/mplexporter/tests/test_utils.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
from numpy.testing import assert_allclose, assert_equal
2-
import matplotlib.pyplot as plt
3-
from .. import utils
1+
# TODO: matplotlib-build-wip
2+
from nose.plugins.attrib import attr
3+
from plotly.tools import _matplotlylib_imported
44

5+
if _matplotlylib_imported:
6+
from numpy.testing import assert_allclose, assert_equal
7+
import matplotlib.pyplot as plt
8+
from .. import utils
59

10+
@attr('matplotlib')
611
def test_path_data():
712
circle = plt.Circle((0, 0), 1)
813
vertices, codes = utils.SVG_path(circle.get_path())

plotly/tests/test_optional/optional_utils.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
from __future__ import absolute_import
22

3-
import matplotlib
4-
# Force matplotlib to not use any Xwindows backend.
5-
matplotlib.use('Agg')
6-
73
import numpy as np
84

9-
from plotly.matplotlylib import Exporter, PlotlyRenderer
105
from plotly.tests.utils import is_num_list
116
from plotly.utils import get_by_path, node_generator
127

8+
# TODO: matplotlib-build-wip
9+
from plotly.tools import _matplotlylib_imported
10+
if _matplotlylib_imported:
11+
import matplotlib
12+
# Force matplotlib to not use any Xwindows backend.
13+
matplotlib.use('Agg')
14+
from plotly.matplotlylib import Exporter, PlotlyRenderer
15+
1316

1417
def run_fig(fig):
1518
renderer = PlotlyRenderer()

plotly/tests/test_optional/test_matplotlylib/test_annotations.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
from __future__ import absolute_import
22

3-
import matplotlib
4-
# Force matplotlib to not use any Xwindows backend.
5-
matplotlib.use('Agg')
6-
import matplotlib.pyplot as plt
3+
from nose.plugins.attrib import attr
74

8-
from plotly.tests.utils import compare_dict
9-
from plotly.tests.test_optional.optional_utils import run_fig
10-
from plotly.tests.test_optional.test_matplotlylib.data.annotations import *
5+
# TODO: matplotlib-build-wip
6+
from plotly.tools import _matplotlylib_imported
117

8+
if _matplotlylib_imported:
9+
import matplotlib
10+
# Force matplotlib to not use any Xwindows backend.
11+
matplotlib.use('Agg')
12+
import matplotlib.pyplot as plt
1213

14+
from plotly.tests.utils import compare_dict
15+
from plotly.tests.test_optional.optional_utils import run_fig
16+
from plotly.tests.test_optional.test_matplotlylib.data.annotations import *
17+
18+
19+
@attr('matplotlib')
1320
def test_annotations():
1421
fig, ax = plt.subplots()
1522
ax.plot([1, 2, 3], 'b-')

plotly/tests/test_optional/test_matplotlylib/test_axis_scales.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
from __future__ import absolute_import
22

3-
import matplotlib
4-
# Force matplotlib to not use any Xwindows backend.
5-
matplotlib.use('Agg')
6-
import matplotlib.pyplot as plt
3+
from nose.plugins.attrib import attr
74

85
from plotly.tests.utils import compare_dict
96
from plotly.tests.test_optional.optional_utils import run_fig
107
from plotly.tests.test_optional.test_matplotlylib.data.axis_scales import *
118

9+
# TODO: matplotlib-build-wip
10+
from plotly.tools import _matplotlylib_imported
1211

12+
if _matplotlylib_imported:
13+
import matplotlib
14+
# Force matplotlib to not use any Xwindows backend.
15+
matplotlib.use('Agg')
16+
import matplotlib.pyplot as plt
17+
18+
19+
@attr('matplotlib')
1320
def test_even_linear_scale():
1421
fig, ax = plt.subplots()
1522
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

plotly/tests/test_optional/test_matplotlylib/test_bars.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
from __future__ import absolute_import
22

3-
import matplotlib
4-
# Force matplotlib to not use any Xwindows backend.
5-
matplotlib.use('Agg')
6-
import matplotlib.pyplot as plt
3+
from nose.plugins.attrib import attr
74

85
from plotly.tests.utils import compare_dict
96
from plotly.tests.test_optional.optional_utils import run_fig
107
from plotly.tests.test_optional.test_matplotlylib.data.bars import *
118

9+
# TODO: matplotlib-build-wip
10+
from plotly.tools import _matplotlylib_imported
11+
if _matplotlylib_imported:
12+
import matplotlib
1213

14+
# Force matplotlib to not use any Xwindows backend.
15+
matplotlib.use('Agg')
16+
import matplotlib.pyplot as plt
17+
18+
19+
@attr('matplotlib')
1320
def test_vertical_bar():
1421
fig, ax = plt.subplots()
1522
ax.bar(left=D['left'], height=D['height'])
@@ -23,6 +30,7 @@ def test_vertical_bar():
2330
assert equivalent, msg
2431

2532

33+
@attr('matplotlib')
2634
def test_horizontal_bar():
2735
fig, ax = plt.subplots()
2836
ax.barh(bottom=D['bottom'], width=D['width'])
@@ -36,6 +44,7 @@ def test_horizontal_bar():
3644
assert equivalent, msg
3745

3846

47+
@attr('matplotlib')
3948
def test_h_and_v_bars():
4049
fig, ax = plt.subplots()
4150
ax.bar(left=D['multi_left'], height=D['multi_height'],

plotly/tests/test_optional/test_matplotlylib/test_data.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
from __future__ import absolute_import
22

3-
import matplotlib
4-
# Force matplotlib to not use any Xwindows backend.
5-
matplotlib.use('Agg')
6-
import matplotlib.pyplot as plt
3+
from nose.plugins.attrib import attr
74

85
from plotly.tests.test_optional.optional_utils import run_fig
96
from plotly.tests.test_optional.test_matplotlylib.data.data import *
107

8+
# TODO: matplotlib-build-wip
9+
from plotly.tools import _matplotlylib_imported
10+
if _matplotlylib_imported:
11+
import matplotlib
1112

13+
# Force matplotlib to not use any Xwindows backend.
14+
matplotlib.use('Agg')
15+
import matplotlib.pyplot as plt
16+
17+
18+
@attr('matplotlib')
1219
def test_line_data():
1320
fig, ax = plt.subplots()
1421
ax.plot(D['x1'], D['y1'])
@@ -21,6 +28,7 @@ def test_line_data():
2128
renderer.plotly_fig['data'][0]['y']) + ' is not ' + str(D['y1'])
2229

2330

31+
@attr('matplotlib')
2432
def test_lines_data():
2533
fig, ax = plt.subplots()
2634
ax.plot(D['x1'], D['y1'])
@@ -40,6 +48,7 @@ def test_lines_data():
4048
renderer.plotly_fig['data'][0]['y']) + ' is not ' + str(D['y2'])
4149

4250

51+
@attr('matplotlib')
4352
def test_bar_data():
4453
fig, ax = plt.subplots()
4554
ax.bar(D['x1'], D['y1'])
@@ -49,6 +58,7 @@ def test_bar_data():
4958
renderer.plotly_fig['data'][0]['y']) + ' is not ' + str(D['y1'])
5059

5160

61+
@attr('matplotlib')
5262
def test_bars_data():
5363
fig, ax = plt.subplots()
5464
ax.bar(D['x1'], D['y1'], color='r')

plotly/tests/test_optional/test_matplotlylib/test_date_times.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,25 @@
44
import random
55
from unittest import TestCase
66

7-
import matplotlib
8-
# Force matplotlib to not use any Xwindows backend.
9-
matplotlib.use('Agg')
10-
from matplotlib.dates import date2num
11-
import matplotlib.pyplot as plt
127
import pandas as pd
8+
from nose.plugins.attrib import attr
139

1410
import plotly.tools as tls
1511

12+
# TODO: matplotlib-build-wip
13+
from plotly.tools import _matplotlylib_imported
14+
if _matplotlylib_imported:
15+
import matplotlib
1616

17+
# Force matplotlib to not use any Xwindows backend.
18+
matplotlib.use('Agg')
19+
from matplotlib.dates import date2num
20+
import matplotlib.pyplot as plt
21+
22+
23+
@attr('matplotlib')
1724
class TestDateTimes(TestCase):
25+
1826
def test_normal_mpl_dates(self):
1927
datetime_format = '%Y-%m-%d %H:%M:%S'
2028
y = [1, 2, 3, 4]

plotly/tests/test_optional/test_matplotlylib/test_lines.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
11
from __future__ import absolute_import
22

3-
import matplotlib
4-
# Force matplotlib to not use any Xwindows backend.
5-
matplotlib.use('Agg')
6-
import matplotlib.pyplot as plt
3+
from nose.plugins.attrib import attr
74

85
from plotly.tests.utils import compare_dict
96
from plotly.tests.test_optional.optional_utils import run_fig
107
from plotly.tests.test_optional.test_matplotlylib.data.lines import *
118

9+
# TODO: matplotlib-build-wip
10+
from plotly.tools import _matplotlylib_imported
11+
if _matplotlylib_imported:
12+
import matplotlib
1213

14+
# Force matplotlib to not use any Xwindows backend.
15+
matplotlib.use('Agg')
16+
import matplotlib.pyplot as plt
17+
18+
19+
@attr('matplotlib')
1320
def test_simple_line():
1421
fig, ax = plt.subplots()
1522
ax.plot(D['x1'], D['y1'], label='simple')
@@ -22,6 +29,7 @@ def test_simple_line():
2229
assert equivalent, msg
2330

2431

32+
@attr('matplotlib')
2533
def test_complicated_line():
2634
fig, ax = plt.subplots()
2735
ax.plot(D['x1'], D['y1'], 'ro', markersize=10, alpha=.5, label='one')

0 commit comments

Comments
 (0)