Show More
@@ -1,96 +1,97 b'' | |||
|
1 | 1 | """Implementation of the parametric test support for Python 2.x |
|
2 | 2 | """ |
|
3 | 3 | |
|
4 | 4 | #----------------------------------------------------------------------------- |
|
5 | 5 | # Copyright (C) 2009-2011 The IPython Development Team |
|
6 | 6 | # |
|
7 | 7 | # Distributed under the terms of the BSD License. The full license is in |
|
8 | 8 | # the file COPYING, distributed as part of this software. |
|
9 | 9 | #----------------------------------------------------------------------------- |
|
10 | 10 | |
|
11 | 11 | #----------------------------------------------------------------------------- |
|
12 | 12 | # Imports |
|
13 | 13 | #----------------------------------------------------------------------------- |
|
14 | 14 | |
|
15 | import sys | |
|
15 | 16 | import unittest |
|
16 | 17 | from compiler.consts import CO_GENERATOR |
|
17 | 18 | |
|
18 | 19 | #----------------------------------------------------------------------------- |
|
19 | 20 | # Classes and functions |
|
20 | 21 | #----------------------------------------------------------------------------- |
|
21 | 22 | |
|
22 | 23 | def isgenerator(func): |
|
23 | 24 | try: |
|
24 | 25 | return func.func_code.co_flags & CO_GENERATOR != 0 |
|
25 | 26 | except AttributeError: |
|
26 | 27 | return False |
|
27 | 28 | |
|
28 | 29 | class ParametricTestCase(unittest.TestCase): |
|
29 | 30 | """Write parametric tests in normal unittest testcase form. |
|
30 | 31 | |
|
31 | 32 | Limitations: the last iteration misses printing out a newline when running |
|
32 | 33 | in verbose mode. |
|
33 | 34 | """ |
|
34 | 35 | def run_parametric(self, result, testMethod): |
|
35 | 36 | # But if we have a test generator, we iterate it ourselves |
|
36 | 37 | testgen = testMethod() |
|
37 | 38 | while True: |
|
38 | 39 | try: |
|
39 | 40 | # Initialize test |
|
40 | 41 | result.startTest(self) |
|
41 | 42 | |
|
42 | 43 | # SetUp |
|
43 | 44 | try: |
|
44 | 45 | self.setUp() |
|
45 | 46 | except KeyboardInterrupt: |
|
46 | 47 | raise |
|
47 | 48 | except: |
|
48 |
result.addError(self, s |
|
|
49 | result.addError(self, sys.exc_info()) | |
|
49 | 50 | return |
|
50 | 51 | # Test execution |
|
51 | 52 | ok = False |
|
52 | 53 | try: |
|
53 | 54 | testgen.next() |
|
54 | 55 | ok = True |
|
55 | 56 | except StopIteration: |
|
56 | 57 | # We stop the loop |
|
57 | 58 | break |
|
58 | 59 | except self.failureException: |
|
59 |
result.addFailure(self, s |
|
|
60 | result.addFailure(self, sys.exc_info()) | |
|
60 | 61 | except KeyboardInterrupt: |
|
61 | 62 | raise |
|
62 | 63 | except: |
|
63 |
result.addError(self, s |
|
|
64 | result.addError(self, sys.exc_info()) | |
|
64 | 65 | # TearDown |
|
65 | 66 | try: |
|
66 | 67 | self.tearDown() |
|
67 | 68 | except KeyboardInterrupt: |
|
68 | 69 | raise |
|
69 | 70 | except: |
|
70 |
result.addError(self, s |
|
|
71 | result.addError(self, sys.exc_info()) | |
|
71 | 72 | ok = False |
|
72 | 73 | if ok: result.addSuccess(self) |
|
73 | 74 | |
|
74 | 75 | finally: |
|
75 | 76 | result.stopTest(self) |
|
76 | 77 | |
|
77 | 78 | def run(self, result=None): |
|
78 | 79 | if result is None: |
|
79 | 80 | result = self.defaultTestResult() |
|
80 | 81 | testMethod = getattr(self, self._testMethodName) |
|
81 | 82 | # For normal tests, we just call the base class and return that |
|
82 | 83 | if isgenerator(testMethod): |
|
83 | 84 | return self.run_parametric(result, testMethod) |
|
84 | 85 | else: |
|
85 | 86 | return super(ParametricTestCase, self).run(result) |
|
86 | 87 | |
|
87 | 88 | |
|
88 | 89 | def parametric(func): |
|
89 | 90 | """Decorator to make a simple function into a normal test via unittest.""" |
|
90 | 91 | |
|
91 | 92 | class Tester(ParametricTestCase): |
|
92 | 93 | test = staticmethod(func) |
|
93 | 94 | |
|
94 | 95 | Tester.__name__ = func.__name__ |
|
95 | 96 | |
|
96 | 97 | return Tester |
General Comments 0
You need to be logged in to leave comments.
Login now