|
11 | 11 | import pytest
|
12 | 12 | from packaging.version import Version
|
13 | 13 |
|
| 14 | +from matplotlib.testing.compare import converter |
| 15 | + |
14 | 16 | MPL_VERSION = Version(matplotlib.__version__)
|
15 | 17 |
|
16 | 18 | baseline_dir = 'baseline'
|
@@ -672,48 +674,44 @@ def test_raises():
|
672 | 674 | result.stdout.fnmatch_lines("FAILED*ValueError*User code*")
|
673 | 675 |
|
674 | 676 |
|
| 677 | +@pytest.mark.parametrize('use_hash_library', (False, True)) |
| 678 | +@pytest.mark.parametrize('passes', (False, True)) |
675 | 679 | @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): |
677 | 681 | """
|
678 | 682 | Note that we don't test all possible formats as some do not compress well
|
679 | 683 | and would bloat the baseline directory.
|
680 | 684 | """
|
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) |
697 | 685 |
|
| 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') |
698 | 697 |
|
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 |
| - """ |
704 | 698 | pytester.makepyfile(
|
705 | 699 | f"""
|
706 | 700 | import pytest
|
707 | 701 | import matplotlib.pyplot as plt
|
708 | 702 | @pytest.mark.mpl_image_compare(baseline_dir='{baseline_dir_abs}',
|
| 703 | + {f'hash_library="{hash_library}",' if use_hash_library else ''} |
709 | 704 | tolerance={DEFAULT_TOLERANCE},
|
710 | 705 | savefig_kwargs={{'format': '{file_format}'}})
|
711 | 706 | def test_format_{file_format}():
|
712 | 707 | fig = plt.figure()
|
713 | 708 | ax = fig.add_subplot(1, 1, 1)
|
714 |
| - ax.plot([3, 2, 3]) |
| 709 | + ax.plot([{1 if passes else 3}, 2, 3]) |
715 | 710 | return fig
|
716 | 711 | """
|
717 | 712 | )
|
718 | 713 | 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