forked from EmilStenstrom/conllu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
runtests.py
101 lines (76 loc) · 3.1 KB
/
runtests.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#!/usr/bin/env python
# Reference: https://gist.github.com/fragmuffin/a245f59bdcd457936c3b51aa2ebb3f6c
import functools
import re
import unittest
class MyTestRunner(unittest.runner.TextTestRunner):
def __init__(self, *args, **kwargs):
"""
Append blacklist & whitelist attributes to TestRunner instance
"""
self.whitelist = set(kwargs.pop('whitelist', []))
self.blacklist = set(kwargs.pop('blacklist', []))
super(MyTestRunner, self).__init__(*args, **kwargs)
@classmethod
def test_iter(cls, suite):
"""
Iterate through test suites, and yield individual tests
"""
for test in suite:
if isinstance(test, unittest.TestSuite):
for t in cls.test_iter(test):
yield t
else:
yield test
def run(self, testlist):
# Change given testlist into a TestSuite
suite = unittest.TestSuite()
# Add each test in testlist, apply skip mechanism if necessary
for test in self.test_iter(testlist):
# Determine if test should be skipped
skip = bool(self.whitelist)
test_labels = getattr(test, '_labels', set())
if test_labels & self.whitelist:
skip = False
if test_labels & self.blacklist:
skip = True
if skip:
# Test should be skipped.
# replace original method with function "skip"
test_method = getattr(test, test._testMethodName)
# Create a "skip test" wrapper for the actual test method
@functools.wraps(test_method)
def skip_wrapper(*args, **kwargs):
raise unittest.SkipTest('label exclusion')
skip_wrapper.__unittest_skip__ = True
skip_wrapper.__unittest_skip_why__ = ", ".join(test_labels)
setattr(test, test._testMethodName, skip_wrapper)
suite.addTest(test)
# Resume normal TextTestRunner function with the new test suite
return super(MyTestRunner, self).run(suite)
if __name__ == '__main__':
import argparse
import sys
# ---- create commandline parser
parser = argparse.ArgumentParser(description='Find and run cqparts tests.')
def label_list(value):
return re.split(r'\W+', value)
parser.add_argument('-w', '--whitelist', dest='whitelist',
type=label_list, default=[],
help="list of labels to test (skip all others)")
parser.add_argument('-b', '--blacklist', dest='blacklist',
type=label_list, default=[],
help="list of labels to skip")
args = parser.parse_args()
# ---- Discover and run tests
# Discover Tests
loader = unittest.TestLoader()
tests = loader.discover('.', pattern='test_*.py')
# Run tests
testRunner = MyTestRunner(
blacklist=args.blacklist,
whitelist=args.whitelist,
verbosity=2,
)
result = testRunner.run(tests)
sys.exit(not result.wasSuccessful())