Skip to content

Commit af15e86

Browse files
committed
Make testing more robust to presence of gs/inkscape and Matplotlib version
1 parent c1eaec0 commit af15e86

File tree

1 file changed

+22
-24
lines changed

1 file changed

+22
-24
lines changed

tests/test_pytest_mpl.py

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import pytest
1212
from packaging.version import Version
1313

14+
from matplotlib.testing.compare import converter
15+
1416
MPL_VERSION = Version(matplotlib.__version__)
1517

1618
baseline_dir = 'baseline'
@@ -672,48 +674,44 @@ def test_raises():
672674
result.stdout.fnmatch_lines("FAILED*ValueError*User code*")
673675

674676

677+
@pytest.mark.parametrize('use_hash_library', (False, True))
678+
@pytest.mark.parametrize('passes', (False, True))
675679
@pytest.mark.parametrize("file_format", ['eps', 'pdf', 'png', 'svg'])
676-
def test_formats(pytester, file_format):
680+
def test_formats(pytester, use_hash_library, passes, file_format):
677681
"""
678682
Note that we don't test all possible formats as some do not compress well
679683
and would bloat the baseline directory.
680684
"""
681-
pytester.makepyfile(
682-
f"""
683-
import pytest
684-
import matplotlib.pyplot as plt
685-
@pytest.mark.mpl_image_compare(baseline_dir='{baseline_dir_abs}',
686-
tolerance={DEFAULT_TOLERANCE},
687-
savefig_kwargs={{'format': '{file_format}'}})
688-
def test_format_{file_format}():
689-
fig = plt.figure()
690-
ax = fig.add_subplot(1, 1, 1)
691-
ax.plot([1, 2, 3])
692-
return fig
693-
"""
694-
)
695-
result = pytester.runpytest('--mpl', '-rs')
696-
result.assert_outcomes(passed=1)
697685

686+
if file_format == 'svg' and MPL_VERSION < Version('3.3'):
687+
pytest.skip('SVG comparison is only supported in Matplotlib 3.3 and above')
688+
689+
if file_format != 'png' and file_format not in converter:
690+
if file_format == 'svg':
691+
pytest.skip('Comparing SVG files requires inkscape to be installed')
692+
else:
693+
pytest.skip('Comparing EPS and PDF files requires ghostscript to be installed')
694+
695+
if use_hash_library:
696+
pytest.skip('Using the hash library does not currently work because the hashes are not deterministic')
698697

699-
@pytest.mark.parametrize("file_format", ['eps', 'pdf', 'png', 'svg'])
700-
def test_formats_check_fail(pytester, file_format):
701-
"""
702-
As for test_formats but make sure the tests fail if there are differences
703-
"""
704698
pytester.makepyfile(
705699
f"""
706700
import pytest
707701
import matplotlib.pyplot as plt
708702
@pytest.mark.mpl_image_compare(baseline_dir='{baseline_dir_abs}',
703+
{f'hash_library="{hash_library}",' if use_hash_library else ''}
709704
tolerance={DEFAULT_TOLERANCE},
710705
savefig_kwargs={{'format': '{file_format}'}})
711706
def test_format_{file_format}():
712707
fig = plt.figure()
713708
ax = fig.add_subplot(1, 1, 1)
714-
ax.plot([3, 2, 3])
709+
ax.plot([{1 if passes else 3}, 2, 3])
715710
return fig
716711
"""
717712
)
718713
result = pytester.runpytest('--mpl', '-rs')
719-
result.assert_outcomes(failed=1)
714+
if passes:
715+
result.assert_outcomes(passed=1)
716+
else:
717+
result.assert_outcomes(failed=1)

0 commit comments

Comments
 (0)