Skip to content

Commit bbaf7dc

Browse files
committed
Pytest compatible knownfailif and image_comparison decorators
1 parent 9b47575 commit bbaf7dc

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

conftest.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from __future__ import (absolute_import, division, print_function,
2+
unicode_literals)
3+
4+
import inspect
5+
import pytest
6+
7+
import matplotlib
8+
matplotlib.use('agg')
9+
10+
from matplotlib.testing.decorators import ImageComparisonTest
11+
12+
13+
def pytest_configure(config):
14+
matplotlib._called_from_pytest = True
15+
16+
17+
def pytest_unconfigure(config):
18+
matplotlib._called_from_pytest = False
19+
20+
21+
def pytest_pycollect_makeitem(collector, name, obj):
22+
if inspect.isclass(obj):
23+
if issubclass(obj, ImageComparisonTest):
24+
# Workaround `image_compare` decorator as it returns class
25+
# instead of function and this confuses pytest because it crawls
26+
# original names and sees 'test_*', but not 'Test*' in that case
27+
return pytest.Class(name, parent=collector)

lib/matplotlib/testing/decorators.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,15 @@
3030
make_test_filename
3131

3232

33+
def knownfail(msg):
34+
# TODO: This is temporary solution until migration to `pytest`
35+
if getattr(matplotlib, '_called_from_pytest', False):
36+
import pytest
37+
pytest.xfail(msg)
38+
else:
39+
raise KnownFailureTest(msg)
40+
41+
3342
def knownfailureif(fail_condition, msg=None, known_exception_class=None ):
3443
"""
3544
@@ -42,6 +51,13 @@ def knownfailureif(fail_condition, msg=None, known_exception_class=None ):
4251
if the exception is an instance of this class. (Default = None)
4352
4453
"""
54+
# TODO: This is temporary solution until migration to `pytest`
55+
if getattr(matplotlib, '_called_from_pytest', False):
56+
import pytest
57+
strict = fail_condition and fail_condition != 'indeterminate'
58+
return pytest.mark.xfail(condition=fail_condition, reason=msg,
59+
raises=known_exception_class, strict=strict)
60+
4561
# based on numpy.testing.dec.knownfailureif
4662
if msg is None:
4763
msg = 'Test known to fail'
@@ -264,7 +280,7 @@ def do_test(fignum, actual_fname, expected_fname):
264280
'(RMS %(rms).3f)'%err)
265281
except ImageComparisonFailure:
266282
if not check_freetype_version(self._freetype_version):
267-
raise KnownFailureTest(
283+
knownfail(
268284
"Mismatched version of freetype. Test requires '%s', you have '%s'" %
269285
(self._freetype_version, ft2font.__freetype_version__))
270286
raise

0 commit comments

Comments
 (0)