|
| 1 | +from __future__ import (absolute_import, division, print_function, |
| 2 | + unicode_literals) |
| 3 | + |
| 4 | +import inspect |
| 5 | +import os |
| 6 | +import pytest |
| 7 | +import unittest |
| 8 | + |
| 9 | +import matplotlib |
| 10 | +matplotlib.use('agg') |
| 11 | + |
| 12 | +from matplotlib import default_test_modules |
| 13 | +from matplotlib.testing.decorators import ImageComparisonTest |
| 14 | + |
| 15 | + |
| 16 | +IGNORED_TESTS = { |
| 17 | + 'matplotlib': [ |
| 18 | + 'test_usetex', |
| 19 | + ], |
| 20 | +} |
| 21 | + |
| 22 | + |
| 23 | +def blacklist_check(path): |
| 24 | + """Check if test is blacklisted and should be ignored""" |
| 25 | + head, tests_dir = os.path.split(path.dirname) |
| 26 | + if tests_dir != 'tests': |
| 27 | + return True |
| 28 | + head, top_module = os.path.split(head) |
| 29 | + return path.purebasename in IGNORED_TESTS.get(top_module, []) |
| 30 | + |
| 31 | + |
| 32 | +def whitelist_check(path): |
| 33 | + """Check if test is not whitelisted and should be ignored""" |
| 34 | + left = path.dirname |
| 35 | + last_left = None |
| 36 | + module_path = path.purebasename |
| 37 | + while len(left) and left != last_left: |
| 38 | + last_left = left |
| 39 | + left, tail = os.path.split(left) |
| 40 | + module_path = '.'.join([tail, module_path]) |
| 41 | + if module_path in default_test_modules: |
| 42 | + return False |
| 43 | + return True |
| 44 | + |
| 45 | + |
| 46 | +COLLECT_FILTERS = { |
| 47 | + 'none': lambda _: False, |
| 48 | + 'blacklist': blacklist_check, |
| 49 | + 'whitelist': whitelist_check, |
| 50 | +} |
| 51 | + |
| 52 | + |
| 53 | +def is_nose_class(cls): |
| 54 | + """Check if supplied class looks like Nose testcase""" |
| 55 | + return any(name in ['setUp', 'tearDown'] |
| 56 | + for name, _ in inspect.getmembers(cls)) |
| 57 | + |
| 58 | + |
| 59 | +def pytest_addoption(parser): |
| 60 | + group = parser.getgroup("matplotlib", "matplotlib custom options") |
| 61 | + |
| 62 | + group.addoption('--collect-filter', action='store', |
| 63 | + choices=COLLECT_FILTERS, default='blacklist', |
| 64 | + help='filter tests during collection phase') |
| 65 | + |
| 66 | + group.addoption('--no-pep8', action='store_true', |
| 67 | + help='skip PEP8 compliance tests') |
| 68 | + |
| 69 | + |
| 70 | +def pytest_configure(config): |
| 71 | + matplotlib._called_from_pytest = True |
| 72 | + matplotlib._init_tests() |
| 73 | + |
| 74 | + if config.getoption('--no-pep8'): |
| 75 | + default_test_modules.remove('matplotlib.tests.test_coding_standards') |
| 76 | + IGNORED_TESTS['matplotlib'] += 'test_coding_standards' |
| 77 | + |
| 78 | + |
| 79 | +def pytest_unconfigure(config): |
| 80 | + matplotlib._called_from_pytest = False |
| 81 | + |
| 82 | + |
| 83 | +def pytest_ignore_collect(path, config): |
| 84 | + if path.ext == '.py': |
| 85 | + collect_filter = config.getoption('--collect-filter') |
| 86 | + return COLLECT_FILTERS[collect_filter](path) |
| 87 | + |
| 88 | + |
| 89 | +def pytest_pycollect_makeitem(collector, name, obj): |
| 90 | + if inspect.isclass(obj): |
| 91 | + if issubclass(obj, ImageComparisonTest): |
| 92 | + # Workaround `image_compare` decorator as it returns class |
| 93 | + # instead of function and this confuses pytest because it crawls |
| 94 | + # original names and sees 'test_*', but not 'Test*' in that case |
| 95 | + return pytest.Class(name, parent=collector) |
| 96 | + |
| 97 | + if is_nose_class(obj) and not issubclass(obj, unittest.TestCase): |
| 98 | + # Workaround unittest-like setup/teardown names in pure classes |
| 99 | + setup = getattr(obj, 'setUp', None) |
| 100 | + if setup is not None: |
| 101 | + obj.setup_method = lambda self, _: obj.setUp(self) |
| 102 | + tearDown = getattr(obj, 'tearDown', None) |
| 103 | + if tearDown is not None: |
| 104 | + obj.teardown_method = lambda self, _: obj.tearDown(self) |
| 105 | + setUpClass = getattr(obj, 'setUpClass', None) |
| 106 | + if setUpClass is not None: |
| 107 | + obj.setup_class = obj.setUpClass |
| 108 | + tearDownClass = getattr(obj, 'tearDownClass', None) |
| 109 | + if tearDownClass is not None: |
| 110 | + obj.teardown_class = obj.tearDownClass |
| 111 | + |
| 112 | + return pytest.Class(name, parent=collector) |
0 commit comments