Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add image comparison tests using pytest-mpl #128

Merged
merged 3 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ jobs:
shell: bash -l {0}
run: |
pytest -v \
--mpl \
--mpl-results-path=./mpl-results \
--mpl-generate-summary html \
--junitxml=junit-py${{ matrix.python-version }}.xml \
--cov --cov-report term \
--cov-report xml:coverage-${{ matrix.python-version }}.xml
Expand All @@ -92,7 +95,16 @@ jobs:
if: always()
with:
report_paths: 'junit-py*.xml'
check_name: "JUnit Test Report - Python ${{ matrix.python-version }}"
check_name: "JUnit Test Report - python ${{ matrix.python-version }}, ${{ matrix.experimental && 'latest' || 'pinned' }} dependencies"

- name: MPL image comparison report
uses: actions/upload-artifact@v3
if: ${{ failure() }}
with:
name: "MPL image comparison report - python ${{ matrix.python-version }}, ${{ matrix.experimental && 'latest' || 'pinned' }} dependencies"
path: './mpl-results'
# No guarantee that the test failures were due to image comparisons
if-no-files-found: 'ignore'

- uses: actions/upload-artifact@v3
with:
Expand Down
7 changes: 7 additions & 0 deletions continuous-integration/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ jinja2==3.1.2
# bokeh
# dask
# distributed
# pytest-mpl
# sphinx
kiwisolver==1.4.4
# via matplotlib
Expand All @@ -115,6 +116,7 @@ matplotlib==3.7.2
# via
# cartopy
# emsarray (setup.cfg)
# pytest-mpl
mccabe==0.7.0
# via flake8
msgpack==1.0.5
Expand Down Expand Up @@ -156,6 +158,7 @@ packaging==23.1
# pydata-sphinx-theme
# pyproject-api
# pytest
# pytest-mpl
# sphinx
# tox
# xarray
Expand All @@ -172,6 +175,7 @@ pillow==10.0.0
# via
# bokeh
# matplotlib
# pytest-mpl
platformdirs==3.10.0
# via
# pooch
Expand Down Expand Up @@ -214,8 +218,11 @@ pytest==7.4.0
# via
# emsarray (setup.cfg)
# pytest-cov
# pytest-mpl
pytest-cov==4.1.0
# via emsarray (setup.cfg)
pytest-mpl==0.16.1
# via emsarray (setup.cfg)
python-dateutil==2.8.2
# via
# matplotlib
Expand Down
3 changes: 3 additions & 0 deletions docs/releases/development.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,6 @@ Next release (in development)
* Use `PEP 585 generic type annotations <https://peps.python.org/pep-0585/>`_
and stop using `PEP 563 postponed annotation evaluation <https://peps.python.org/pep-0563/>`_
(:issue:`109`, :pr:`127`).
* Use `pytest-mpl <https://pypi.org/project/pytest-mpl/>`_ for image comparison tests
for testing plotting methods
(:pr:`128`).
11 changes: 11 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ requires = [
build-backend = "setuptools.build_meta"

[tool.pytest.ini_options]
addopts = [
# TODO Work around till the mpl-baseline-path ini option is included in the
# next pytest-mpl release.
"--mpl-baseline-path", "tests/baseline_images",
]

testpaths = ["tests"]
filterwarnings = [
"error",
Expand All @@ -18,6 +24,11 @@ markers = [
"tutorial: Tests that involve the tutorial datasets",
]

mpl-use-full-test-name = true
# TODO This option will be included in the next version of pytest-mpl.
# Till then, it is added using addopts above
# mpl-baseline-path = "tests/baseline_images"

[tool.mypy]
python_version = "3.9"
plugins = ["numpy.typing.mypy_plugin"]
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ testing =
%(docs)s
pytest
pytest-cov
pytest-mpl
mypy
pandas-stubs
types-pytz
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions tests/test_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def test_add_landmarks():
assert text.xy == landmark[1].coords.xy


@pytest.mark.mpl_image_compare
@pytest.mark.matplotlib(mock_coast=True)
@pytest.mark.tutorial
def test_plot(
Expand All @@ -68,8 +69,7 @@ def test_plot(
axes = figure.axes[0]
assert axes.get_title() == 'Temperature\n2022-05-11T14:00'

matplotlib.pyplot.savefig(tmp_path / 'plot.png')
logger.info("Saved plot to %r", tmp_path / 'plot.png')
return figure


@pytest.mark.matplotlib(mock_coast=True)
Expand Down
10 changes: 5 additions & 5 deletions tests/test_transect.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
logger = logging.getLogger(__name__)


@pytest.mark.matplotlib(mock_coast=True)
@pytest.mark.mpl_image_compare
@pytest.mark.matplotlib
@pytest.mark.tutorial
def test_plot(
datasets: pathlib.Path,
Expand Down Expand Up @@ -53,10 +54,10 @@ def test_plot(
# This is the variable units
assert colorbar.get_ylabel() == 'degrees C'

matplotlib.pyplot.savefig(tmp_path / 'plot.png')
logger.info("Saved plot to %r", tmp_path / 'plot.png')
return figure


@pytest.mark.mpl_image_compare
@pytest.mark.matplotlib(mock_coast=True)
@pytest.mark.tutorial
def test_plot_no_intersection(
Expand Down Expand Up @@ -95,5 +96,4 @@ def test_plot_no_intersection(
# This is the variable units
assert colorbar.get_ylabel() == 'degrees C'

matplotlib.pyplot.savefig(tmp_path / 'plot.png')
logger.info("Saved plot to %r", tmp_path / 'plot.png')
return figure
Loading