@@ -310,8 +310,38 @@ Leave a comment in the PR and we'll help you out.
310
310
311
311
### Testing plots
312
312
313
- We use the [ pytest-mpl] ( https://github.com/matplotlib/pytest-mpl ) plug-in to test plot
314
- generating code.
313
+ Writing an image-based test is only slightly more difficult than a simple test.
314
+ The main consideration is that you must specify the "baseline" or reference
315
+ image, and compare it with a "generated" or test image. This is handled using
316
+ the * decorator* functions ` @check_figures_equal ` and
317
+ ` @pytest.mark.mpl_image_compare ` whose usage are further described below.
318
+
319
+ #### Using check_figures_equal
320
+
321
+ This approach draws the same figure using two different methods (the reference
322
+ method and the tested method), and checks that both of them are the same.
323
+ It takes two ` pygmt.Figure ` objects ('fig_ref' and 'fig_test'), generates a png
324
+ image, and checks for the Root Mean Square (RMS) error between the two.
325
+ Here's an example:
326
+
327
+ ``` python
328
+ @check_figures_equal ()
329
+ def test_my_plotting_case (fig_ref , fig_test ):
330
+ " Test that my plotting function works"
331
+ fig_ref.grdimage(" @earth_relief_01d_g" , projection = " W120/15c" , cmap = " geo" )
332
+ fig_test.grdimage(grid, projection = " W120/15c" , cmap = " geo" )
333
+ ```
334
+
335
+ Note: This is the recommended way to test plots whenever possible, such as when
336
+ we want to compare a reference GMT plot created from NetCDF files with one
337
+ generated by PyGMT that passes through several layers of virtualfile machinery.
338
+ Using this method will help save space in the git repository by not having to
339
+ store baseline images as with the other method below.
340
+
341
+ #### Using mpl_image_compare
342
+
343
+ This method uses the [ pytest-mpl] ( https://github.com/matplotlib/pytest-mpl )
344
+ plug-in to test plot generating code.
315
345
Every time the tests are run, ` pytest-mpl ` compares the generated plots with known
316
346
correct ones stored in ` pygmt/tests/baseline ` .
317
347
If your test created a ` pygmt.Figure ` object, you can test it by adding a * decorator* and
0 commit comments