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