Skip to content

Commit 203e647

Browse files
seismanweiji14
andauthored
Refactor xfail tests to avoid storing baseline images (#603)
This PR refactors the xfail/fail tests to let the tests pass and avoid storing baseline images. The baseline images are generated by calling the same modules using plain text or grid files as input, and using single-character arguments. * Refactor basemap tests * Refactor legend tests * Refactor coast tests * Refactor colorbar tests * Refactor makecpt tests * Refactor logo tests * Refactor grdcontour tests * Refactor plot tests Co-authored-by: Wei Ji <[email protected]>
1 parent 713ea72 commit 203e647

8 files changed

+114
-66
lines changed

pygmt/tests/test_basemap.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pytest
55

66
from .. import Figure
7+
from ..helpers.testing import check_figures_equal
78
from ..exceptions import GMTInvalidInput
89

910

@@ -54,15 +55,15 @@ def test_basemap_power_axis():
5455
return fig
5556

5657

57-
@pytest.mark.xfail(
58-
reason="Baseline image not updated to use earth relief grid in GMT 6.1.0",
59-
)
60-
@pytest.mark.mpl_image_compare
58+
@check_figures_equal()
6159
def test_basemap_polar():
6260
"Create a polar basemap plot"
63-
fig = Figure()
64-
fig.basemap(R="0/360/0/1000", J="P6i", B="afg")
65-
return fig
61+
fig_ref, fig_test = Figure(), Figure()
62+
# Use single-character arguments for the reference image
63+
fig_ref.basemap(R="0/360/0/1000", J="P6i", B="afg")
64+
fig_test.basemap(region=[0, 360, 0, 1000], projection="P6i", frame="afg")
65+
66+
return fig_ref, fig_test
6667

6768

6869
@pytest.mark.mpl_image_compare

pygmt/tests/test_coast.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pytest
55

66
from .. import Figure
7+
from ..helpers.testing import check_figures_equal
78

89

910
@pytest.mark.mpl_image_compare
@@ -25,15 +26,16 @@ def test_coast():
2526
return fig
2627

2728

28-
@pytest.mark.xfail(
29-
reason="Baseline image not updated to use earth relief grid in GMT 6.1.0",
30-
)
31-
@pytest.mark.mpl_image_compare
29+
@check_figures_equal()
3230
def test_coast_iceland():
3331
"Test passing in R as a list"
34-
fig = Figure()
35-
fig.coast(R=[-30, -10, 60, 65], J="m1c", B=True, G="p28+r100")
36-
return fig
32+
fig_ref, fig_test = Figure(), Figure()
33+
# Use single-character arguments for the reference image
34+
fig_ref.coast(R="-30/-10/60/65", J="m1c", B="", G="p28+r100")
35+
fig_test.coast(
36+
region=[-30, -10, 60, 65], projection="m1c", frame=True, land="p28+r100"
37+
)
38+
return fig_ref, fig_test
3739

3840

3941
@pytest.mark.mpl_image_compare

pygmt/tests/test_colorbar.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pytest
55

66
from .. import Figure
7+
from ..helpers.testing import check_figures_equal
78

89

910
@pytest.mark.mpl_image_compare
@@ -37,18 +38,19 @@ def test_colorbar_positioned_using_map_coordinates():
3738
return fig
3839

3940

40-
@pytest.mark.xfail(
41-
reason="Baseline image not updated to use earth relief grid in GMT 6.1.0",
42-
)
43-
@pytest.mark.mpl_image_compare
41+
@check_figures_equal()
4442
def test_colorbar_positioned_using_justification_code():
4543
"""
4644
Create colorbar at Top Center inside the map frame with length 2cm.
4745
"""
48-
fig = Figure()
49-
fig.basemap(region=[2, 4, 6, 8], projection="t0/2c", frame=True)
50-
fig.colorbar(cmap="rainbow", position="jTC+w2c")
51-
return fig
46+
fig_ref, fig_test = Figure(), Figure()
47+
# Use single-character arguments for the reference image
48+
fig_ref.basemap(R="2/4/6/8", J="t0/2c", B="")
49+
fig_ref.colorbar(C="rainbow", D="jTC+w2c")
50+
51+
fig_test.basemap(region=[2, 4, 6, 8], projection="t0/2c", frame=True)
52+
fig_test.colorbar(cmap="rainbow", position="jTC+w2c")
53+
return fig_ref, fig_test
5254

5355

5456
@pytest.mark.mpl_image_compare

pygmt/tests/test_grdcontour.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,26 +82,36 @@ def test_grdcontour_file():
8282
return fig
8383

8484

85-
@pytest.mark.xfail(
86-
reason="Baseline image not updated to use earth relief grid in GMT 6.1.0",
87-
)
88-
@pytest.mark.mpl_image_compare
85+
@check_figures_equal()
8986
def test_grdcontour_interval_file_full_opts():
9087
""" Plot based on external contour level file """
91-
fig = Figure()
92-
comargs = {
88+
fig_ref, fig_test = Figure(), Figure()
89+
# Use single-character arguments for the reference image
90+
comargs_ref = {
91+
"grid": "@earth_relief_10m",
92+
"R": "-161.5/-154/18.5/23",
93+
"C": TEST_CONTOUR_FILE,
94+
"S": 100,
95+
"J": "M6i",
96+
"Q": 10,
97+
}
98+
fig_ref.grdcontour(**comargs_ref, L="-25000/-1", W=["a1p,blue", "c0.5p,blue"])
99+
fig_ref.grdcontour(**comargs_ref, L="0", W=["a1p,black", "c0.5p,black"])
100+
101+
comargs_test = {
93102
"region": [-161.5, -154, 18.5, 23],
94103
"interval": TEST_CONTOUR_FILE,
95104
"grid": "@earth_relief_10m",
96105
"resample": "100",
97106
"projection": "M6i",
98107
"cut": 10,
99108
}
109+
fig_test.grdcontour(
110+
**comargs_test, limit=(-25000, -1), pen=["a1p,blue", "c0.5p,blue"]
111+
)
112+
fig_test.grdcontour(**comargs_test, limit=0, pen=["a1p,black", "c0.5p,black"])
100113

101-
fig.grdcontour(**comargs, limit=(-25000, -1), pen=["a1p,blue", "c0.5p,blue"])
102-
103-
fig.grdcontour(**comargs, limit="0", pen=["a1p,black", "c0.5p,black"])
104-
return fig
114+
return fig_ref, fig_test
105115

106116

107117
def test_grdcontour_fails():

pygmt/tests/test_legend.py

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from .. import Figure
77
from ..exceptions import GMTInvalidInput
88
from ..helpers import GMTTempFile
9+
from ..helpers.testing import check_figures_equal
910

1011

1112
@pytest.mark.mpl_image_compare
@@ -44,32 +45,42 @@ def test_legend_default_position():
4445
return fig
4546

4647

47-
@pytest.mark.xfail(
48-
reason="Baseline image not updated to use earth relief grid in GMT 6.1.0",
49-
)
50-
@pytest.mark.mpl_image_compare
48+
@check_figures_equal()
5149
def test_legend_entries():
5250
"""
5351
Test different marker types/shapes.
5452
"""
53+
fig_ref, fig_test = Figure(), Figure()
5554

56-
fig = Figure()
57-
58-
fig.basemap(projection="x1i", region=[0, 7, 3, 7], frame=True)
55+
# Use single-character arguments for the reference image
56+
fig_ref = Figure()
57+
fig_ref.basemap(J="x1i", R="0/7/3/7", B="")
58+
fig_ref.plot(
59+
data="@Table_5_11.txt",
60+
S="c0.15i",
61+
G="lightgreen",
62+
W="faint",
63+
l="Apples",
64+
)
65+
fig_ref.plot(data="@Table_5_11.txt", W="1.5p,gray", l='"My lines"')
66+
fig_ref.plot(data="@Table_5_11.txt", S="t0.15i", G="orange", l="Oranges")
67+
fig_ref.legend(D="JTR+jTR")
5968

60-
fig.plot(
69+
fig_test.basemap(projection="x1i", region=[0, 7, 3, 7], frame=True)
70+
fig_test.plot(
6171
data="@Table_5_11.txt",
6272
style="c0.15i",
6373
color="lightgreen",
6474
pen="faint",
65-
l="Apples",
75+
label="Apples",
6676
)
67-
fig.plot(data="@Table_5_11.txt", pen="1.5p,gray", label='"My lines"')
68-
fig.plot(data="@Table_5_11.txt", style="t0.15i", color="orange", label="Oranges")
69-
70-
fig.legend(position="JTR+jTR")
77+
fig_test.plot(data="@Table_5_11.txt", pen="1.5p,gray", label='"My lines"')
78+
fig_test.plot(
79+
data="@Table_5_11.txt", style="t0.15i", color="orange", label="Oranges"
80+
)
81+
fig_test.legend(position="JTR+jTR")
7182

72-
return fig
83+
return fig_ref, fig_test
7384

7485

7586
@pytest.mark.mpl_image_compare

pygmt/tests/test_logo.py

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,32 @@
55

66
from .. import Figure
77
from ..exceptions import GMTInvalidInput
8+
from ..helpers.testing import check_figures_equal
89

910

10-
@pytest.mark.mpl_image_compare
11+
@check_figures_equal()
1112
def test_logo():
1213
"Plot a GMT logo of a 2 inch width as a stand-alone plot"
13-
fig = Figure()
14-
fig.logo(D="x0/0+w2i")
15-
return fig
14+
fig_ref, fig_test = Figure(), Figure()
15+
# Use single-character arguments for the reference image
16+
fig_ref.logo(D="x0/0+w2i")
17+
fig_test.logo(position="x0/0+w2i")
18+
return fig_ref, fig_test
1619

1720

18-
@pytest.mark.mpl_image_compare
21+
@check_figures_equal()
1922
def test_logo_on_a_map():
2023
"Plot a GMT logo in the upper right corner of a map"
21-
fig = Figure()
22-
fig.coast(region=[-90, -70, 0, 20], projection="M6i", land="chocolate", frame=True)
23-
fig.logo(D="jTR+o0.1i/0.1i+w3i", F=True)
24-
return fig
24+
fig_ref, fig_test = Figure(), Figure()
25+
# Use single-character arguments for the reference image
26+
fig_ref.coast(R="-90/-70/0/20", J="M6i", G="chocolate", B="")
27+
fig_ref.logo(D="jTR+o0.1i/0.1i+w3i", F="")
28+
29+
fig_test.coast(
30+
region=[-90, -70, 0, 20], projection="M6i", land="chocolate", frame=True
31+
)
32+
fig_test.logo(position="jTR+o0.1i/0.1i+w3i", box=True)
33+
return fig_ref, fig_test
2534

2635

2736
def test_logo_fails():

pygmt/tests/test_makecpt.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
from ..datasets import load_earth_relief
1111
from ..exceptions import GMTInvalidInput
1212
from ..helpers import GMTTempFile
13+
from ..helpers.testing import check_figures_equal
1314

1415
TEST_DATA_DIR = os.path.join(os.path.dirname(__file__), "data")
1516
POINTS_DATA = os.path.join(TEST_DATA_DIR, "points.txt")
@@ -62,19 +63,21 @@ def test_makecpt_to_plot_grid(grid):
6263
return fig
6364

6465

65-
@pytest.mark.xfail(
66-
reason="Baseline image not updated to use earth relief grid in GMT 6.1.0",
67-
)
68-
@pytest.mark.mpl_image_compare
66+
@check_figures_equal()
6967
def test_makecpt_to_plot_grid_scaled_with_series(grid):
7068
"""
7169
Use static color palette table scaled to a min/max series to change color
7270
of grid
7371
"""
74-
fig = Figure()
72+
# Use single-character arguments for the reference image
73+
fig_ref = Figure()
74+
makecpt(C="oleron", T="-4500/4500")
75+
fig_ref.grdimage(grid, J="W0/6i")
76+
77+
fig_test = Figure()
7578
makecpt(cmap="oleron", series="-4500/4500")
76-
fig.grdimage(grid, projection="W0/6i")
77-
return fig
79+
fig_test.grdimage(grid, projection="W0/6i")
80+
return fig_ref, fig_test
7881

7982

8083
def test_makecpt_output_to_cpt_file():

pygmt/tests/test_plot.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,21 @@ def test_plot_projection(data):
128128
return fig
129129

130130

131-
@pytest.mark.mpl_image_compare
131+
@check_figures_equal()
132132
def test_plot_colors(data, region):
133133
"Plot the data using z as colors"
134-
fig = Figure()
135-
fig.plot(
134+
fig_ref, fig_test = Figure(), Figure()
135+
# Use single-character arguments for the reference image
136+
fig_ref.plot(
137+
data=POINTS_DATA,
138+
R="/".join(map(str, region)),
139+
J="X3i",
140+
S="c0.5c",
141+
C="cubhelix",
142+
B="af",
143+
)
144+
145+
fig_test.plot(
136146
x=data[:, 0],
137147
y=data[:, 1],
138148
color=data[:, 2],
@@ -142,7 +152,7 @@ def test_plot_colors(data, region):
142152
cmap="cubhelix",
143153
frame="af",
144154
)
145-
return fig
155+
return fig_ref, fig_test
146156

147157

148158
@pytest.mark.mpl_image_compare

0 commit comments

Comments
 (0)