Skip to content

Commit 30f7b65

Browse files
committed
update testing helpers
- `@matplotlib.testing.decorators.image_comparison` now prints the path to the diff image so it is easier to find, so that newcomers know it exists. - update `tests.py` to give more information about how it should be run if matplotlib is not installed already. - update `matplotlib.test` to return -1 instead of raising ImportErrors if there are preconditions not met. This allows the error messages to not be buried by the stack traces, is still a return value which would signify an error, and does not conflict with the range used by `pytest.ExitCode` (0-5) allowing callers to distinguish different pytest error codes from an error starting up pytest - move the `import pytest` check out of `matplotlib._init_tests` because it is only called via `matplotlib.test` and via pytest's configure handler, and it is more appropriate and can be handled more gracefully if it is checked in `matplotlib.test`
1 parent bb75f73 commit 30f7b65

File tree

3 files changed

+18
-13
lines changed

3 files changed

+18
-13
lines changed

lib/matplotlib/__init__.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -1168,29 +1168,29 @@ def _init_tests():
11681168
"Freetype build type is {}local".format(
11691169
"" if ft2font.__freetype_build_type__ == 'local' else "not "))
11701170

1171-
try:
1172-
import pytest
1173-
except ImportError:
1174-
print("matplotlib.test requires pytest to run.")
1175-
raise
1176-
11771171

11781172
@cbook._delete_parameter("3.2", "switch_backend_warn")
11791173
@cbook._delete_parameter("3.3", "recursionlimit")
11801174
def test(verbosity=None, coverage=False, switch_backend_warn=True,
11811175
recursionlimit=0, **kwargs):
11821176
"""Run the matplotlib test suite."""
1183-
_init_tests()
1177+
1178+
try:
1179+
import pytest
1180+
except ImportError:
1181+
print("matplotlib.test requires pytest to run.")
1182+
return -1
1183+
11841184
if not os.path.isdir(os.path.join(os.path.dirname(__file__), 'tests')):
1185-
raise ImportError("Matplotlib test data is not installed")
1185+
print("Matplotlib test data is not installed")
1186+
return -1
11861187

11871188
old_backend = get_backend()
11881189
old_recursionlimit = sys.getrecursionlimit()
11891190
try:
11901191
use('agg')
11911192
if recursionlimit:
11921193
sys.setrecursionlimit(recursionlimit)
1193-
import pytest
11941194

11951195
args = kwargs.pop('argv', [])
11961196
provide_default_modules = True

lib/matplotlib/testing/decorators.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,11 @@ def _raise_on_image_difference(expected, actual, tol):
130130

131131
err = compare_images(expected, actual, tol, in_decorator=True)
132132
if err:
133-
for key in ["actual", "expected"]:
133+
for key in ["actual", "expected", "diff"]:
134134
err[key] = os.path.relpath(err[key])
135135
raise ImageComparisonFailure(
136-
'images not close (RMS %(rms).3f):\n\t%(actual)s\n\t%(expected)s '
137-
% err)
136+
('images not close (RMS %(rms).3f):'
137+
'\n\t%(actual)s\n\t%(expected)s\n\t%(diff)s') % err)
138138

139139

140140
def _skip_if_format_is_uncomparable(extension):

tests.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@
1313

1414

1515
if __name__ == '__main__':
16-
from matplotlib import test
16+
try:
17+
from matplotlib import test
18+
except ImportError:
19+
print('matplotlib.test could not be imported.\n\n'
20+
'Try a virtual env and `pip install -e .`')
21+
sys.exit(-1)
1722

1823
parser = argparse.ArgumentParser(add_help=False)
1924
parser.add_argument('--recursionlimit', type=int, default=None,

0 commit comments

Comments
 (0)