1 | n/a | """Unittest main program""" |
---|
2 | n/a | |
---|
3 | n/a | import sys |
---|
4 | n/a | import argparse |
---|
5 | n/a | import os |
---|
6 | n/a | |
---|
7 | n/a | from . import loader, runner |
---|
8 | n/a | from .signals import installHandler |
---|
9 | n/a | |
---|
10 | n/a | __unittest = True |
---|
11 | n/a | |
---|
12 | n/a | MAIN_EXAMPLES = """\ |
---|
13 | n/a | Examples: |
---|
14 | n/a | %(prog)s test_module - run tests from test_module |
---|
15 | n/a | %(prog)s module.TestClass - run tests from module.TestClass |
---|
16 | n/a | %(prog)s module.Class.test_method - run specified test method |
---|
17 | n/a | """ |
---|
18 | n/a | |
---|
19 | n/a | MODULE_EXAMPLES = """\ |
---|
20 | n/a | Examples: |
---|
21 | n/a | %(prog)s - run default set of tests |
---|
22 | n/a | %(prog)s MyTestSuite - run suite 'MyTestSuite' |
---|
23 | n/a | %(prog)s MyTestCase.testSomething - run MyTestCase.testSomething |
---|
24 | n/a | %(prog)s MyTestCase - run all 'test*' test methods |
---|
25 | n/a | in MyTestCase |
---|
26 | n/a | """ |
---|
27 | n/a | |
---|
28 | n/a | def _convert_name(name): |
---|
29 | n/a | # on Linux / Mac OS X 'foo.PY' is not importable, but on |
---|
30 | n/a | # Windows it is. Simpler to do a case insensitive match |
---|
31 | n/a | # a better check would be to check that the name is a |
---|
32 | n/a | # valid Python module name. |
---|
33 | n/a | if os.path.isfile(name) and name.lower().endswith('.py'): |
---|
34 | n/a | if os.path.isabs(name): |
---|
35 | n/a | rel_path = os.path.relpath(name, os.getcwd()) |
---|
36 | n/a | if os.path.isabs(rel_path) or rel_path.startswith(os.pardir): |
---|
37 | n/a | return name |
---|
38 | n/a | name = rel_path |
---|
39 | n/a | # on Windows both '\' and '/' are used as path |
---|
40 | n/a | # separators. Better to replace both than rely on os.path.sep |
---|
41 | n/a | return name[:-3].replace('\\', '.').replace('/', '.') |
---|
42 | n/a | return name |
---|
43 | n/a | |
---|
44 | n/a | def _convert_names(names): |
---|
45 | n/a | return [_convert_name(name) for name in names] |
---|
46 | n/a | |
---|
47 | n/a | |
---|
48 | n/a | class TestProgram(object): |
---|
49 | n/a | """A command-line program that runs a set of tests; this is primarily |
---|
50 | n/a | for making test modules conveniently executable. |
---|
51 | n/a | """ |
---|
52 | n/a | # defaults for testing |
---|
53 | n/a | module=None |
---|
54 | n/a | verbosity = 1 |
---|
55 | n/a | failfast = catchbreak = buffer = progName = warnings = None |
---|
56 | n/a | _discovery_parser = None |
---|
57 | n/a | |
---|
58 | n/a | def __init__(self, module='__main__', defaultTest=None, argv=None, |
---|
59 | n/a | testRunner=None, testLoader=loader.defaultTestLoader, |
---|
60 | n/a | exit=True, verbosity=1, failfast=None, catchbreak=None, |
---|
61 | n/a | buffer=None, warnings=None, *, tb_locals=False): |
---|
62 | n/a | if isinstance(module, str): |
---|
63 | n/a | self.module = __import__(module) |
---|
64 | n/a | for part in module.split('.')[1:]: |
---|
65 | n/a | self.module = getattr(self.module, part) |
---|
66 | n/a | else: |
---|
67 | n/a | self.module = module |
---|
68 | n/a | if argv is None: |
---|
69 | n/a | argv = sys.argv |
---|
70 | n/a | |
---|
71 | n/a | self.exit = exit |
---|
72 | n/a | self.failfast = failfast |
---|
73 | n/a | self.catchbreak = catchbreak |
---|
74 | n/a | self.verbosity = verbosity |
---|
75 | n/a | self.buffer = buffer |
---|
76 | n/a | self.tb_locals = tb_locals |
---|
77 | n/a | if warnings is None and not sys.warnoptions: |
---|
78 | n/a | # even if DeprecationWarnings are ignored by default |
---|
79 | n/a | # print them anyway unless other warnings settings are |
---|
80 | n/a | # specified by the warnings arg or the -W python flag |
---|
81 | n/a | self.warnings = 'default' |
---|
82 | n/a | else: |
---|
83 | n/a | # here self.warnings is set either to the value passed |
---|
84 | n/a | # to the warnings args or to None. |
---|
85 | n/a | # If the user didn't pass a value self.warnings will |
---|
86 | n/a | # be None. This means that the behavior is unchanged |
---|
87 | n/a | # and depends on the values passed to -W. |
---|
88 | n/a | self.warnings = warnings |
---|
89 | n/a | self.defaultTest = defaultTest |
---|
90 | n/a | self.testRunner = testRunner |
---|
91 | n/a | self.testLoader = testLoader |
---|
92 | n/a | self.progName = os.path.basename(argv[0]) |
---|
93 | n/a | self.parseArgs(argv) |
---|
94 | n/a | self.runTests() |
---|
95 | n/a | |
---|
96 | n/a | def usageExit(self, msg=None): |
---|
97 | n/a | if msg: |
---|
98 | n/a | print(msg) |
---|
99 | n/a | if self._discovery_parser is None: |
---|
100 | n/a | self._initArgParsers() |
---|
101 | n/a | self._print_help() |
---|
102 | n/a | sys.exit(2) |
---|
103 | n/a | |
---|
104 | n/a | def _print_help(self, *args, **kwargs): |
---|
105 | n/a | if self.module is None: |
---|
106 | n/a | print(self._main_parser.format_help()) |
---|
107 | n/a | print(MAIN_EXAMPLES % {'prog': self.progName}) |
---|
108 | n/a | self._discovery_parser.print_help() |
---|
109 | n/a | else: |
---|
110 | n/a | print(self._main_parser.format_help()) |
---|
111 | n/a | print(MODULE_EXAMPLES % {'prog': self.progName}) |
---|
112 | n/a | |
---|
113 | n/a | def parseArgs(self, argv): |
---|
114 | n/a | self._initArgParsers() |
---|
115 | n/a | if self.module is None: |
---|
116 | n/a | if len(argv) > 1 and argv[1].lower() == 'discover': |
---|
117 | n/a | self._do_discovery(argv[2:]) |
---|
118 | n/a | return |
---|
119 | n/a | self._main_parser.parse_args(argv[1:], self) |
---|
120 | n/a | if not self.tests: |
---|
121 | n/a | # this allows "python -m unittest -v" to still work for |
---|
122 | n/a | # test discovery. |
---|
123 | n/a | self._do_discovery([]) |
---|
124 | n/a | return |
---|
125 | n/a | else: |
---|
126 | n/a | self._main_parser.parse_args(argv[1:], self) |
---|
127 | n/a | |
---|
128 | n/a | if self.tests: |
---|
129 | n/a | self.testNames = _convert_names(self.tests) |
---|
130 | n/a | if __name__ == '__main__': |
---|
131 | n/a | # to support python -m unittest ... |
---|
132 | n/a | self.module = None |
---|
133 | n/a | elif self.defaultTest is None: |
---|
134 | n/a | # createTests will load tests from self.module |
---|
135 | n/a | self.testNames = None |
---|
136 | n/a | elif isinstance(self.defaultTest, str): |
---|
137 | n/a | self.testNames = (self.defaultTest,) |
---|
138 | n/a | else: |
---|
139 | n/a | self.testNames = list(self.defaultTest) |
---|
140 | n/a | self.createTests() |
---|
141 | n/a | |
---|
142 | n/a | def createTests(self): |
---|
143 | n/a | if self.testNames is None: |
---|
144 | n/a | self.test = self.testLoader.loadTestsFromModule(self.module) |
---|
145 | n/a | else: |
---|
146 | n/a | self.test = self.testLoader.loadTestsFromNames(self.testNames, |
---|
147 | n/a | self.module) |
---|
148 | n/a | |
---|
149 | n/a | def _initArgParsers(self): |
---|
150 | n/a | parent_parser = self._getParentArgParser() |
---|
151 | n/a | self._main_parser = self._getMainArgParser(parent_parser) |
---|
152 | n/a | self._discovery_parser = self._getDiscoveryArgParser(parent_parser) |
---|
153 | n/a | |
---|
154 | n/a | def _getParentArgParser(self): |
---|
155 | n/a | parser = argparse.ArgumentParser(add_help=False) |
---|
156 | n/a | |
---|
157 | n/a | parser.add_argument('-v', '--verbose', dest='verbosity', |
---|
158 | n/a | action='store_const', const=2, |
---|
159 | n/a | help='Verbose output') |
---|
160 | n/a | parser.add_argument('-q', '--quiet', dest='verbosity', |
---|
161 | n/a | action='store_const', const=0, |
---|
162 | n/a | help='Quiet output') |
---|
163 | n/a | parser.add_argument('--locals', dest='tb_locals', |
---|
164 | n/a | action='store_true', |
---|
165 | n/a | help='Show local variables in tracebacks') |
---|
166 | n/a | if self.failfast is None: |
---|
167 | n/a | parser.add_argument('-f', '--failfast', dest='failfast', |
---|
168 | n/a | action='store_true', |
---|
169 | n/a | help='Stop on first fail or error') |
---|
170 | n/a | self.failfast = False |
---|
171 | n/a | if self.catchbreak is None: |
---|
172 | n/a | parser.add_argument('-c', '--catch', dest='catchbreak', |
---|
173 | n/a | action='store_true', |
---|
174 | n/a | help='Catch Ctrl-C and display results so far') |
---|
175 | n/a | self.catchbreak = False |
---|
176 | n/a | if self.buffer is None: |
---|
177 | n/a | parser.add_argument('-b', '--buffer', dest='buffer', |
---|
178 | n/a | action='store_true', |
---|
179 | n/a | help='Buffer stdout and stderr during tests') |
---|
180 | n/a | self.buffer = False |
---|
181 | n/a | |
---|
182 | n/a | return parser |
---|
183 | n/a | |
---|
184 | n/a | def _getMainArgParser(self, parent): |
---|
185 | n/a | parser = argparse.ArgumentParser(parents=[parent]) |
---|
186 | n/a | parser.prog = self.progName |
---|
187 | n/a | parser.print_help = self._print_help |
---|
188 | n/a | |
---|
189 | n/a | parser.add_argument('tests', nargs='*', |
---|
190 | n/a | help='a list of any number of test modules, ' |
---|
191 | n/a | 'classes and test methods.') |
---|
192 | n/a | |
---|
193 | n/a | return parser |
---|
194 | n/a | |
---|
195 | n/a | def _getDiscoveryArgParser(self, parent): |
---|
196 | n/a | parser = argparse.ArgumentParser(parents=[parent]) |
---|
197 | n/a | parser.prog = '%s discover' % self.progName |
---|
198 | n/a | parser.epilog = ('For test discovery all test modules must be ' |
---|
199 | n/a | 'importable from the top level directory of the ' |
---|
200 | n/a | 'project.') |
---|
201 | n/a | |
---|
202 | n/a | parser.add_argument('-s', '--start-directory', dest='start', |
---|
203 | n/a | help="Directory to start discovery ('.' default)") |
---|
204 | n/a | parser.add_argument('-p', '--pattern', dest='pattern', |
---|
205 | n/a | help="Pattern to match tests ('test*.py' default)") |
---|
206 | n/a | parser.add_argument('-t', '--top-level-directory', dest='top', |
---|
207 | n/a | help='Top level directory of project (defaults to ' |
---|
208 | n/a | 'start directory)') |
---|
209 | n/a | for arg in ('start', 'pattern', 'top'): |
---|
210 | n/a | parser.add_argument(arg, nargs='?', |
---|
211 | n/a | default=argparse.SUPPRESS, |
---|
212 | n/a | help=argparse.SUPPRESS) |
---|
213 | n/a | |
---|
214 | n/a | return parser |
---|
215 | n/a | |
---|
216 | n/a | def _do_discovery(self, argv, Loader=None): |
---|
217 | n/a | self.start = '.' |
---|
218 | n/a | self.pattern = 'test*.py' |
---|
219 | n/a | self.top = None |
---|
220 | n/a | if argv is not None: |
---|
221 | n/a | # handle command line args for test discovery |
---|
222 | n/a | if self._discovery_parser is None: |
---|
223 | n/a | # for testing |
---|
224 | n/a | self._initArgParsers() |
---|
225 | n/a | self._discovery_parser.parse_args(argv, self) |
---|
226 | n/a | |
---|
227 | n/a | loader = self.testLoader if Loader is None else Loader() |
---|
228 | n/a | self.test = loader.discover(self.start, self.pattern, self.top) |
---|
229 | n/a | |
---|
230 | n/a | def runTests(self): |
---|
231 | n/a | if self.catchbreak: |
---|
232 | n/a | installHandler() |
---|
233 | n/a | if self.testRunner is None: |
---|
234 | n/a | self.testRunner = runner.TextTestRunner |
---|
235 | n/a | if isinstance(self.testRunner, type): |
---|
236 | n/a | try: |
---|
237 | n/a | try: |
---|
238 | n/a | testRunner = self.testRunner(verbosity=self.verbosity, |
---|
239 | n/a | failfast=self.failfast, |
---|
240 | n/a | buffer=self.buffer, |
---|
241 | n/a | warnings=self.warnings, |
---|
242 | n/a | tb_locals=self.tb_locals) |
---|
243 | n/a | except TypeError: |
---|
244 | n/a | # didn't accept the tb_locals argument |
---|
245 | n/a | testRunner = self.testRunner(verbosity=self.verbosity, |
---|
246 | n/a | failfast=self.failfast, |
---|
247 | n/a | buffer=self.buffer, |
---|
248 | n/a | warnings=self.warnings) |
---|
249 | n/a | except TypeError: |
---|
250 | n/a | # didn't accept the verbosity, buffer or failfast arguments |
---|
251 | n/a | testRunner = self.testRunner() |
---|
252 | n/a | else: |
---|
253 | n/a | # it is assumed to be a TestRunner instance |
---|
254 | n/a | testRunner = self.testRunner |
---|
255 | n/a | self.result = testRunner.run(self.test) |
---|
256 | n/a | if self.exit: |
---|
257 | n/a | sys.exit(not self.result.wasSuccessful()) |
---|
258 | n/a | |
---|
259 | n/a | main = TestProgram |
---|