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