Skip to content

Commit 5a17858

Browse files
authored
Redesign check_figures_equal testing function to be more explicit (#590)
Improve readability of test code by explicitly declaring fig_ref and fig_test as pygmt.Figure objects, and returning the two figures to be compared against. Makes each testing code more standalone, though it will become 3 lines longer.
1 parent 53eb1b5 commit 5a17858

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

CONTRIBUTING.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -326,10 +326,13 @@ Here's an example:
326326

327327
```python
328328
@check_figures_equal()
329-
def test_my_plotting_case(fig_ref, fig_test):
329+
def test_my_plotting_case():
330330
"Test that my plotting function works"
331+
fig_ref = Figure()
331332
fig_ref.grdimage("@earth_relief_01d_g", projection="W120/15c", cmap="geo")
333+
fig_test = Figure()
332334
fig_test.grdimage(grid, projection="W120/15c", cmap="geo")
335+
return fig_ref, fig_test
333336
```
334337

335338
Note: This is the recommended way to test plots whenever possible, such as when

pygmt/helpers/testing.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from matplotlib.testing.compare import compare_images
99

1010
from ..exceptions import GMTImageComparisonFailure
11-
from ..figure import Figure
1211

1312

1413
def check_figures_equal(*, tol=0.0, result_dir="result_images"):
@@ -36,19 +35,26 @@ def check_figures_equal(*, tol=0.0, result_dir="result_images"):
3635
3736
>>> import pytest
3837
>>> import shutil
38+
>>> from pygmt import Figure
3939
4040
>>> @check_figures_equal(result_dir="tmp_result_images")
41-
... def test_check_figures_equal(fig_ref, fig_test):
41+
... def test_check_figures_equal():
42+
... fig_ref = Figure()
4243
... fig_ref.basemap(projection="X5c", region=[0, 5, 0, 5], frame=True)
44+
... fig_test = Figure()
4345
... fig_test.basemap(projection="X5c", region=[0, 5, 0, 5], frame="af")
46+
... return fig_ref, fig_test
4447
>>> test_check_figures_equal()
4548
>>> assert len(os.listdir("tmp_result_images")) == 0
4649
>>> shutil.rmtree(path="tmp_result_images") # cleanup folder if tests pass
4750
4851
>>> @check_figures_equal(result_dir="tmp_result_images")
49-
... def test_check_figures_unequal(fig_ref, fig_test):
52+
... def test_check_figures_unequal():
53+
... fig_ref = Figure()
5054
... fig_ref.basemap(projection="X5c", region=[0, 5, 0, 5], frame=True)
55+
... fig_test = Figure()
5156
... fig_test.basemap(projection="X5c", region=[0, 3, 0, 3], frame=True)
57+
... return fig_ref, fig_test
5258
>>> with pytest.raises(GMTImageComparisonFailure):
5359
... test_check_figures_unequal()
5460
>>> for suffix in ["", "-expected", "-failed-diff"]:
@@ -68,9 +74,7 @@ def decorator(func):
6874

6975
def wrapper(*args, **kwargs):
7076
try:
71-
fig_ref = Figure()
72-
fig_test = Figure()
73-
func(*args, fig_ref=fig_ref, fig_test=fig_test, **kwargs)
77+
fig_ref, fig_test = func(*args, **kwargs)
7478
ref_image_path = os.path.join(
7579
result_dir, func.__name__ + "-expected.png"
7680
)

pygmt/tests/test_grdimage.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,12 @@ def test_grdimage_over_dateline(xrgrid):
9393

9494

9595
@check_figures_equal()
96-
def test_grdimage_central_longitude(grid, fig_ref, fig_test):
96+
def test_grdimage_central_longitude(grid):
9797
"""
9898
Test that plotting a grid centred at different longitudes/meridians work.
9999
"""
100+
fig_ref = Figure()
100101
fig_ref.grdimage("@earth_relief_01d_g", projection="W120/15c", cmap="geo")
102+
fig_test = Figure()
101103
fig_test.grdimage(grid, projection="W120/15c", cmap="geo")
104+
return fig_ref, fig_test

0 commit comments

Comments
 (0)