-
Notifications
You must be signed in to change notification settings - Fork 14
/
test.py
executable file
·66 lines (51 loc) · 2.08 KB
/
test.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
#!/usr/bin/env python
# Python Standard Library
from __future__ import print_function
import doctest
import sys
# Third-Party Libraries
import yaml
# Doctest Helper
# ------------------------------------------------------------------------------
# The issue: b'A' displays as b'A' in Python 3 but as 'A' in Python 2.
# The solution: write your doctests with the 'b' prefix
# -- that is, target Python 3 -- but use the doctest directive BYTES
# that we introduce in this section to automatically tweak them for Python 2.
# declare 'BYTES'
doctest.BYTES = doctest.register_optionflag("BYTES")
doctest.__all__.append("BYTES")
doctest.COMPARISON_FLAGS = doctest.COMPARISON_FLAGS | doctest.BYTES
_doctest_OutputChecker = doctest.OutputChecker
class BytesOutputChecker(_doctest_OutputChecker):
def check_output(self, want, got, optionflags):
super_check_output = _doctest_OutputChecker.check_output
if (optionflags & doctest.BYTES) and sys.version_info[0] == 2:
want = want[1:] # strip the 'b' prefix from the expected result
return super_check_output(self, want, got, optionflags)
# monkey-patching
doctest.OutputChecker = BytesOutputChecker
# Test Files
# ------------------------------------------------------------------------------
mkdocs_pages = yaml.load(open("mkdocs.yml"), Loader=yaml.FullLoader)["pages"]
mkdocs_files = ["mkdocs/" + list(item.values())[0] for item in mkdocs_pages]
extra_testfiles = []
test_files = mkdocs_files + extra_testfiles
# Run the Tests
# ------------------------------------------------------------------------------
verbose = "-v" in sys.argv or "--verbose" in sys.argv
fails = 0
tests = 0
for filename in test_files:
# TODO enable verbose mode in each testfile call if appropriate
options = {"module_relative": False, "verbose": verbose}
_fails, _tests = doctest.testfile(filename, **options)
fails += _fails
tests += _tests
if fails > 0 or verbose:
print()
print(60*"-")
print("Test Suite Report:", end="")
print("{0} failures / {1} tests".format(fails, tests))
print(60*"-")
if fails:
sys.exit(1)