Skip to content

Commit 7428893

Browse files
committed
Implemented test filtering for pytest
1 parent 9ae6eb3 commit 7428893

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

conftest.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,71 @@
22
unicode_literals)
33

44
import inspect
5+
import os
56
import pytest
67
import unittest
78

89
import matplotlib
910
matplotlib.use('agg')
1011

12+
from matplotlib import default_test_modules
1113
from matplotlib.testing.decorators import ImageComparisonTest
1214

1315

16+
IGNORED_TESTS = {
17+
'matplotlib': [
18+
'test_sankey',
19+
'test_skew',
20+
'test_ttconv',
21+
'test_usetex',
22+
],
23+
}
24+
25+
26+
def blacklist_check(path):
27+
"""Check if test is blacklisted and should be ignored"""
28+
head, tests_dir = os.path.split(path.dirname)
29+
if tests_dir != 'tests':
30+
return True
31+
head, top_module = os.path.split(head)
32+
return path.purebasename in IGNORED_TESTS.get(top_module, [])
33+
34+
35+
def whitelist_check(path):
36+
"""Check if test is not whitelisted and should be ignored"""
37+
left = path.dirname
38+
last_left = None
39+
module_path = path.purebasename
40+
while len(left) and left != last_left:
41+
last_left = left
42+
left, tail = os.path.split(left)
43+
module_path = '.'.join([tail, module_path])
44+
if module_path in default_test_modules:
45+
return False
46+
return True
47+
48+
49+
COLLECT_FILTERS = {
50+
'none': lambda _: False,
51+
'blacklist': blacklist_check,
52+
'whitelist': whitelist_check,
53+
}
54+
55+
1456
def is_nose_class(cls):
57+
"""Check if supplied class looks like Nose testcase"""
1558
return any(name in ['setUp', 'tearDown']
1659
for name, _ in inspect.getmembers(cls))
1760

1861

62+
def pytest_addoption(parser):
63+
group = parser.getgroup("matplotlib", "matplotlib custom options")
64+
65+
group.addoption('--collect-filter', action='store',
66+
choices=COLLECT_FILTERS, default='whitelist',
67+
help='filter tests during collection phase')
68+
69+
1970
def pytest_configure(config):
2071
matplotlib._called_from_pytest = True
2172

@@ -24,6 +75,12 @@ def pytest_unconfigure(config):
2475
matplotlib._called_from_pytest = False
2576

2677

78+
def pytest_ignore_collect(path, config):
79+
if path.ext == '.py':
80+
collect_filter = config.getoption('--collect-filter')
81+
return COLLECT_FILTERS[collect_filter](path)
82+
83+
2784
def pytest_pycollect_makeitem(collector, name, obj):
2885
if inspect.isclass(obj):
2986
if issubclass(obj, ImageComparisonTest):

pytest.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[pytest]
2+
norecursedirs = .git build ci dist extern release tools unit

0 commit comments

Comments
 (0)