-
Notifications
You must be signed in to change notification settings - Fork 0
/
run_tests.py
executable file
·63 lines (51 loc) · 2.2 KB
/
run_tests.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
#!/usr/bin/env python
# Copyright 2015 The Switch Authors. All rights reserved.
# Licensed under the Apache License, Version 2, which is in the LICENSE file.
import doctest
import os
import sys
import unittest
class TestLoader(unittest.TestLoader):
# unittest.main does not allow multiple "--start-directory"
# options, but we can make it scan multiple separate directories
# by overriding discover(). This allows us to have a "tests"
# directory that's separate from "switch_mod".
#
# We don't want to scan for *.py files in the parent directory in
# case any of those are throwaway scripts that have unexpected
# effects when imported.
def discover(self, start_dir, pattern, top_level_dir):
test_suite = unittest.TestSuite()
for subdir in ('switch_mod', 'tests'):
test_suite.addTests(
super(TestLoader, self).discover(
os.path.join(top_level_dir, subdir),
pattern, top_level_dir))
return test_suite
# The unittest module does not have built-in support for finding
# doctests. In order to run the doctests, we need a custom
# TestLoader that overrides loadTestsFromModule().
def loadTestsFromModule(self, module):
test_suite = super(TestLoader, self).loadTestsFromModule(module)
docstring = module.__doc__
if not docstring:
# Work around a misfeature whereby doctest complains if a
# module contains no docstrings.
module.__doc__ = 'Placeholder docstring'
test_suite.addTests(doctest.DocTestSuite(module))
if not docstring:
# Restore the original, in case this matters.
module.__doc__ = docstring
return test_suite
def main():
script_dir = os.path.join(os.getcwd(), os.path.dirname(__file__))
# The doctests expect to be run from the "switch_mod" directory in
# order to find test_dat.
os.chdir(os.path.join(script_dir, 'switch_mod'))
argv = [sys.argv[0],
'discover',
'--top-level-dir', script_dir,
'--pattern', '*.py'] + sys.argv[1:]
unittest.TestProgram(testLoader=TestLoader(), argv=argv)
if __name__ == '__main__':
main()