Use as a decorator: doctest a function's docstring as a unittest. This version runs normal doctests, but the idea is to make it later run ipython syntax instead.
(self, func)
| 127 | self.finder = DocTestFinder(verbose=verbose, recurse=False) |
| 128 | |
| 129 | def __call__(self, func): |
| 130 | """Use as a decorator: doctest a function's docstring as a unittest. |
| 131 | |
| 132 | This version runs normal doctests, but the idea is to make it later run |
| 133 | ipython syntax instead.""" |
| 134 | |
| 135 | # Capture the enclosing instance with a different name, so the new |
| 136 | # class below can see it without confusion regarding its own 'self' |
| 137 | # that will point to the test instance at runtime |
| 138 | d2u = self |
| 139 | |
| 140 | # Rewrite the function's docstring to have python syntax |
| 141 | if func.__doc__ is not None: |
| 142 | func.__doc__ = ip2py(func.__doc__) |
| 143 | |
| 144 | # Now, create a tester object that is a real unittest instance, so |
| 145 | # normal unittest machinery (or Nose, or Trial) can find it. |
| 146 | class Tester(unittest.TestCase): |
| 147 | def test(self): |
| 148 | # Make a new runner per function to be tested |
| 149 | runner = DocTestRunner(verbose=d2u.verbose) |
| 150 | for the_test in d2u.finder.find(func, func.__name__): |
| 151 | runner.run(the_test) |
| 152 | failed = count_failures(runner) |
| 153 | if failed: |
| 154 | # Since we only looked at a single function's docstring, |
| 155 | # failed should contain at most one item. More than that |
| 156 | # is a case we can't handle and should error out on |
| 157 | if len(failed) > 1: |
| 158 | err = "Invalid number of test results: %s" % failed |
| 159 | raise ValueError(err) |
| 160 | # Report a normal failure. |
| 161 | self.fail('failed doctests: %s' % str(failed[0])) |
| 162 | |
| 163 | # Rename it so test reports have the original signature. |
| 164 | Tester.__name__ = func.__name__ |
| 165 | return Tester |
| 166 | |
| 167 | |
| 168 | def ipdocstring(func): |
nothing calls this directly
no outgoing calls
no test coverage detected