1 | """ |
---|
2 | Provide support for test skipping. |
---|
3 | |
---|
4 | Based on work by Titus Brown in pygr |
---|
5 | """ |
---|
6 | |
---|
7 | import unittest, time |
---|
8 | import pathfix |
---|
9 | from genetrack import logger |
---|
10 | |
---|
11 | class SkipTest(Exception): |
---|
12 | pass |
---|
13 | |
---|
14 | class TestResult(unittest._TextTestResult): |
---|
15 | |
---|
16 | def __init__(self, *args, **kwargs): |
---|
17 | unittest._TextTestResult.__init__(self, *args, **kwargs) |
---|
18 | self.skipped = [] |
---|
19 | |
---|
20 | def addError(self, test, err): |
---|
21 | exc_type, val, _ = err |
---|
22 | if issubclass(exc_type, SkipTest): |
---|
23 | self.skipped.append((self, val)) |
---|
24 | if self.showAll: |
---|
25 | # report skips: SKIP/S |
---|
26 | self.stream.writeln("SKIP") |
---|
27 | elif self.dots: |
---|
28 | self.stream.write('S') |
---|
29 | else: |
---|
30 | unittest._TextTestResult.addError(self, test, err) |
---|
31 | |
---|
32 | class TestRunner(unittest.TextTestRunner): |
---|
33 | """ |
---|
34 | Support running tests that understand SkipTest. |
---|
35 | """ |
---|
36 | def _makeResult(self): |
---|
37 | return TestResult(self.stream, self.descriptions, |
---|
38 | self.verbosity) |
---|
39 | |
---|
40 | def run(self, test): |
---|
41 | "Run the given test case or test suite." |
---|
42 | result = self._makeResult() |
---|
43 | startTime = time.time() |
---|
44 | test(result) |
---|
45 | stopTime = time.time() |
---|
46 | timeTaken = stopTime - startTime |
---|
47 | result.printErrors() |
---|
48 | #self.stream.writeln(result.separator2) |
---|
49 | run = result.testsRun |
---|
50 | |
---|
51 | if self.verbosity>0: |
---|
52 | self.stream.writeln("Ran %d test%s in %.3fs" % |
---|
53 | (run, run != 1 and "s" or "", timeTaken)) |
---|
54 | self.stream.writeln() |
---|
55 | |
---|
56 | if not result.wasSuccessful(): |
---|
57 | self.stream.write("FAILED (") |
---|
58 | failed, errored = map(len, (result.failures, result.errors)) |
---|
59 | if failed: |
---|
60 | self.stream.write("failures=%d" % failed) |
---|
61 | if errored: |
---|
62 | if failed: self.stream.write(", ") |
---|
63 | self.stream.write("errors=%d" % errored) |
---|
64 | self.stream.writeln(")") |
---|
65 | else: |
---|
66 | #self.stream.writeln("OK") |
---|
67 | pass |
---|
68 | return result |
---|
69 | |
---|
70 | class TestProgram(unittest.TestProgram): |
---|
71 | |
---|
72 | def __init__(self, **kwargs): |
---|
73 | verbosity = kwargs.pop('verbosity', 1) |
---|
74 | if verbosity != 2: |
---|
75 | logger.disable('DEBUG') |
---|
76 | if kwargs.get('testRunner') is None: |
---|
77 | kwargs['testRunner'] = TestRunner(verbosity=verbosity) |
---|
78 | |
---|
79 | unittest.TestProgram.__init__(self, **kwargs) |
---|