Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 6c94b47

Browse files
committed
Implemented test filtering for pytest
1 parent 922d3ac commit 6c94b47

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

conftest.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,68 @@
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_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+
1453
def is_nose_class(cls):
54+
"""Check if supplied class looks like Nose testcase"""
1555
return any(name in ['setUp', 'tearDown']
1656
for name, _ in inspect.getmembers(cls))
1757

1858

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+
1967
def pytest_configure(config):
2068
matplotlib._called_from_pytest = True
2169

@@ -24,6 +72,12 @@ def pytest_unconfigure(config):
2472
matplotlib._called_from_pytest = False
2573

2674

75+
def pytest_ignore_collect(path, config):
76+
if path.ext == '.py':
77+
collect_filter = config.getoption('--collect-filter')
78+
return COLLECT_FILTERS[collect_filter](path)
79+
80+
2781
def pytest_pycollect_makeitem(collector, name, obj):
2882
if inspect.isclass(obj):
2983
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)