-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathtestdocs.py
45 lines (36 loc) · 1.32 KB
/
testdocs.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
import doctest
import sys
def test(**kwargs):
doctest.NORMALIZE_WHITESPACE = 1
verbosity = kwargs.get('verbose', 0)
if verbosity == 0:
print('Running doctests...')
# ignore py2-3 unicode differences
import re
class Py23DocChecker(doctest.OutputChecker):
def check_output(self, want, got, optionflags):
if sys.version_info[0] == 2:
got = re.sub("u'(.*?)'", "'\\1'", got)
got = re.sub('u"(.*?)"', '"\\1"', got)
res = doctest.OutputChecker.check_output(self, want, got, optionflags)
return res
def summarize(self):
doctest.OutputChecker.summarize(True)
# run tests
runner = doctest.DocTestRunner(checker=Py23DocChecker(), verbose=verbosity)
with open('README.md') as r:
doc = r.read()
test = doctest.DocTestParser().get_doctest(string=doc, globs={}, name="__init__", filename="__init__.py", lineno=0)
failure_count, test_count = runner.run(test)
# print results
if verbosity:
runner.summarize(True)
else:
if failure_count == 0:
print('All test passed successfully')
elif failure_count > 0:
runner.summarize(verbosity)
return failure_count
if __name__ == '__main__':
failure_count = test()
sys.exit(failure_count)