Make more of testing/ pytest-independent. · matplotlib/matplotlib@b0d5f41 · GitHub
Skip to content

Commit b0d5f41

Browse files
committed
Make more of testing/ pytest-independent.
This makes it actually possible to write tests independently of pytest (the API is still private, but could be made public): ``` import unittest from matplotlib import pyplot as plt from matplotlib.testing.decorators import _make_image_comparator class TestFoo(unittest.TestCase): @_make_image_comparator(baseline_images=["foo"], extension="png") def test_bar(self): fig, ax = plt.subplots() ``` works as expected.
1 parent 09bd5dd commit b0d5f41

3 files changed

Lines changed: 66 additions & 73 deletions

File tree

Lines changed: 6 additions & 0 deletions

lib/matplotlib/testing/compare.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import subprocess
1313
import sys
1414
from tempfile import TemporaryFile
15+
import unittest
1516

1617
import numpy as np
1718

@@ -255,6 +256,12 @@ def comparable_formats():
255256
return ['png', *converter]
256257

257258

259+
def _skip_if_incomparable(ext):
260+
"""Raise `unittest.SkipTest` if an *ext* files cannot be compared."""
261+
if ext not in comparable_formats():
262+
raise unittest.SkipTest(f"Lacking dependency to compare {ext} files")
263+
264+
258265
def convert(filename, cache):
259266
"""
260267
Convert the named file to png; return the name of the created file.
@@ -265,9 +272,7 @@ def convert(filename, cache):
265272
size of the cache, so it may need to be manually cleared periodically.
266273
"""
267274
base, extension = os.fspath(filename).rsplit('.', 1)
268-
if extension not in converter:
269-
import pytest
270-
pytest.skip(f"Don't know how to convert {extension} files to png")
275+
_skip_if_incomparable(extension)
271276
newname = base + '_' + extension + '.png'
272277
if not os.path.exists(filename):
273278
raise IOError("'%s' does not exist" % filename)

lib/matplotlib/testing/decorators.py

Lines changed: 52 additions & 70 deletions

0 commit comments

Comments
 (0)