Skip to content

Commit 4f860ec

Browse files
committed
Fix tests on Windows
1 parent de262f6 commit 4f860ec

File tree

2 files changed

+32
-26
lines changed

2 files changed

+32
-26
lines changed

tests/test_pytest_mpl.py

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,21 @@
2222

2323
WIN = sys.platform.startswith('win')
2424

25+
# In some cases, the fonts on Windows can be quite different
26+
DEFAULT_TOLERANCE = 10 if WIN else None
2527

26-
@pytest.mark.mpl_image_compare(baseline_dir=baseline_dir_local)
28+
29+
@pytest.mark.mpl_image_compare(baseline_dir=baseline_dir_local,
30+
tolerance=DEFAULT_TOLERANCE)
2731
def test_succeeds():
2832
fig = plt.figure()
2933
ax = fig.add_subplot(1, 1, 1)
3034
ax.plot([1, 2, 3])
3135
return fig
3236

3337

34-
@pytest.mark.mpl_image_compare(baseline_dir=baseline_dir_remote)
38+
@pytest.mark.mpl_image_compare(baseline_dir=baseline_dir_remote,
39+
tolerance=DEFAULT_TOLERANCE)
3540
def test_succeeds_remote():
3641
fig = plt.figure()
3742
ax = fig.add_subplot(1, 1, 1)
@@ -42,7 +47,8 @@ def test_succeeds_remote():
4247
# The following tries an invalid URL first (or at least a URL where the baseline
4348
# image won't exist), but should succeed with the second mirror.
4449
@pytest.mark.mpl_image_compare(baseline_dir='http://www.python.org,' + baseline_dir_remote,
45-
filename='test_succeeds_remote.png')
50+
filename='test_succeeds_remote.png',
51+
tolerance=DEFAULT_TOLERANCE)
4652
def test_succeeds_faulty_mirror():
4753
fig = plt.figure()
4854
ax = fig.add_subplot(1, 1, 1)
@@ -52,15 +58,18 @@ def test_succeeds_faulty_mirror():
5258

5359
class TestClass(object):
5460

55-
@pytest.mark.mpl_image_compare(baseline_dir=baseline_dir_local)
61+
@pytest.mark.mpl_image_compare(baseline_dir=baseline_dir_local,
62+
tolerance=DEFAULT_TOLERANCE)
5663
def test_succeeds(self):
5764
fig = plt.figure()
5865
ax = fig.add_subplot(1, 1, 1)
5966
ax.plot([1, 2, 3])
6067
return fig
6168

6269

63-
@pytest.mark.mpl_image_compare(baseline_dir=baseline_dir_local, savefig_kwargs={'dpi': 30})
70+
@pytest.mark.mpl_image_compare(baseline_dir=baseline_dir_local,
71+
savefig_kwargs={'dpi': 30},
72+
tolerance=DEFAULT_TOLERANCE)
6473
def test_dpi():
6574
fig = plt.figure()
6675
ax = fig.add_subplot(1, 1, 1)
@@ -87,11 +96,11 @@ def test_fails(tmpdir):
8796
f.write(TEST_FAILING)
8897

8998
# If we use --mpl, it should detect that the figure is wrong
90-
code = subprocess.call('{0} -m pytest --mpl {1}'.format(sys.executable, test_file), shell=True)
99+
code = subprocess.call([sys.executable, '-m', 'pytest', '--mpl', test_file])
91100
assert code != 0
92101

93102
# If we don't use --mpl option, the test should succeed
94-
code = subprocess.call('{0} -m pytest {1}'.format(sys.executable, test_file), shell=True)
103+
code = subprocess.call([sys.executable, '-m', 'pytest', test_file])
95104
assert code == 0
96105

97106

@@ -114,9 +123,9 @@ def test_output_dir(tmpdir):
114123

115124
# When we run the test, we should get output images where we specify
116125
output_dir = tmpdir.join('test_output_dir').strpath
117-
code = subprocess.call('{0} -m pytest --mpl-results-path={1} --mpl {2}'
118-
.format(sys.executable, output_dir, test_file),
119-
shell=True)
126+
code = subprocess.call([sys.executable, '-m', 'pytest',
127+
'--mpl-results-path={0}'.format(output_dir),
128+
'--mpl', test_file])
120129

121130
assert code != 0
122131
assert os.path.exists(output_dir)
@@ -138,12 +147,6 @@ def test_gen():
138147
"""
139148

140149

141-
# TODO: We skip the following test on Windows since the first subprocess calls.
142-
# This should be fixed in the long term, but is not critical since we already
143-
# test this on Linux.
144-
145-
146-
@pytest.mark.skipif("WIN")
147150
def test_generate(tmpdir):
148151

149152
test_file = tmpdir.join('test.py').strpath
@@ -153,14 +156,15 @@ def test_generate(tmpdir):
153156
gen_dir = tmpdir.mkdir('spam').mkdir('egg').strpath
154157

155158
# If we don't generate, the test will fail
156-
p = subprocess.Popen('{0} -m pytest --mpl {1}'.format(sys.executable, test_file), shell=True,
157-
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
158-
p.wait()
159-
assert b'Image file not found for comparison test' in p.stdout.read()
159+
try:
160+
subprocess.check_output([sys.executable, '-m', 'pytest', '--mpl', test_file])
161+
except subprocess.CalledProcessError as exc:
162+
assert b'Image file not found for comparison test' in exc.output
160163

161164
# If we do generate, the test should succeed and a new file will appear
162-
code = subprocess.call('{0} -m pytest --mpl-generate-path={1} {2}'
163-
.format(sys.executable, gen_dir, test_file), shell=True)
165+
code = subprocess.call([sys.executable, '-m', 'pytest',
166+
'--mpl-generate-path={0}'.format(gen_dir),
167+
test_file])
164168
assert code == 0
165169
assert os.path.exists(os.path.join(gen_dir, 'test_gen.png'))
166170

@@ -180,7 +184,8 @@ def test_nofigure():
180184
@pytest.mark.skipif(MPL_LT_2, reason="the fivethirtyeight style is only available "
181185
"in Matplotlib 2.0 and later")
182186
@pytest.mark.mpl_image_compare(baseline_dir=baseline_dir_local,
183-
style='fivethirtyeight')
187+
style='fivethirtyeight',
188+
tolerance=DEFAULT_TOLERANCE)
184189
def test_base_style():
185190
fig = plt.figure()
186191
ax = fig.add_subplot(1, 1, 1)
@@ -215,7 +220,8 @@ def setup_method(self, method):
215220
self.x = [1, 2, 3]
216221

217222
@pytest.mark.mpl_image_compare(baseline_dir=baseline_dir_local,
218-
filename='test_succeeds.png')
223+
filename='test_succeeds.png',
224+
tolerance=DEFAULT_TOLERANCE)
219225
def test_succeeds(self):
220226
fig = plt.figure()
221227
ax = fig.add_subplot(1, 1, 1)

tox.ini

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ extras =
2727
commands =
2828
pip freeze
2929
# Make sure the tests pass with and without --mpl
30-
!minimal: pytest {toxinidir}/tests --cov pytest_mpl --cov-config={toxinidir}/setup.cfg {posargs}
31-
!minimal: pytest {toxinidir}/tests --mpl --cov pytest_mpl --cov-config={toxinidir}/setup.cfg {posargs}
30+
!minimal: pytest '{toxinidir}' {posargs}
31+
!minimal: pytest '{toxinidir}' --mpl --cov pytest_mpl {posargs}
3232
# Make sure that the plugin won't crash pytest if Matplotlib isn't installed
3333
minimal: python -c 'import pytest_mpl.plugin'
3434

0 commit comments

Comments
 (0)