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

Redesign check_figures_equal testing function to be more explicit #590

Merged
merged 2 commits into from
Sep 7, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 4 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -326,10 +326,13 @@ Here's an example:

```python
@check_figures_equal()
def test_my_plotting_case(fig_ref, fig_test):
def test_my_plotting_case():
"Test that my plotting function works"
fig_ref = Figure()
fig_ref.grdimage("@earth_relief_01d_g", projection="W120/15c", cmap="geo")
fig_test = Figure()
fig_test.grdimage(grid, projection="W120/15c", cmap="geo")
return fig_ref, fig_test
seisman marked this conversation as resolved.
Show resolved Hide resolved
```

Note: This is the recommended way to test plots whenever possible, such as when
Expand Down
16 changes: 10 additions & 6 deletions pygmt/helpers/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from matplotlib.testing.compare import compare_images

from ..exceptions import GMTImageComparisonFailure
from ..figure import Figure


def check_figures_equal(*, tol=0.0, result_dir="result_images"):
Expand Down Expand Up @@ -36,19 +35,26 @@ def check_figures_equal(*, tol=0.0, result_dir="result_images"):

>>> import pytest
>>> import shutil
>>> from pygmt import Figure

>>> @check_figures_equal(result_dir="tmp_result_images")
... def test_check_figures_equal(fig_ref, fig_test):
... def test_check_figures_equal():
... fig_ref = Figure()
... fig_ref.basemap(projection="X5c", region=[0, 5, 0, 5], frame=True)
... fig_test = Figure()
... fig_test.basemap(projection="X5c", region=[0, 5, 0, 5], frame="af")
... return fig_ref, fig_test
>>> test_check_figures_equal()
>>> assert len(os.listdir("tmp_result_images")) == 0
>>> shutil.rmtree(path="tmp_result_images") # cleanup folder if tests pass

>>> @check_figures_equal(result_dir="tmp_result_images")
... def test_check_figures_unequal(fig_ref, fig_test):
... def test_check_figures_unequal():
... fig_ref = Figure()
... fig_ref.basemap(projection="X5c", region=[0, 5, 0, 5], frame=True)
... fig_test = Figure()
... fig_test.basemap(projection="X5c", region=[0, 3, 0, 3], frame=True)
... return fig_ref, fig_test
>>> with pytest.raises(GMTImageComparisonFailure):
... test_check_figures_unequal()
>>> for suffix in ["", "-expected", "-failed-diff"]:
Expand All @@ -68,9 +74,7 @@ def decorator(func):

def wrapper(*args, **kwargs):
try:
fig_ref = Figure()
fig_test = Figure()
func(*args, fig_ref=fig_ref, fig_test=fig_test, **kwargs)
fig_ref, fig_test = func(*args, **kwargs)
ref_image_path = os.path.join(
result_dir, func.__name__ + "-expected.png"
)
Expand Down
5 changes: 4 additions & 1 deletion pygmt/tests/test_grdimage.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,12 @@ def test_grdimage_over_dateline(xrgrid):


@check_figures_equal()
def test_grdimage_central_longitude(grid, fig_ref, fig_test):
def test_grdimage_central_longitude(grid):
"""
Test that plotting a grid centred at different longitudes/meridians work.
"""
fig_ref = Figure()
fig_ref.grdimage("@earth_relief_01d_g", projection="W120/15c", cmap="geo")
fig_test = Figure()
fig_test.grdimage(grid, projection="W120/15c", cmap="geo")
return fig_ref, fig_test