From e6b6e458c424bd41d357d209b1d75fd3ca8b6602 Mon Sep 17 00:00:00 2001 From: Kelsey Jordahl Date: Mon, 28 Oct 2013 22:54:52 -0400 Subject: [PATCH] TST: Use compare_images rather than decorator and switch back to unittest.TestCase --- tests/test_plotting.py | 44 +++++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/tests/test_plotting.py b/tests/test_plotting.py index 52d5587..e3598b9 100644 --- a/tests/test_plotting.py +++ b/tests/test_plotting.py @@ -1,8 +1,10 @@ import os +import tempfile import unittest from matplotlib.pyplot import Artist, savefig -from matplotlib.testing.decorators import image_comparison +from matplotlib.testing.noseclasses import ImageComparisonFailure +from matplotlib.testing.compare import compare_images from shapely.geometry import Polygon, LineString, Point from geopandas import GeoSeries @@ -10,23 +12,33 @@ from geopandas import GeoSeries # If set to True, generate images rather than perform tests (all tests will pass!) GENERATE_BASELINE = False +TEMPDIR = tempfile.gettempdir() BASELINE_DIR = os.path.join(os.path.dirname(__file__), 'baseline_images', 'test_plotting') -def save_baseline_image(filename): - """ save a baseline image """ - savefig(os.path.join(BASELINE_DIR, filename)) -@image_comparison(baseline_images=['poly_plot'], extensions=['png']) -def test_poly_plot(): - """ Test plotting a simple series of polygons """ - t1 = Polygon([(0, 0), (1, 0), (1, 1)]) - t2 = Polygon([(1, 0), (2, 1), (2, 1)]) - polys = GeoSeries([t1, t2]) - ax = polys.plot() - assert isinstance(ax, Artist) - if GENERATE_BASELINE: - save_baseline_image('poly_plot.png') +class PlotTests(unittest.TestCase): + + def test_poly_plot(self, tol=8): + """ Test plotting a simple series of polygons """ + filename = 'poly_plot.png' + t1 = Polygon([(0, 0), (1, 0), (1, 1)]) + t2 = Polygon([(1, 0), (2, 0), (2, 1)]) + polys = GeoSeries([t1, t2]) + ax = polys.plot() + assert isinstance(ax, Artist) + if GENERATE_BASELINE: + savefig(os.path.join(BASELINE_DIR, filename)) + savefig(os.path.join(TEMPDIR, filename)) + err = compare_images(os.path.join(BASELINE_DIR, filename), + os.path.join(TEMPDIR, filename), + tol, in_decorator=True) + try: + if err: + raise ImageComparisonFailure('images not close: %(actual)s ' + 'vs. %(expected)s ' + '(RMS %(rms).3f)' % err) + finally: + os.remove(os.path.join(TEMPDIR, filename)) if __name__ == '__main__': - import nose - nose.runmodule(argv=['-s', '--with-doctest']) + unittest.main()