Tim Peters | 4fd9e2f | 2001-08-18 00:05:50 +0000 | [diff] [blame] | 1 | # Module doctest. |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2 | # Released to the public domain 16-Jan-2001, by Tim Peters (tim@python.org). |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 3 | # Major enhancements and refactoring by: |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 4 | # Jim Fulton |
| 5 | # Edward Loper |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 6 | |
| 7 | # Provided as-is; use at your own risk; no warranty; no promises; enjoy! |
| 8 | |
Martin v. Löwis | 92816de | 2004-05-31 19:01:00 +0000 | [diff] [blame] | 9 | r"""Module doctest -- a framework for running examples in docstrings. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 10 | |
| 11 | NORMAL USAGE |
| 12 | |
Tim Peters | 80e5314 | 2004-08-09 04:34:45 +0000 | [diff] [blame] | 13 | In simplest use, end each module M to be tested with: |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 14 | |
| 15 | def _test(): |
Tim Peters | 80e5314 | 2004-08-09 04:34:45 +0000 | [diff] [blame] | 16 | import doctest |
| 17 | return doctest.testmod() |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 18 | |
| 19 | if __name__ == "__main__": |
| 20 | _test() |
| 21 | |
| 22 | Then running the module as a script will cause the examples in the |
| 23 | docstrings to get executed and verified: |
| 24 | |
| 25 | python M.py |
| 26 | |
| 27 | This won't display anything unless an example fails, in which case the |
| 28 | failing example(s) and the cause(s) of the failure(s) are printed to stdout |
| 29 | (why not stderr? because stderr is a lame hack <0.2 wink>), and the final |
| 30 | line of output is "Test failed.". |
| 31 | |
| 32 | Run it with the -v switch instead: |
| 33 | |
| 34 | python M.py -v |
| 35 | |
| 36 | and a detailed report of all examples tried is printed to stdout, along |
| 37 | with assorted summaries at the end. |
| 38 | |
Tim Peters | 80e5314 | 2004-08-09 04:34:45 +0000 | [diff] [blame] | 39 | You can force verbose mode by passing "verbose=True" to testmod, or prohibit |
| 40 | it by passing "verbose=False". In either of those cases, sys.argv is not |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 41 | examined by testmod. |
| 42 | |
| 43 | In any case, testmod returns a 2-tuple of ints (f, t), where f is the |
| 44 | number of docstring examples that failed and t is the total number of |
| 45 | docstring examples attempted. |
| 46 | |
Tim Peters | 80e5314 | 2004-08-09 04:34:45 +0000 | [diff] [blame] | 47 | There are a variety of other ways to run doctests, including integration |
| 48 | with the unittest framework, and support for running non-Python text |
| 49 | files containing doctests. There are also many ways to override parts |
| 50 | of doctest's default behaviors. See the Library Reference Manual for |
| 51 | details. |
| 52 | |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 53 | |
| 54 | WHICH DOCSTRINGS ARE EXAMINED? |
| 55 | |
| 56 | + M.__doc__. |
| 57 | |
| 58 | + f.__doc__ for all functions f in M.__dict__.values(), except those |
Raymond Hettinger | 71adf7e | 2003-07-16 19:25:22 +0000 | [diff] [blame] | 59 | defined in other modules. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 60 | |
Raymond Hettinger | 71adf7e | 2003-07-16 19:25:22 +0000 | [diff] [blame] | 61 | + C.__doc__ for all classes C in M.__dict__.values(), except those |
| 62 | defined in other modules. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 63 | |
| 64 | + If M.__test__ exists and "is true", it must be a dict, and |
| 65 | each entry maps a (string) name to a function object, class object, or |
| 66 | string. Function and class object docstrings found from M.__test__ |
Tim Peters | 80e5314 | 2004-08-09 04:34:45 +0000 | [diff] [blame] | 67 | are searched, and strings are searched directly as if they were docstrings. |
| 68 | In output, a key K in M.__test__ appears with name |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 69 | <name of M>.__test__.K |
| 70 | |
| 71 | Any classes found are recursively searched similarly, to test docstrings in |
Tim Peters | 80e5314 | 2004-08-09 04:34:45 +0000 | [diff] [blame] | 72 | their contained methods and nested classes. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 73 | |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 74 | |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 75 | WHAT'S THE EXECUTION CONTEXT? |
| 76 | |
| 77 | By default, each time testmod finds a docstring to test, it uses a *copy* |
| 78 | of M's globals (so that running tests on a module doesn't change the |
| 79 | module's real globals, and so that one test in M can't leave behind crumbs |
| 80 | that accidentally allow another test to work). This means examples can |
| 81 | freely use any names defined at top-level in M. It also means that sloppy |
| 82 | imports (see above) can cause examples in external docstrings to use |
| 83 | globals inappropriate for them. |
| 84 | |
| 85 | You can force use of your own dict as the execution context by passing |
| 86 | "globs=your_dict" to testmod instead. Presumably this would be a copy of |
| 87 | M.__dict__ merged with the globals from other imported modules. |
| 88 | |
| 89 | |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 90 | WHAT ABOUT EXCEPTIONS? |
| 91 | |
| 92 | No problem, as long as the only output generated by the example is the |
| 93 | traceback itself. For example: |
| 94 | |
Tim Peters | 60e23f4 | 2001-02-14 00:43:21 +0000 | [diff] [blame] | 95 | >>> [1, 2, 3].remove(42) |
Tim Peters | ea4f931 | 2001-02-13 20:54:42 +0000 | [diff] [blame] | 96 | Traceback (most recent call last): |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 97 | File "<stdin>", line 1, in ? |
Tim Peters | 60e23f4 | 2001-02-14 00:43:21 +0000 | [diff] [blame] | 98 | ValueError: list.remove(x): x not in list |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 99 | >>> |
| 100 | |
Tim Peters | 80e5314 | 2004-08-09 04:34:45 +0000 | [diff] [blame] | 101 | Note that only the exception type and value are compared. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 102 | |
| 103 | |
Tim Peters | 80e5314 | 2004-08-09 04:34:45 +0000 | [diff] [blame] | 104 | SO WHAT DOES A DOCTEST EXAMPLE LOOK LIKE ALREADY!? |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 105 | |
| 106 | Oh ya. It's easy! In most cases a copy-and-paste of an interactive |
| 107 | console session works fine -- just make sure the leading whitespace is |
| 108 | rigidly consistent (you can mix tabs and spaces if you're too lazy to do it |
| 109 | right, but doctest is not in the business of guessing what you think a tab |
| 110 | means). |
| 111 | |
| 112 | >>> # comments are ignored |
| 113 | >>> x = 12 |
| 114 | >>> x |
| 115 | 12 |
| 116 | >>> if x == 13: |
| 117 | ... print "yes" |
| 118 | ... else: |
| 119 | ... print "no" |
| 120 | ... print "NO" |
| 121 | ... print "NO!!!" |
| 122 | ... |
| 123 | no |
| 124 | NO |
| 125 | NO!!! |
| 126 | >>> |
| 127 | |
| 128 | Any expected output must immediately follow the final ">>>" or "..." line |
| 129 | containing the code, and the expected output (if any) extends to the next |
| 130 | ">>>" or all-whitespace line. That's it. |
| 131 | |
| 132 | Bummers: |
| 133 | |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 134 | + Output to stdout is captured, but not output to stderr (exception |
| 135 | tracebacks are captured via a different means). |
| 136 | |
Martin v. Löwis | 92816de | 2004-05-31 19:01:00 +0000 | [diff] [blame] | 137 | + If you continue a line via backslashing in an interactive session, |
| 138 | or for any other reason use a backslash, you should use a raw |
| 139 | docstring, which will preserve your backslahses exactly as you type |
| 140 | them: |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 141 | |
Tim Peters | 4e0e1b6 | 2004-07-07 20:54:48 +0000 | [diff] [blame] | 142 | >>> def f(x): |
Martin v. Löwis | 92816de | 2004-05-31 19:01:00 +0000 | [diff] [blame] | 143 | ... r'''Backslashes in a raw docstring: m\n''' |
| 144 | >>> print f.__doc__ |
| 145 | Backslashes in a raw docstring: m\n |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 146 | |
Martin v. Löwis | 92816de | 2004-05-31 19:01:00 +0000 | [diff] [blame] | 147 | Otherwise, the backslash will be interpreted as part of the string. |
| 148 | E.g., the "\n" above would be interpreted as a newline character. |
| 149 | Alternatively, you can double each backslash in the doctest version |
| 150 | (and not use a raw string): |
| 151 | |
Tim Peters | 4e0e1b6 | 2004-07-07 20:54:48 +0000 | [diff] [blame] | 152 | >>> def f(x): |
Martin v. Löwis | 92816de | 2004-05-31 19:01:00 +0000 | [diff] [blame] | 153 | ... '''Backslashes in a raw docstring: m\\n''' |
| 154 | >>> print f.__doc__ |
| 155 | Backslashes in a raw docstring: m\n |
Tim Peters | 4e0e1b6 | 2004-07-07 20:54:48 +0000 | [diff] [blame] | 156 | |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 157 | The starting column doesn't matter: |
| 158 | |
| 159 | >>> assert "Easy!" |
| 160 | >>> import math |
| 161 | >>> math.floor(1.9) |
| 162 | 1.0 |
| 163 | |
| 164 | and as many leading whitespace characters are stripped from the expected |
| 165 | output as appeared in the initial ">>>" line that triggered it. |
| 166 | |
| 167 | If you execute this very file, the examples above will be found and |
Tim Peters | 80e5314 | 2004-08-09 04:34:45 +0000 | [diff] [blame] | 168 | executed. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 169 | """ |
Edward Loper | 8e4a34b | 2004-08-12 02:34:27 +0000 | [diff] [blame] | 170 | __docformat__ = 'reStructuredText en' |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 171 | |
Tim Peters | 4fd9e2f | 2001-08-18 00:05:50 +0000 | [diff] [blame] | 172 | __all__ = [ |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 173 | 'is_private', |
| 174 | 'Example', |
| 175 | 'DocTest', |
| 176 | 'DocTestFinder', |
| 177 | 'DocTestRunner', |
Tim Peters | 4fd9e2f | 2001-08-18 00:05:50 +0000 | [diff] [blame] | 178 | 'testmod', |
| 179 | 'run_docstring_examples', |
Tim Peters | 4fd9e2f | 2001-08-18 00:05:50 +0000 | [diff] [blame] | 180 | 'Tester', |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 181 | 'DocTestCase', |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 182 | 'DocTestSuite', |
| 183 | 'testsource', |
| 184 | 'debug', |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 185 | # 'master', |
Tim Peters | 4fd9e2f | 2001-08-18 00:05:50 +0000 | [diff] [blame] | 186 | ] |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 187 | |
Tim Peters | 4fd9e2f | 2001-08-18 00:05:50 +0000 | [diff] [blame] | 188 | import __future__ |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 189 | |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 190 | import sys, traceback, inspect, linecache, os, re, types |
Jim Fulton | 356fd19 | 2004-08-09 11:34:47 +0000 | [diff] [blame] | 191 | import unittest, difflib, pdb, tempfile |
Tim Peters | f727c6c | 2004-08-08 01:48:59 +0000 | [diff] [blame] | 192 | import warnings |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 193 | from StringIO import StringIO |
Tim Peters | 7402f79 | 2001-10-02 03:53:41 +0000 | [diff] [blame] | 194 | |
Jim Fulton | 356fd19 | 2004-08-09 11:34:47 +0000 | [diff] [blame] | 195 | real_pdb_set_trace = pdb.set_trace |
| 196 | |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 197 | # There are 4 basic classes: |
| 198 | # - Example: a <source, want> pair, plus an intra-docstring line number. |
| 199 | # - DocTest: a collection of examples, parsed from a docstring, plus |
| 200 | # info about where the docstring came from (name, filename, lineno). |
| 201 | # - DocTestFinder: extracts DocTests from a given object's docstring and |
| 202 | # its contained objects' docstrings. |
| 203 | # - DocTestRunner: runs DocTest cases, and accumulates statistics. |
| 204 | # |
| 205 | # So the basic picture is: |
| 206 | # |
| 207 | # list of: |
| 208 | # +------+ +---------+ +-------+ |
| 209 | # |object| --DocTestFinder-> | DocTest | --DocTestRunner-> |results| |
| 210 | # +------+ +---------+ +-------+ |
| 211 | # | Example | |
| 212 | # | ... | |
| 213 | # | Example | |
| 214 | # +---------+ |
| 215 | |
Edward Loper | ac20f57 | 2004-08-12 02:02:24 +0000 | [diff] [blame] | 216 | # Option constants. |
| 217 | OPTIONFLAGS_BY_NAME = {} |
| 218 | def register_optionflag(name): |
| 219 | flag = 1 << len(OPTIONFLAGS_BY_NAME) |
| 220 | OPTIONFLAGS_BY_NAME[name] = flag |
| 221 | return flag |
| 222 | |
| 223 | DONT_ACCEPT_TRUE_FOR_1 = register_optionflag('DONT_ACCEPT_TRUE_FOR_1') |
| 224 | DONT_ACCEPT_BLANKLINE = register_optionflag('DONT_ACCEPT_BLANKLINE') |
| 225 | NORMALIZE_WHITESPACE = register_optionflag('NORMALIZE_WHITESPACE') |
| 226 | ELLIPSIS = register_optionflag('ELLIPSIS') |
| 227 | UNIFIED_DIFF = register_optionflag('UNIFIED_DIFF') |
| 228 | CONTEXT_DIFF = register_optionflag('CONTEXT_DIFF') |
| 229 | |
| 230 | # Special string markers for use in `want` strings: |
| 231 | BLANKLINE_MARKER = '<BLANKLINE>' |
| 232 | ELLIPSIS_MARKER = '...' |
| 233 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 234 | ###################################################################### |
| 235 | ## Table of Contents |
| 236 | ###################################################################### |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 237 | # 1. Utility Functions |
| 238 | # 2. Example & DocTest -- store test cases |
| 239 | # 3. DocTest Parser -- extracts examples from strings |
| 240 | # 4. DocTest Finder -- extracts test cases from objects |
| 241 | # 5. DocTest Runner -- runs test cases |
| 242 | # 6. Test Functions -- convenient wrappers for testing |
| 243 | # 7. Tester Class -- for backwards compatibility |
| 244 | # 8. Unittest Support |
| 245 | # 9. Debugging Support |
| 246 | # 10. Example Usage |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 247 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 248 | ###################################################################### |
| 249 | ## 1. Utility Functions |
| 250 | ###################################################################### |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 251 | |
| 252 | def is_private(prefix, base): |
| 253 | """prefix, base -> true iff name prefix + "." + base is "private". |
| 254 | |
| 255 | Prefix may be an empty string, and base does not contain a period. |
| 256 | Prefix is ignored (although functions you write conforming to this |
| 257 | protocol may make use of it). |
| 258 | Return true iff base begins with an (at least one) underscore, but |
| 259 | does not both begin and end with (at least) two underscores. |
| 260 | |
Tim Peters | bafb1fe | 2004-08-08 01:52:57 +0000 | [diff] [blame] | 261 | >>> warnings.filterwarnings("ignore", "is_private", DeprecationWarning, |
| 262 | ... "doctest", 0) |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 263 | >>> is_private("a.b", "my_func") |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 264 | False |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 265 | >>> is_private("____", "_my_func") |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 266 | True |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 267 | >>> is_private("someclass", "__init__") |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 268 | False |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 269 | >>> is_private("sometypo", "__init_") |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 270 | True |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 271 | >>> is_private("x.y.z", "_") |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 272 | True |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 273 | >>> is_private("_x.y.z", "__") |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 274 | False |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 275 | >>> is_private("", "") # senseless but consistent |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 276 | False |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 277 | """ |
Tim Peters | bafb1fe | 2004-08-08 01:52:57 +0000 | [diff] [blame] | 278 | warnings.warn("is_private is deprecated; it wasn't useful; " |
| 279 | "examine DocTestFinder.find() lists instead", |
Tim Peters | 3ddd60a | 2004-08-08 02:43:33 +0000 | [diff] [blame] | 280 | DeprecationWarning, stacklevel=2) |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 281 | return base[:1] == "_" and not base[:2] == "__" == base[-2:] |
| 282 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 283 | def _extract_future_flags(globs): |
| 284 | """ |
| 285 | Return the compiler-flags associated with the future features that |
| 286 | have been imported into the given namespace (globs). |
| 287 | """ |
| 288 | flags = 0 |
| 289 | for fname in __future__.all_feature_names: |
| 290 | feature = globs.get(fname, None) |
| 291 | if feature is getattr(__future__, fname): |
| 292 | flags |= feature.compiler_flag |
| 293 | return flags |
Tim Peters | 7402f79 | 2001-10-02 03:53:41 +0000 | [diff] [blame] | 294 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 295 | def _normalize_module(module, depth=2): |
| 296 | """ |
| 297 | Return the module specified by `module`. In particular: |
| 298 | - If `module` is a module, then return module. |
| 299 | - If `module` is a string, then import and return the |
| 300 | module with that name. |
| 301 | - If `module` is None, then return the calling module. |
| 302 | The calling module is assumed to be the module of |
| 303 | the stack frame at the given depth in the call stack. |
| 304 | """ |
| 305 | if inspect.ismodule(module): |
| 306 | return module |
| 307 | elif isinstance(module, (str, unicode)): |
| 308 | return __import__(module, globals(), locals(), ["*"]) |
| 309 | elif module is None: |
| 310 | return sys.modules[sys._getframe(depth).f_globals['__name__']] |
| 311 | else: |
| 312 | raise TypeError("Expected a module, string, or None") |
Tim Peters | 7402f79 | 2001-10-02 03:53:41 +0000 | [diff] [blame] | 313 | |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 314 | def _tag_msg(tag, msg, indent=' '): |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 315 | """ |
| 316 | Return a string that displays a tag-and-message pair nicely, |
| 317 | keeping the tag and its message on the same line when that |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 318 | makes sense. If the message is displayed on separate lines, |
| 319 | then `indent` is added to the beginning of each line. |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 320 | """ |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 321 | # If the message doesn't end in a newline, then add one. |
| 322 | if msg[-1:] != '\n': |
| 323 | msg += '\n' |
| 324 | # If the message is short enough, and contains no internal |
| 325 | # newlines, then display it on the same line as the tag. |
| 326 | # Otherwise, display the tag on its own line. |
| 327 | if (len(tag) + len(msg) < 75 and |
| 328 | msg.find('\n', 0, len(msg)-1) == -1): |
| 329 | return '%s: %s' % (tag, msg) |
| 330 | else: |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 331 | msg = '\n'.join([indent+l for l in msg[:-1].split('\n')]) |
| 332 | return '%s:\n%s\n' % (tag, msg) |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 333 | |
Edward Loper | 8e4a34b | 2004-08-12 02:34:27 +0000 | [diff] [blame] | 334 | def _exception_traceback(exc_info): |
| 335 | """ |
| 336 | Return a string containing a traceback message for the given |
| 337 | exc_info tuple (as returned by sys.exc_info()). |
| 338 | """ |
| 339 | # Get a traceback message. |
| 340 | excout = StringIO() |
| 341 | exc_type, exc_val, exc_tb = exc_info |
| 342 | traceback.print_exception(exc_type, exc_val, exc_tb, file=excout) |
| 343 | return excout.getvalue() |
| 344 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 345 | # Override some StringIO methods. |
| 346 | class _SpoofOut(StringIO): |
| 347 | def getvalue(self): |
| 348 | result = StringIO.getvalue(self) |
| 349 | # If anything at all was written, make sure there's a trailing |
| 350 | # newline. There's no way for the expected output to indicate |
| 351 | # that a trailing newline is missing. |
| 352 | if result and not result.endswith("\n"): |
| 353 | result += "\n" |
| 354 | # Prevent softspace from screwing up the next test case, in |
| 355 | # case they used print with a trailing comma in an example. |
| 356 | if hasattr(self, "softspace"): |
| 357 | del self.softspace |
| 358 | return result |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 359 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 360 | def truncate(self, size=None): |
| 361 | StringIO.truncate(self, size) |
| 362 | if hasattr(self, "softspace"): |
| 363 | del self.softspace |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 364 | |
Tim Peters | 26b3ebb | 2004-08-19 08:10:08 +0000 | [diff] [blame^] | 365 | # Worst-case linear-time ellipsis matching. |
| 366 | def ellipsis_match(want, got): |
| 367 | if ELLIPSIS_MARKER not in want: |
| 368 | return want == got |
| 369 | # Remove \n from ...\n, else the newline will be required, |
| 370 | # and (for example) ... on a line by itself can't match |
| 371 | # nothing gracefully. |
| 372 | want = want.replace(ELLIPSIS_MARKER + '\n', ELLIPSIS_MARKER) |
| 373 | # Find "the real" strings. |
| 374 | ws = want.split(ELLIPSIS_MARKER) |
| 375 | assert len(ws) >= 2 |
| 376 | # Match. In general, we only need to find the leftmost non-overlapping |
| 377 | # match for each piece. "Real strings" at the start or end of `want` |
| 378 | # are special cases. |
| 379 | w = ws[0] |
| 380 | if w: |
| 381 | # An ellipsis didn't start `want`. We need to match exactly |
| 382 | # at the start. |
| 383 | if not got.startswith(w): |
| 384 | return False |
| 385 | pos = len(w) |
| 386 | del ws[0] |
| 387 | else: |
| 388 | pos = 0 |
| 389 | |
| 390 | for w in ws: |
| 391 | # w may be '' at times, if there are consecutive ellipses, or |
| 392 | # due to an ellipsis at the start or end of `want`. That's OK. |
| 393 | # Search for an empty string succeeds, and doesn't change pos. |
| 394 | pos = got.find(w, pos) |
| 395 | if pos < 0: |
| 396 | return False |
| 397 | pos += len(w) |
| 398 | |
| 399 | # If `want` ended with an ellipsis, the tail matches anything. |
| 400 | if ws[-1] == '': |
| 401 | return True |
| 402 | # Else `want` ended with a real string. If the last real match |
| 403 | # exhausted `got`, we win. |
| 404 | if pos == len(got): |
| 405 | return True |
| 406 | # Else maybe we matched the last real string too early. |
| 407 | return got.endswith(ws[-1]) |
| 408 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 409 | ###################################################################### |
| 410 | ## 2. Example & DocTest |
| 411 | ###################################################################### |
| 412 | ## - An "example" is a <source, want> pair, where "source" is a |
| 413 | ## fragment of source code, and "want" is the expected output for |
| 414 | ## "source." The Example class also includes information about |
| 415 | ## where the example was extracted from. |
| 416 | ## |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 417 | ## - A "doctest" is a collection of examples, typically extracted from |
| 418 | ## a string (such as an object's docstring). The DocTest class also |
| 419 | ## includes information about where the string was extracted from. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 420 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 421 | class Example: |
| 422 | """ |
| 423 | A single doctest example, consisting of source code and expected |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 424 | output. `Example` defines the following attributes: |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 425 | |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 426 | - source: A single Python statement, always ending with a newline. |
Tim Peters | bb43147 | 2004-08-09 03:51:46 +0000 | [diff] [blame] | 427 | The constructor adds a newline if needed. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 428 | |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 429 | - want: The expected output from running the source code (either |
Tim Peters | bb43147 | 2004-08-09 03:51:46 +0000 | [diff] [blame] | 430 | from stdout, or a traceback in case of exception). `want` ends |
| 431 | with a newline unless it's empty, in which case it's an empty |
| 432 | string. The constructor adds a newline if needed. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 433 | |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 434 | - lineno: The line number within the DocTest string containing |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 435 | this Example where the Example begins. This line number is |
| 436 | zero-based, with respect to the beginning of the DocTest. |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 437 | |
| 438 | - indent: The example's indentation in the DocTest string. |
| 439 | I.e., the number of space characters that preceed the |
| 440 | example's first prompt. |
| 441 | |
| 442 | - options: A dictionary mapping from option flags to True or |
| 443 | False, which is used to override default options for this |
| 444 | example. Any option flags not contained in this dictionary |
| 445 | are left at their default value (as specified by the |
| 446 | DocTestRunner's optionflags). By default, no options are set. |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 447 | """ |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 448 | def __init__(self, source, want, lineno, indent=0, options=None): |
Tim Peters | bb43147 | 2004-08-09 03:51:46 +0000 | [diff] [blame] | 449 | # Normalize inputs. |
| 450 | if not source.endswith('\n'): |
| 451 | source += '\n' |
| 452 | if want and not want.endswith('\n'): |
| 453 | want += '\n' |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 454 | # Store properties. |
| 455 | self.source = source |
| 456 | self.want = want |
| 457 | self.lineno = lineno |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 458 | self.indent = indent |
| 459 | if options is None: options = {} |
| 460 | self.options = options |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 461 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 462 | class DocTest: |
| 463 | """ |
| 464 | A collection of doctest examples that should be run in a single |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 465 | namespace. Each `DocTest` defines the following attributes: |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 466 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 467 | - examples: the list of examples. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 468 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 469 | - globs: The namespace (aka globals) that the examples should |
| 470 | be run in. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 471 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 472 | - name: A name identifying the DocTest (typically, the name of |
| 473 | the object whose docstring this DocTest was extracted from). |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 474 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 475 | - filename: The name of the file that this DocTest was extracted |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 476 | from, or `None` if the filename is unknown. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 477 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 478 | - lineno: The line number within filename where this DocTest |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 479 | begins, or `None` if the line number is unavailable. This |
| 480 | line number is zero-based, with respect to the beginning of |
| 481 | the file. |
| 482 | |
| 483 | - docstring: The string that the examples were extracted from, |
| 484 | or `None` if the string is unavailable. |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 485 | """ |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 486 | def __init__(self, examples, globs, name, filename, lineno, docstring): |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 487 | """ |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 488 | Create a new DocTest containing the given examples. The |
| 489 | DocTest's globals are initialized with a copy of `globs`. |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 490 | """ |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 491 | assert not isinstance(examples, basestring), \ |
| 492 | "DocTest no longer accepts str; use DocTestParser instead" |
| 493 | self.examples = examples |
| 494 | self.docstring = docstring |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 495 | self.globs = globs.copy() |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 496 | self.name = name |
| 497 | self.filename = filename |
| 498 | self.lineno = lineno |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 499 | |
| 500 | def __repr__(self): |
| 501 | if len(self.examples) == 0: |
| 502 | examples = 'no examples' |
| 503 | elif len(self.examples) == 1: |
| 504 | examples = '1 example' |
| 505 | else: |
| 506 | examples = '%d examples' % len(self.examples) |
| 507 | return ('<DocTest %s from %s:%s (%s)>' % |
| 508 | (self.name, self.filename, self.lineno, examples)) |
| 509 | |
| 510 | |
| 511 | # This lets us sort tests by name: |
| 512 | def __cmp__(self, other): |
| 513 | if not isinstance(other, DocTest): |
| 514 | return -1 |
| 515 | return cmp((self.name, self.filename, self.lineno, id(self)), |
| 516 | (other.name, other.filename, other.lineno, id(other))) |
| 517 | |
| 518 | ###################################################################### |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 519 | ## 2. DocTestParser |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 520 | ###################################################################### |
| 521 | |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 522 | class DocTestParser: |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 523 | """ |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 524 | A class used to parse strings containing doctest examples. |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 525 | """ |
Edward Loper | 8e4a34b | 2004-08-12 02:34:27 +0000 | [diff] [blame] | 526 | # This regular expression is used to find doctest examples in a |
| 527 | # string. It defines three groups: `source` is the source code |
| 528 | # (including leading indentation and prompts); `indent` is the |
| 529 | # indentation of the first (PS1) line of the source code; and |
| 530 | # `want` is the expected output (including leading indentation). |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 531 | _EXAMPLE_RE = re.compile(r''' |
Tim Peters | d40a92b | 2004-08-09 03:28:45 +0000 | [diff] [blame] | 532 | # Source consists of a PS1 line followed by zero or more PS2 lines. |
| 533 | (?P<source> |
| 534 | (?:^(?P<indent> [ ]*) >>> .*) # PS1 line |
| 535 | (?:\n [ ]* \.\.\. .*)*) # PS2 lines |
| 536 | \n? |
| 537 | # Want consists of any non-blank lines that do not start with PS1. |
| 538 | (?P<want> (?:(?![ ]*$) # Not a blank line |
| 539 | (?![ ]*>>>) # Not a line starting with PS1 |
| 540 | .*$\n? # But any other line |
| 541 | )*) |
| 542 | ''', re.MULTILINE | re.VERBOSE) |
Edward Loper | 8e4a34b | 2004-08-12 02:34:27 +0000 | [diff] [blame] | 543 | |
Tim Peters | 7ea48dd | 2004-08-13 01:52:59 +0000 | [diff] [blame] | 544 | # A callable returning a true value iff its argument is a blank line |
| 545 | # or contains a single comment. |
Edward Loper | 8e4a34b | 2004-08-12 02:34:27 +0000 | [diff] [blame] | 546 | _IS_BLANK_OR_COMMENT = re.compile(r'^[ ]*(#.*)?$').match |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 547 | |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 548 | def get_doctest(self, string, globs, name, filename, lineno): |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 549 | """ |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 550 | Extract all doctest examples from the given string, and |
| 551 | collect them into a `DocTest` object. |
| 552 | |
| 553 | `globs`, `name`, `filename`, and `lineno` are attributes for |
| 554 | the new `DocTest` object. See the documentation for `DocTest` |
| 555 | for more information. |
| 556 | """ |
| 557 | return DocTest(self.get_examples(string, name), globs, |
| 558 | name, filename, lineno, string) |
| 559 | |
| 560 | def get_examples(self, string, name='<string>'): |
| 561 | """ |
| 562 | Extract all doctest examples from the given string, and return |
| 563 | them as a list of `Example` objects. Line numbers are |
| 564 | 0-based, because it's most common in doctests that nothing |
| 565 | interesting appears on the same line as opening triple-quote, |
| 566 | and so the first interesting line is called \"line 1\" then. |
| 567 | |
| 568 | The optional argument `name` is a name identifying this |
| 569 | string, and is only used for error messages. |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 570 | |
| 571 | >>> text = ''' |
| 572 | ... >>> x, y = 2, 3 # no output expected |
| 573 | ... >>> if 1: |
| 574 | ... ... print x |
| 575 | ... ... print y |
| 576 | ... 2 |
| 577 | ... 3 |
| 578 | ... |
| 579 | ... Some text. |
| 580 | ... >>> x+y |
| 581 | ... 5 |
| 582 | ... ''' |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 583 | >>> for x in DocTestParser().get_examples(text): |
Edward Loper | 78b58f3 | 2004-08-09 02:56:02 +0000 | [diff] [blame] | 584 | ... print (x.source, x.want, x.lineno) |
Tim Peters | bb43147 | 2004-08-09 03:51:46 +0000 | [diff] [blame] | 585 | ('x, y = 2, 3 # no output expected\\n', '', 1) |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 586 | ('if 1:\\n print x\\n print y\\n', '2\\n3\\n', 2) |
Tim Peters | bb43147 | 2004-08-09 03:51:46 +0000 | [diff] [blame] | 587 | ('x+y\\n', '5\\n', 9) |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 588 | """ |
| 589 | examples = [] |
| 590 | charno, lineno = 0, 0 |
| 591 | # Find all doctest examples in the string: |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 592 | for m in self._EXAMPLE_RE.finditer(string.expandtabs()): |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 593 | # Update lineno (lines before this example) |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 594 | lineno += string.count('\n', charno, m.start()) |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 595 | # Extract source/want from the regexp match. |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 596 | (source, want) = self._parse_example(m, name, lineno) |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 597 | # Extract extra options from the source. |
| 598 | options = self._find_options(source, name, lineno) |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 599 | # Create an Example, and add it to the list. |
Edward Loper | b51b234 | 2004-08-17 16:37:12 +0000 | [diff] [blame] | 600 | if not self._IS_BLANK_OR_COMMENT(source): |
| 601 | examples.append( Example(source, want, lineno, |
| 602 | len(m.group('indent')), options) ) |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 603 | # Update lineno (lines inside this example) |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 604 | lineno += string.count('\n', m.start(), m.end()) |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 605 | # Update charno. |
| 606 | charno = m.end() |
| 607 | return examples |
| 608 | |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 609 | def get_program(self, string, name="<string>"): |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 610 | """ |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 611 | Return an executable program from the given string, as a string. |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 612 | |
| 613 | The format of this isn't rigidly defined. In general, doctest |
| 614 | examples become the executable statements in the result, and |
| 615 | their expected outputs become comments, preceded by an \"#Expected:\" |
| 616 | comment. Everything else (text, comments, everything not part of |
| 617 | a doctest test) is also placed in comments. |
| 618 | |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 619 | The optional argument `name` is a name identifying this |
| 620 | string, and is only used for error messages. |
| 621 | |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 622 | >>> text = ''' |
| 623 | ... >>> x, y = 2, 3 # no output expected |
| 624 | ... >>> if 1: |
| 625 | ... ... print x |
| 626 | ... ... print y |
| 627 | ... 2 |
| 628 | ... 3 |
| 629 | ... |
| 630 | ... Some text. |
| 631 | ... >>> x+y |
| 632 | ... 5 |
| 633 | ... ''' |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 634 | >>> print DocTestParser().get_program(text) |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 635 | x, y = 2, 3 # no output expected |
| 636 | if 1: |
| 637 | print x |
| 638 | print y |
| 639 | # Expected: |
Edward Loper | a5db600 | 2004-08-12 02:41:30 +0000 | [diff] [blame] | 640 | ## 2 |
| 641 | ## 3 |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 642 | # |
Edward Loper | a5db600 | 2004-08-12 02:41:30 +0000 | [diff] [blame] | 643 | # Some text. |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 644 | x+y |
| 645 | # Expected: |
Edward Loper | a5db600 | 2004-08-12 02:41:30 +0000 | [diff] [blame] | 646 | ## 5 |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 647 | """ |
Edward Loper | a5db600 | 2004-08-12 02:41:30 +0000 | [diff] [blame] | 648 | string = string.expandtabs() |
| 649 | # If all lines begin with the same indentation, then strip it. |
| 650 | min_indent = self._min_indent(string) |
| 651 | if min_indent > 0: |
| 652 | string = '\n'.join([l[min_indent:] for l in string.split('\n')]) |
| 653 | |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 654 | output = [] |
| 655 | charnum, lineno = 0, 0 |
| 656 | # Find all doctest examples in the string: |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 657 | for m in self._EXAMPLE_RE.finditer(string.expandtabs()): |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 658 | # Add any text before this example, as a comment. |
| 659 | if m.start() > charnum: |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 660 | lines = string[charnum:m.start()-1].split('\n') |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 661 | output.extend([self._comment_line(l) for l in lines]) |
| 662 | lineno += len(lines) |
| 663 | |
| 664 | # Extract source/want from the regexp match. |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 665 | (source, want) = self._parse_example(m, name, lineno) |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 666 | # Display the source |
| 667 | output.append(source) |
| 668 | # Display the expected output, if any |
| 669 | if want: |
| 670 | output.append('# Expected:') |
Edward Loper | a5db600 | 2004-08-12 02:41:30 +0000 | [diff] [blame] | 671 | output.extend(['## '+l for l in want.split('\n')]) |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 672 | |
| 673 | # Update the line number & char number. |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 674 | lineno += string.count('\n', m.start(), m.end()) |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 675 | charnum = m.end() |
| 676 | # Add any remaining text, as comments. |
| 677 | output.extend([self._comment_line(l) |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 678 | for l in string[charnum:].split('\n')]) |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 679 | # Trim junk on both ends. |
| 680 | while output and output[-1] == '#': |
| 681 | output.pop() |
| 682 | while output and output[0] == '#': |
| 683 | output.pop(0) |
| 684 | # Combine the output, and return it. |
| 685 | return '\n'.join(output) |
| 686 | |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 687 | def _parse_example(self, m, name, lineno): |
| 688 | """ |
| 689 | Given a regular expression match from `_EXAMPLE_RE` (`m`), |
| 690 | return a pair `(source, want)`, where `source` is the matched |
| 691 | example's source code (with prompts and indentation stripped); |
| 692 | and `want` is the example's expected output (with indentation |
| 693 | stripped). |
| 694 | |
| 695 | `name` is the string's name, and `lineno` is the line number |
| 696 | where the example starts; both are used for error messages. |
| 697 | """ |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 698 | # Get the example's indentation level. |
| 699 | indent = len(m.group('indent')) |
| 700 | |
| 701 | # Divide source into lines; check that they're properly |
| 702 | # indented; and then strip their indentation & prompts. |
| 703 | source_lines = m.group('source').split('\n') |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 704 | self._check_prompt_blank(source_lines, indent, name, lineno) |
| 705 | self._check_prefix(source_lines[1:], ' '*indent+'.', name, lineno) |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 706 | source = '\n'.join([sl[indent+4:] for sl in source_lines]) |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 707 | |
| 708 | # Divide want into lines; check that it's properly |
| 709 | # indented; and then strip the indentation. |
| 710 | want_lines = m.group('want').rstrip().split('\n') |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 711 | self._check_prefix(want_lines, ' '*indent, name, |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 712 | lineno+len(source_lines)) |
| 713 | want = '\n'.join([wl[indent:] for wl in want_lines]) |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 714 | |
| 715 | return source, want |
| 716 | |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 717 | # This regular expression looks for option directives in the |
| 718 | # source code of an example. Option directives are comments |
| 719 | # starting with "doctest:". Warning: this may give false |
| 720 | # positives for string-literals that contain the string |
| 721 | # "#doctest:". Eliminating these false positives would require |
| 722 | # actually parsing the string; but we limit them by ignoring any |
| 723 | # line containing "#doctest:" that is *followed* by a quote mark. |
| 724 | _OPTION_DIRECTIVE_RE = re.compile(r'#\s*doctest:\s*([^\n\'"]*)$', |
| 725 | re.MULTILINE) |
| 726 | |
| 727 | def _find_options(self, source, name, lineno): |
| 728 | """ |
| 729 | Return a dictionary containing option overrides extracted from |
| 730 | option directives in the given source string. |
| 731 | |
| 732 | `name` is the string's name, and `lineno` is the line number |
| 733 | where the example starts; both are used for error messages. |
| 734 | """ |
| 735 | options = {} |
| 736 | # (note: with the current regexp, this will match at most once:) |
| 737 | for m in self._OPTION_DIRECTIVE_RE.finditer(source): |
| 738 | option_strings = m.group(1).replace(',', ' ').split() |
| 739 | for option in option_strings: |
| 740 | if (option[0] not in '+-' or |
| 741 | option[1:] not in OPTIONFLAGS_BY_NAME): |
| 742 | raise ValueError('line %r of the doctest for %s ' |
| 743 | 'has an invalid option: %r' % |
| 744 | (lineno+1, name, option)) |
| 745 | flag = OPTIONFLAGS_BY_NAME[option[1:]] |
| 746 | options[flag] = (option[0] == '+') |
| 747 | if options and self._IS_BLANK_OR_COMMENT(source): |
| 748 | raise ValueError('line %r of the doctest for %s has an option ' |
| 749 | 'directive on a line with no example: %r' % |
| 750 | (lineno, name, source)) |
| 751 | return options |
| 752 | |
Edward Loper | a5db600 | 2004-08-12 02:41:30 +0000 | [diff] [blame] | 753 | # This regular expression finds the indentation of every non-blank |
| 754 | # line in a string. |
| 755 | _INDENT_RE = re.compile('^([ ]+)(?=\S)', re.MULTILINE) |
| 756 | |
| 757 | def _min_indent(self, s): |
| 758 | "Return the minimum indentation of any non-blank line in `s`" |
| 759 | return min([len(indent) for indent in self._INDENT_RE.findall(s)]) |
| 760 | |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 761 | def _comment_line(self, line): |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 762 | "Return a commented form of the given line" |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 763 | line = line.rstrip() |
Tim Peters | dd0e475 | 2004-08-09 03:31:56 +0000 | [diff] [blame] | 764 | if line: |
Edward Loper | a5db600 | 2004-08-12 02:41:30 +0000 | [diff] [blame] | 765 | return '# '+line |
Tim Peters | dd0e475 | 2004-08-09 03:31:56 +0000 | [diff] [blame] | 766 | else: |
| 767 | return '#' |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 768 | |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 769 | def _check_prompt_blank(self, lines, indent, name, lineno): |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 770 | """ |
| 771 | Given the lines of a source string (including prompts and |
| 772 | leading indentation), check to make sure that every prompt is |
| 773 | followed by a space character. If any line is not followed by |
| 774 | a space character, then raise ValueError. |
| 775 | """ |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 776 | for i, line in enumerate(lines): |
| 777 | if len(line) >= indent+4 and line[indent+3] != ' ': |
| 778 | raise ValueError('line %r of the docstring for %s ' |
| 779 | 'lacks blank after %s: %r' % |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 780 | (lineno+i+1, name, |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 781 | line[indent:indent+3], line)) |
| 782 | |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 783 | def _check_prefix(self, lines, prefix, name, lineno): |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 784 | """ |
| 785 | Check that every line in the given list starts with the given |
| 786 | prefix; if any line does not, then raise a ValueError. |
| 787 | """ |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 788 | for i, line in enumerate(lines): |
| 789 | if line and not line.startswith(prefix): |
| 790 | raise ValueError('line %r of the docstring for %s has ' |
| 791 | 'inconsistent leading whitespace: %r' % |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 792 | (lineno+i+1, name, line)) |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 793 | |
| 794 | |
| 795 | ###################################################################### |
| 796 | ## 4. DocTest Finder |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 797 | ###################################################################### |
| 798 | |
| 799 | class DocTestFinder: |
| 800 | """ |
| 801 | A class used to extract the DocTests that are relevant to a given |
| 802 | object, from its docstring and the docstrings of its contained |
| 803 | objects. Doctests can currently be extracted from the following |
| 804 | object types: modules, functions, classes, methods, staticmethods, |
| 805 | classmethods, and properties. |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 806 | """ |
| 807 | |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 808 | def __init__(self, verbose=False, parser=DocTestParser(), |
Tim Peters | f727c6c | 2004-08-08 01:48:59 +0000 | [diff] [blame] | 809 | recurse=True, _namefilter=None): |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 810 | """ |
| 811 | Create a new doctest finder. |
| 812 | |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 813 | The optional argument `parser` specifies a class or |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 814 | function that should be used to create new DocTest objects (or |
Tim Peters | 161c963 | 2004-08-08 03:38:33 +0000 | [diff] [blame] | 815 | objects that implement the same interface as DocTest). The |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 816 | signature for this factory function should match the signature |
| 817 | of the DocTest constructor. |
| 818 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 819 | If the optional argument `recurse` is false, then `find` will |
| 820 | only examine the given object, and not any contained objects. |
| 821 | """ |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 822 | self._parser = parser |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 823 | self._verbose = verbose |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 824 | self._recurse = recurse |
Tim Peters | f727c6c | 2004-08-08 01:48:59 +0000 | [diff] [blame] | 825 | # _namefilter is undocumented, and exists only for temporary backward- |
| 826 | # compatibility support of testmod's deprecated isprivate mess. |
| 827 | self._namefilter = _namefilter |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 828 | |
| 829 | def find(self, obj, name=None, module=None, globs=None, |
Tim Peters | f3f5747 | 2004-08-08 06:11:48 +0000 | [diff] [blame] | 830 | extraglobs=None): |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 831 | """ |
| 832 | Return a list of the DocTests that are defined by the given |
| 833 | object's docstring, or by any of its contained objects' |
| 834 | docstrings. |
| 835 | |
| 836 | The optional parameter `module` is the module that contains |
Tim Peters | f3f5747 | 2004-08-08 06:11:48 +0000 | [diff] [blame] | 837 | the given object. If the module is not specified or is None, then |
| 838 | the test finder will attempt to automatically determine the |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 839 | correct module. The object's module is used: |
| 840 | |
| 841 | - As a default namespace, if `globs` is not specified. |
| 842 | - To prevent the DocTestFinder from extracting DocTests |
Tim Peters | f3f5747 | 2004-08-08 06:11:48 +0000 | [diff] [blame] | 843 | from objects that are imported from other modules. |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 844 | - To find the name of the file containing the object. |
| 845 | - To help find the line number of the object within its |
| 846 | file. |
| 847 | |
Tim Peters | f3f5747 | 2004-08-08 06:11:48 +0000 | [diff] [blame] | 848 | Contained objects whose module does not match `module` are ignored. |
| 849 | |
| 850 | If `module` is False, no attempt to find the module will be made. |
| 851 | This is obscure, of use mostly in tests: if `module` is False, or |
| 852 | is None but cannot be found automatically, then all objects are |
| 853 | considered to belong to the (non-existent) module, so all contained |
| 854 | objects will (recursively) be searched for doctests. |
| 855 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 856 | The globals for each DocTest is formed by combining `globs` |
| 857 | and `extraglobs` (bindings in `extraglobs` override bindings |
| 858 | in `globs`). A new copy of the globals dictionary is created |
| 859 | for each DocTest. If `globs` is not specified, then it |
| 860 | defaults to the module's `__dict__`, if specified, or {} |
| 861 | otherwise. If `extraglobs` is not specified, then it defaults |
| 862 | to {}. |
| 863 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 864 | """ |
| 865 | # If name was not specified, then extract it from the object. |
| 866 | if name is None: |
| 867 | name = getattr(obj, '__name__', None) |
| 868 | if name is None: |
| 869 | raise ValueError("DocTestFinder.find: name must be given " |
| 870 | "when obj.__name__ doesn't exist: %r" % |
| 871 | (type(obj),)) |
| 872 | |
| 873 | # Find the module that contains the given object (if obj is |
| 874 | # a module, then module=obj.). Note: this may fail, in which |
| 875 | # case module will be None. |
Tim Peters | f3f5747 | 2004-08-08 06:11:48 +0000 | [diff] [blame] | 876 | if module is False: |
| 877 | module = None |
| 878 | elif module is None: |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 879 | module = inspect.getmodule(obj) |
| 880 | |
| 881 | # Read the module's source code. This is used by |
| 882 | # DocTestFinder._find_lineno to find the line number for a |
| 883 | # given object's docstring. |
| 884 | try: |
| 885 | file = inspect.getsourcefile(obj) or inspect.getfile(obj) |
| 886 | source_lines = linecache.getlines(file) |
| 887 | if not source_lines: |
| 888 | source_lines = None |
| 889 | except TypeError: |
| 890 | source_lines = None |
| 891 | |
| 892 | # Initialize globals, and merge in extraglobs. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 893 | if globs is None: |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 894 | if module is None: |
| 895 | globs = {} |
| 896 | else: |
| 897 | globs = module.__dict__.copy() |
| 898 | else: |
| 899 | globs = globs.copy() |
| 900 | if extraglobs is not None: |
| 901 | globs.update(extraglobs) |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 902 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 903 | # Recursively expore `obj`, extracting DocTests. |
| 904 | tests = [] |
Tim Peters | f3f5747 | 2004-08-08 06:11:48 +0000 | [diff] [blame] | 905 | self._find(tests, obj, name, module, source_lines, globs, {}) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 906 | return tests |
| 907 | |
| 908 | def _filter(self, obj, prefix, base): |
| 909 | """ |
| 910 | Return true if the given object should not be examined. |
| 911 | """ |
Tim Peters | f727c6c | 2004-08-08 01:48:59 +0000 | [diff] [blame] | 912 | return (self._namefilter is not None and |
| 913 | self._namefilter(prefix, base)) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 914 | |
| 915 | def _from_module(self, module, object): |
| 916 | """ |
| 917 | Return true if the given object is defined in the given |
| 918 | module. |
| 919 | """ |
| 920 | if module is None: |
| 921 | return True |
| 922 | elif inspect.isfunction(object): |
| 923 | return module.__dict__ is object.func_globals |
| 924 | elif inspect.isclass(object): |
| 925 | return module.__name__ == object.__module__ |
| 926 | elif inspect.getmodule(object) is not None: |
| 927 | return module is inspect.getmodule(object) |
| 928 | elif hasattr(object, '__module__'): |
| 929 | return module.__name__ == object.__module__ |
| 930 | elif isinstance(object, property): |
| 931 | return True # [XX] no way not be sure. |
| 932 | else: |
| 933 | raise ValueError("object must be a class or function") |
| 934 | |
Tim Peters | f3f5747 | 2004-08-08 06:11:48 +0000 | [diff] [blame] | 935 | def _find(self, tests, obj, name, module, source_lines, globs, seen): |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 936 | """ |
| 937 | Find tests for the given object and any contained objects, and |
| 938 | add them to `tests`. |
| 939 | """ |
| 940 | if self._verbose: |
| 941 | print 'Finding tests in %s' % name |
| 942 | |
| 943 | # If we've already processed this object, then ignore it. |
| 944 | if id(obj) in seen: |
| 945 | return |
| 946 | seen[id(obj)] = 1 |
| 947 | |
| 948 | # Find a test for this object, and add it to the list of tests. |
| 949 | test = self._get_test(obj, name, module, globs, source_lines) |
| 950 | if test is not None: |
| 951 | tests.append(test) |
| 952 | |
| 953 | # Look for tests in a module's contained objects. |
| 954 | if inspect.ismodule(obj) and self._recurse: |
| 955 | for valname, val in obj.__dict__.items(): |
| 956 | # Check if this contained object should be ignored. |
| 957 | if self._filter(val, name, valname): |
| 958 | continue |
| 959 | valname = '%s.%s' % (name, valname) |
| 960 | # Recurse to functions & classes. |
| 961 | if ((inspect.isfunction(val) or inspect.isclass(val)) and |
Tim Peters | f3f5747 | 2004-08-08 06:11:48 +0000 | [diff] [blame] | 962 | self._from_module(module, val)): |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 963 | self._find(tests, val, valname, module, source_lines, |
Tim Peters | f3f5747 | 2004-08-08 06:11:48 +0000 | [diff] [blame] | 964 | globs, seen) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 965 | |
| 966 | # Look for tests in a module's __test__ dictionary. |
| 967 | if inspect.ismodule(obj) and self._recurse: |
| 968 | for valname, val in getattr(obj, '__test__', {}).items(): |
| 969 | if not isinstance(valname, basestring): |
| 970 | raise ValueError("DocTestFinder.find: __test__ keys " |
| 971 | "must be strings: %r" % |
| 972 | (type(valname),)) |
| 973 | if not (inspect.isfunction(val) or inspect.isclass(val) or |
| 974 | inspect.ismethod(val) or inspect.ismodule(val) or |
| 975 | isinstance(val, basestring)): |
| 976 | raise ValueError("DocTestFinder.find: __test__ values " |
| 977 | "must be strings, functions, methods, " |
| 978 | "classes, or modules: %r" % |
| 979 | (type(val),)) |
| 980 | valname = '%s.%s' % (name, valname) |
| 981 | self._find(tests, val, valname, module, source_lines, |
Tim Peters | f3f5747 | 2004-08-08 06:11:48 +0000 | [diff] [blame] | 982 | globs, seen) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 983 | |
| 984 | # Look for tests in a class's contained objects. |
| 985 | if inspect.isclass(obj) and self._recurse: |
| 986 | for valname, val in obj.__dict__.items(): |
| 987 | # Check if this contained object should be ignored. |
| 988 | if self._filter(val, name, valname): |
| 989 | continue |
| 990 | # Special handling for staticmethod/classmethod. |
| 991 | if isinstance(val, staticmethod): |
| 992 | val = getattr(obj, valname) |
| 993 | if isinstance(val, classmethod): |
| 994 | val = getattr(obj, valname).im_func |
| 995 | |
| 996 | # Recurse to methods, properties, and nested classes. |
| 997 | if ((inspect.isfunction(val) or inspect.isclass(val) or |
Tim Peters | f3f5747 | 2004-08-08 06:11:48 +0000 | [diff] [blame] | 998 | isinstance(val, property)) and |
| 999 | self._from_module(module, val)): |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1000 | valname = '%s.%s' % (name, valname) |
| 1001 | self._find(tests, val, valname, module, source_lines, |
Tim Peters | f3f5747 | 2004-08-08 06:11:48 +0000 | [diff] [blame] | 1002 | globs, seen) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1003 | |
| 1004 | def _get_test(self, obj, name, module, globs, source_lines): |
| 1005 | """ |
| 1006 | Return a DocTest for the given object, if it defines a docstring; |
| 1007 | otherwise, return None. |
| 1008 | """ |
| 1009 | # Extract the object's docstring. If it doesn't have one, |
| 1010 | # then return None (no test for this object). |
| 1011 | if isinstance(obj, basestring): |
| 1012 | docstring = obj |
| 1013 | else: |
| 1014 | try: |
| 1015 | if obj.__doc__ is None: |
| 1016 | return None |
| 1017 | docstring = str(obj.__doc__) |
| 1018 | except (TypeError, AttributeError): |
| 1019 | return None |
| 1020 | |
| 1021 | # Don't bother if the docstring is empty. |
| 1022 | if not docstring: |
| 1023 | return None |
| 1024 | |
| 1025 | # Find the docstring's location in the file. |
| 1026 | lineno = self._find_lineno(obj, source_lines) |
| 1027 | |
| 1028 | # Return a DocTest for this object. |
| 1029 | if module is None: |
| 1030 | filename = None |
| 1031 | else: |
| 1032 | filename = getattr(module, '__file__', module.__name__) |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 1033 | return self._parser.get_doctest(docstring, globs, name, |
| 1034 | filename, lineno) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1035 | |
| 1036 | def _find_lineno(self, obj, source_lines): |
| 1037 | """ |
| 1038 | Return a line number of the given object's docstring. Note: |
| 1039 | this method assumes that the object has a docstring. |
| 1040 | """ |
| 1041 | lineno = None |
| 1042 | |
| 1043 | # Find the line number for modules. |
| 1044 | if inspect.ismodule(obj): |
| 1045 | lineno = 0 |
| 1046 | |
| 1047 | # Find the line number for classes. |
| 1048 | # Note: this could be fooled if a class is defined multiple |
| 1049 | # times in a single file. |
| 1050 | if inspect.isclass(obj): |
| 1051 | if source_lines is None: |
| 1052 | return None |
| 1053 | pat = re.compile(r'^\s*class\s*%s\b' % |
| 1054 | getattr(obj, '__name__', '-')) |
| 1055 | for i, line in enumerate(source_lines): |
| 1056 | if pat.match(line): |
| 1057 | lineno = i |
| 1058 | break |
| 1059 | |
| 1060 | # Find the line number for functions & methods. |
| 1061 | if inspect.ismethod(obj): obj = obj.im_func |
| 1062 | if inspect.isfunction(obj): obj = obj.func_code |
| 1063 | if inspect.istraceback(obj): obj = obj.tb_frame |
| 1064 | if inspect.isframe(obj): obj = obj.f_code |
| 1065 | if inspect.iscode(obj): |
| 1066 | lineno = getattr(obj, 'co_firstlineno', None)-1 |
| 1067 | |
| 1068 | # Find the line number where the docstring starts. Assume |
| 1069 | # that it's the first line that begins with a quote mark. |
| 1070 | # Note: this could be fooled by a multiline function |
| 1071 | # signature, where a continuation line begins with a quote |
| 1072 | # mark. |
| 1073 | if lineno is not None: |
| 1074 | if source_lines is None: |
| 1075 | return lineno+1 |
| 1076 | pat = re.compile('(^|.*:)\s*\w*("|\')') |
| 1077 | for lineno in range(lineno, len(source_lines)): |
| 1078 | if pat.match(source_lines[lineno]): |
| 1079 | return lineno |
| 1080 | |
| 1081 | # We couldn't find the line number. |
| 1082 | return None |
| 1083 | |
| 1084 | ###################################################################### |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 1085 | ## 5. DocTest Runner |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1086 | ###################################################################### |
| 1087 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1088 | class DocTestRunner: |
| 1089 | """ |
| 1090 | A class used to run DocTest test cases, and accumulate statistics. |
| 1091 | The `run` method is used to process a single DocTest case. It |
| 1092 | returns a tuple `(f, t)`, where `t` is the number of test cases |
| 1093 | tried, and `f` is the number of test cases that failed. |
| 1094 | |
| 1095 | >>> tests = DocTestFinder().find(_TestClass) |
| 1096 | >>> runner = DocTestRunner(verbose=False) |
| 1097 | >>> for test in tests: |
| 1098 | ... print runner.run(test) |
| 1099 | (0, 2) |
| 1100 | (0, 1) |
| 1101 | (0, 2) |
| 1102 | (0, 2) |
| 1103 | |
| 1104 | The `summarize` method prints a summary of all the test cases that |
| 1105 | have been run by the runner, and returns an aggregated `(f, t)` |
| 1106 | tuple: |
| 1107 | |
| 1108 | >>> runner.summarize(verbose=1) |
| 1109 | 4 items passed all tests: |
| 1110 | 2 tests in _TestClass |
| 1111 | 2 tests in _TestClass.__init__ |
| 1112 | 2 tests in _TestClass.get |
| 1113 | 1 tests in _TestClass.square |
| 1114 | 7 tests in 4 items. |
| 1115 | 7 passed and 0 failed. |
| 1116 | Test passed. |
| 1117 | (0, 7) |
| 1118 | |
| 1119 | The aggregated number of tried examples and failed examples is |
| 1120 | also available via the `tries` and `failures` attributes: |
| 1121 | |
| 1122 | >>> runner.tries |
| 1123 | 7 |
| 1124 | >>> runner.failures |
| 1125 | 0 |
| 1126 | |
| 1127 | The comparison between expected outputs and actual outputs is done |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 1128 | by an `OutputChecker`. This comparison may be customized with a |
| 1129 | number of option flags; see the documentation for `testmod` for |
| 1130 | more information. If the option flags are insufficient, then the |
| 1131 | comparison may also be customized by passing a subclass of |
| 1132 | `OutputChecker` to the constructor. |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1133 | |
| 1134 | The test runner's display output can be controlled in two ways. |
| 1135 | First, an output function (`out) can be passed to |
| 1136 | `TestRunner.run`; this function will be called with strings that |
| 1137 | should be displayed. It defaults to `sys.stdout.write`. If |
| 1138 | capturing the output is not sufficient, then the display output |
| 1139 | can be also customized by subclassing DocTestRunner, and |
| 1140 | overriding the methods `report_start`, `report_success`, |
| 1141 | `report_unexpected_exception`, and `report_failure`. |
| 1142 | """ |
| 1143 | # This divider string is used to separate failure messages, and to |
| 1144 | # separate sections of the summary. |
| 1145 | DIVIDER = "*" * 70 |
| 1146 | |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 1147 | def __init__(self, checker=None, verbose=None, optionflags=0): |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1148 | """ |
| 1149 | Create a new test runner. |
| 1150 | |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 1151 | Optional keyword arg `checker` is the `OutputChecker` that |
| 1152 | should be used to compare the expected outputs and actual |
| 1153 | outputs of doctest examples. |
| 1154 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1155 | Optional keyword arg 'verbose' prints lots of stuff if true, |
| 1156 | only failures if false; by default, it's true iff '-v' is in |
| 1157 | sys.argv. |
| 1158 | |
| 1159 | Optional argument `optionflags` can be used to control how the |
| 1160 | test runner compares expected output to actual output, and how |
| 1161 | it displays failures. See the documentation for `testmod` for |
| 1162 | more information. |
| 1163 | """ |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 1164 | self._checker = checker or OutputChecker() |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1165 | if verbose is None: |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1166 | verbose = '-v' in sys.argv |
| 1167 | self._verbose = verbose |
Tim Peters | 6ebe61f | 2003-06-27 20:48:05 +0000 | [diff] [blame] | 1168 | self.optionflags = optionflags |
| 1169 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1170 | # Keep track of the examples we've run. |
| 1171 | self.tries = 0 |
| 1172 | self.failures = 0 |
| 1173 | self._name2ft = {} |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1174 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1175 | # Create a fake output target for capturing doctest output. |
| 1176 | self._fakeout = _SpoofOut() |
Tim Peters | 4fd9e2f | 2001-08-18 00:05:50 +0000 | [diff] [blame] | 1177 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1178 | #///////////////////////////////////////////////////////////////// |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1179 | # Reporting methods |
| 1180 | #///////////////////////////////////////////////////////////////// |
Tim Peters | 17111f3 | 2001-10-03 04:08:26 +0000 | [diff] [blame] | 1181 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1182 | def report_start(self, out, test, example): |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1183 | """ |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1184 | Report that the test runner is about to process the given |
| 1185 | example. (Only displays a message if verbose=True) |
| 1186 | """ |
| 1187 | if self._verbose: |
| 1188 | out(_tag_msg("Trying", example.source) + |
| 1189 | _tag_msg("Expecting", example.want or "nothing")) |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1190 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1191 | def report_success(self, out, test, example, got): |
| 1192 | """ |
| 1193 | Report that the given example ran successfully. (Only |
| 1194 | displays a message if verbose=True) |
| 1195 | """ |
| 1196 | if self._verbose: |
| 1197 | out("ok\n") |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1198 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1199 | def report_failure(self, out, test, example, got): |
| 1200 | """ |
| 1201 | Report that the given example failed. |
| 1202 | """ |
| 1203 | # Print an error message. |
Edward Loper | 8e4a34b | 2004-08-12 02:34:27 +0000 | [diff] [blame] | 1204 | out(self._failure_header(test, example) + |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 1205 | self._checker.output_difference(example.want, got, |
| 1206 | self.optionflags)) |
Tim Peters | 7402f79 | 2001-10-02 03:53:41 +0000 | [diff] [blame] | 1207 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1208 | def report_unexpected_exception(self, out, test, example, exc_info): |
| 1209 | """ |
| 1210 | Report that the given example raised an unexpected exception. |
| 1211 | """ |
Edward Loper | 8e4a34b | 2004-08-12 02:34:27 +0000 | [diff] [blame] | 1212 | out(self._failure_header(test, example) + |
| 1213 | _tag_msg("Exception raised", _exception_traceback(exc_info))) |
Tim Peters | 7402f79 | 2001-10-02 03:53:41 +0000 | [diff] [blame] | 1214 | |
Edward Loper | 8e4a34b | 2004-08-12 02:34:27 +0000 | [diff] [blame] | 1215 | def _failure_header(self, test, example): |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1216 | s = (self.DIVIDER + "\n" + |
| 1217 | _tag_msg("Failure in example", example.source)) |
| 1218 | if test.filename is None: |
| 1219 | # [XX] I'm not putting +1 here, to give the same output |
| 1220 | # as the old version. But I think it *should* go here. |
| 1221 | return s + ("from line #%s of %s\n" % |
| 1222 | (example.lineno, test.name)) |
| 1223 | elif test.lineno is None: |
| 1224 | return s + ("from line #%s of %s in %s\n" % |
| 1225 | (example.lineno+1, test.name, test.filename)) |
| 1226 | else: |
| 1227 | lineno = test.lineno+example.lineno+1 |
| 1228 | return s + ("from line #%s of %s (%s)\n" % |
| 1229 | (lineno, test.filename, test.name)) |
Tim Peters | 7402f79 | 2001-10-02 03:53:41 +0000 | [diff] [blame] | 1230 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1231 | #///////////////////////////////////////////////////////////////// |
| 1232 | # DocTest Running |
| 1233 | #///////////////////////////////////////////////////////////////// |
Tim Peters | 7402f79 | 2001-10-02 03:53:41 +0000 | [diff] [blame] | 1234 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1235 | # A regular expression for handling `want` strings that contain |
Tim Peters | 41a65ea | 2004-08-13 03:55:05 +0000 | [diff] [blame] | 1236 | # expected exceptions. It divides `want` into three pieces: |
| 1237 | # - the pre-exception output (`want`) |
| 1238 | # - the traceback header line (`hdr`) |
| 1239 | # - the exception message (`msg`), as generated by |
| 1240 | # traceback.format_exception_only() |
| 1241 | # `msg` may have multiple lines. We assume/require that the |
| 1242 | # exception message is the first non-indented line starting with a word |
| 1243 | # character following the traceback header line. |
| 1244 | _EXCEPTION_RE = re.compile(r""" |
| 1245 | (?P<want> .*?) # suck up everything until traceback header |
| 1246 | # Grab the traceback header. Different versions of Python have |
| 1247 | # said different things on the first traceback line. |
| 1248 | ^(?P<hdr> Traceback\ \( |
| 1249 | (?: most\ recent\ call\ last |
| 1250 | | innermost\ last |
| 1251 | ) \) : |
| 1252 | ) |
| 1253 | \s* $ # toss trailing whitespace on traceback header |
| 1254 | .*? # don't blink: absorb stuff until a line *starts* with \w |
| 1255 | ^ (?P<msg> \w+ .*) |
| 1256 | """, re.VERBOSE | re.MULTILINE | re.DOTALL) |
Tim Peters | 7402f79 | 2001-10-02 03:53:41 +0000 | [diff] [blame] | 1257 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1258 | def __run(self, test, compileflags, out): |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1259 | """ |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1260 | Run the examples in `test`. Write the outcome of each example |
| 1261 | with one of the `DocTestRunner.report_*` methods, using the |
| 1262 | writer function `out`. `compileflags` is the set of compiler |
| 1263 | flags that should be used to execute examples. Return a tuple |
| 1264 | `(f, t)`, where `t` is the number of examples tried, and `f` |
| 1265 | is the number of examples that failed. The examples are run |
| 1266 | in the namespace `test.globs`. |
| 1267 | """ |
| 1268 | # Keep track of the number of failures and tries. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1269 | failures = tries = 0 |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1270 | |
| 1271 | # Save the option flags (since option directives can be used |
| 1272 | # to modify them). |
| 1273 | original_optionflags = self.optionflags |
| 1274 | |
| 1275 | # Process each example. |
| 1276 | for example in test.examples: |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1277 | # Merge in the example's options. |
| 1278 | self.optionflags = original_optionflags |
| 1279 | if example.options: |
| 1280 | for (optionflag, val) in example.options.items(): |
| 1281 | if val: |
| 1282 | self.optionflags |= optionflag |
| 1283 | else: |
| 1284 | self.optionflags &= ~optionflag |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1285 | |
| 1286 | # Record that we started this example. |
| 1287 | tries += 1 |
| 1288 | self.report_start(out, test, example) |
| 1289 | |
| 1290 | # Run the example in the given context (globs), and record |
| 1291 | # any exception that gets raised. (But don't intercept |
| 1292 | # keyboard interrupts.) |
| 1293 | try: |
Tim Peters | 208ca70 | 2004-08-09 04:12:36 +0000 | [diff] [blame] | 1294 | # Don't blink! This is where the user's code gets run. |
Tim Peters | bb43147 | 2004-08-09 03:51:46 +0000 | [diff] [blame] | 1295 | exec compile(example.source, "<string>", "single", |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1296 | compileflags, 1) in test.globs |
| 1297 | exception = None |
| 1298 | except KeyboardInterrupt: |
| 1299 | raise |
| 1300 | except: |
| 1301 | exception = sys.exc_info() |
| 1302 | |
Tim Peters | 208ca70 | 2004-08-09 04:12:36 +0000 | [diff] [blame] | 1303 | got = self._fakeout.getvalue() # the actual output |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1304 | self._fakeout.truncate(0) |
| 1305 | |
| 1306 | # If the example executed without raising any exceptions, |
| 1307 | # then verify its output and report its outcome. |
| 1308 | if exception is None: |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 1309 | if self._checker.check_output(example.want, got, |
| 1310 | self.optionflags): |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1311 | self.report_success(out, test, example, got) |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1312 | else: |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1313 | self.report_failure(out, test, example, got) |
| 1314 | failures += 1 |
| 1315 | |
| 1316 | # If the example raised an exception, then check if it was |
| 1317 | # expected. |
| 1318 | else: |
| 1319 | exc_info = sys.exc_info() |
| 1320 | exc_msg = traceback.format_exception_only(*exc_info[:2])[-1] |
| 1321 | |
| 1322 | # Search the `want` string for an exception. If we don't |
| 1323 | # find one, then report an unexpected exception. |
| 1324 | m = self._EXCEPTION_RE.match(example.want) |
| 1325 | if m is None: |
| 1326 | self.report_unexpected_exception(out, test, example, |
| 1327 | exc_info) |
| 1328 | failures += 1 |
| 1329 | else: |
Tim Peters | 41a65ea | 2004-08-13 03:55:05 +0000 | [diff] [blame] | 1330 | e_want, e_msg = m.group('want', 'msg') |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1331 | # The test passes iff the pre-exception output and |
| 1332 | # the exception description match the values given |
| 1333 | # in `want`. |
Tim Peters | 41a65ea | 2004-08-13 03:55:05 +0000 | [diff] [blame] | 1334 | if (self._checker.check_output(e_want, got, |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 1335 | self.optionflags) and |
Tim Peters | 41a65ea | 2004-08-13 03:55:05 +0000 | [diff] [blame] | 1336 | self._checker.check_output(e_msg, exc_msg, |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 1337 | self.optionflags)): |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1338 | self.report_success(out, test, example, |
Tim Peters | 41a65ea | 2004-08-13 03:55:05 +0000 | [diff] [blame] | 1339 | got + _exception_traceback(exc_info)) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1340 | else: |
| 1341 | self.report_failure(out, test, example, |
Tim Peters | 41a65ea | 2004-08-13 03:55:05 +0000 | [diff] [blame] | 1342 | got + _exception_traceback(exc_info)) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1343 | failures += 1 |
| 1344 | |
| 1345 | # Restore the option flags (in case they were modified) |
| 1346 | self.optionflags = original_optionflags |
| 1347 | |
| 1348 | # Record and return the number of failures and tries. |
| 1349 | self.__record_outcome(test, failures, tries) |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1350 | return failures, tries |
| 1351 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1352 | def __record_outcome(self, test, f, t): |
| 1353 | """ |
| 1354 | Record the fact that the given DocTest (`test`) generated `f` |
| 1355 | failures out of `t` tried examples. |
| 1356 | """ |
| 1357 | f2, t2 = self._name2ft.get(test.name, (0,0)) |
| 1358 | self._name2ft[test.name] = (f+f2, t+t2) |
| 1359 | self.failures += f |
| 1360 | self.tries += t |
| 1361 | |
| 1362 | def run(self, test, compileflags=None, out=None, clear_globs=True): |
| 1363 | """ |
| 1364 | Run the examples in `test`, and display the results using the |
| 1365 | writer function `out`. |
| 1366 | |
| 1367 | The examples are run in the namespace `test.globs`. If |
| 1368 | `clear_globs` is true (the default), then this namespace will |
| 1369 | be cleared after the test runs, to help with garbage |
| 1370 | collection. If you would like to examine the namespace after |
| 1371 | the test completes, then use `clear_globs=False`. |
| 1372 | |
| 1373 | `compileflags` gives the set of flags that should be used by |
| 1374 | the Python compiler when running the examples. If not |
| 1375 | specified, then it will default to the set of future-import |
| 1376 | flags that apply to `globs`. |
| 1377 | |
| 1378 | The output of each example is checked using |
| 1379 | `DocTestRunner.check_output`, and the results are formatted by |
| 1380 | the `DocTestRunner.report_*` methods. |
| 1381 | """ |
| 1382 | if compileflags is None: |
| 1383 | compileflags = _extract_future_flags(test.globs) |
Jim Fulton | 356fd19 | 2004-08-09 11:34:47 +0000 | [diff] [blame] | 1384 | |
Tim Peters | 6c542b7 | 2004-08-09 16:43:36 +0000 | [diff] [blame] | 1385 | save_stdout = sys.stdout |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1386 | if out is None: |
Tim Peters | 6c542b7 | 2004-08-09 16:43:36 +0000 | [diff] [blame] | 1387 | out = save_stdout.write |
| 1388 | sys.stdout = self._fakeout |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1389 | |
Tim Peters | 6c542b7 | 2004-08-09 16:43:36 +0000 | [diff] [blame] | 1390 | # Patch pdb.set_trace to restore sys.stdout, so that interactive |
| 1391 | # debugging output is visible (not still redirected to self._fakeout). |
| 1392 | # Note that we run "the real" pdb.set_trace (captured at doctest |
| 1393 | # import time) in our replacement. Because the current run() may |
| 1394 | # run another doctest (and so on), the current pdb.set_trace may be |
| 1395 | # our set_trace function, which changes sys.stdout. If we called |
| 1396 | # a chain of those, we wouldn't be left with the save_stdout |
| 1397 | # *this* run() invocation wants. |
Jim Fulton | 356fd19 | 2004-08-09 11:34:47 +0000 | [diff] [blame] | 1398 | def set_trace(): |
Tim Peters | 6c542b7 | 2004-08-09 16:43:36 +0000 | [diff] [blame] | 1399 | sys.stdout = save_stdout |
Jim Fulton | 356fd19 | 2004-08-09 11:34:47 +0000 | [diff] [blame] | 1400 | real_pdb_set_trace() |
| 1401 | |
Tim Peters | 6c542b7 | 2004-08-09 16:43:36 +0000 | [diff] [blame] | 1402 | save_set_trace = pdb.set_trace |
| 1403 | pdb.set_trace = set_trace |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1404 | try: |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1405 | return self.__run(test, compileflags, out) |
| 1406 | finally: |
Tim Peters | 6c542b7 | 2004-08-09 16:43:36 +0000 | [diff] [blame] | 1407 | sys.stdout = save_stdout |
| 1408 | pdb.set_trace = save_set_trace |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1409 | if clear_globs: |
| 1410 | test.globs.clear() |
| 1411 | |
| 1412 | #///////////////////////////////////////////////////////////////// |
| 1413 | # Summarization |
| 1414 | #///////////////////////////////////////////////////////////////// |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1415 | def summarize(self, verbose=None): |
| 1416 | """ |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1417 | Print a summary of all the test cases that have been run by |
| 1418 | this DocTestRunner, and return a tuple `(f, t)`, where `f` is |
| 1419 | the total number of failed examples, and `t` is the total |
| 1420 | number of tried examples. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1421 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1422 | The optional `verbose` argument controls how detailed the |
| 1423 | summary is. If the verbosity is not specified, then the |
| 1424 | DocTestRunner's verbosity is used. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1425 | """ |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1426 | if verbose is None: |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1427 | verbose = self._verbose |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1428 | notests = [] |
| 1429 | passed = [] |
| 1430 | failed = [] |
| 1431 | totalt = totalf = 0 |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1432 | for x in self._name2ft.items(): |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1433 | name, (f, t) = x |
| 1434 | assert f <= t |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1435 | totalt += t |
| 1436 | totalf += f |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1437 | if t == 0: |
| 1438 | notests.append(name) |
| 1439 | elif f == 0: |
| 1440 | passed.append( (name, t) ) |
| 1441 | else: |
| 1442 | failed.append(x) |
| 1443 | if verbose: |
| 1444 | if notests: |
| 1445 | print len(notests), "items had no tests:" |
| 1446 | notests.sort() |
| 1447 | for thing in notests: |
| 1448 | print " ", thing |
| 1449 | if passed: |
| 1450 | print len(passed), "items passed all tests:" |
| 1451 | passed.sort() |
| 1452 | for thing, count in passed: |
| 1453 | print " %3d tests in %s" % (count, thing) |
| 1454 | if failed: |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1455 | print self.DIVIDER |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1456 | print len(failed), "items had failures:" |
| 1457 | failed.sort() |
| 1458 | for thing, (f, t) in failed: |
| 1459 | print " %3d of %3d in %s" % (f, t, thing) |
| 1460 | if verbose: |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1461 | print totalt, "tests in", len(self._name2ft), "items." |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1462 | print totalt - totalf, "passed and", totalf, "failed." |
| 1463 | if totalf: |
| 1464 | print "***Test Failed***", totalf, "failures." |
| 1465 | elif verbose: |
| 1466 | print "Test passed." |
| 1467 | return totalf, totalt |
| 1468 | |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 1469 | class OutputChecker: |
| 1470 | """ |
| 1471 | A class used to check the whether the actual output from a doctest |
| 1472 | example matches the expected output. `OutputChecker` defines two |
| 1473 | methods: `check_output`, which compares a given pair of outputs, |
| 1474 | and returns true if they match; and `output_difference`, which |
| 1475 | returns a string describing the differences between two outputs. |
| 1476 | """ |
| 1477 | def check_output(self, want, got, optionflags): |
| 1478 | """ |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1479 | Return True iff the actual output from an example (`got`) |
| 1480 | matches the expected output (`want`). These strings are |
| 1481 | always considered to match if they are identical; but |
| 1482 | depending on what option flags the test runner is using, |
| 1483 | several non-exact match types are also possible. See the |
| 1484 | documentation for `TestRunner` for more information about |
| 1485 | option flags. |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 1486 | """ |
| 1487 | # Handle the common case first, for efficiency: |
| 1488 | # if they're string-identical, always return true. |
| 1489 | if got == want: |
| 1490 | return True |
| 1491 | |
| 1492 | # The values True and False replaced 1 and 0 as the return |
| 1493 | # value for boolean comparisons in Python 2.3. |
| 1494 | if not (optionflags & DONT_ACCEPT_TRUE_FOR_1): |
| 1495 | if (got,want) == ("True\n", "1\n"): |
| 1496 | return True |
| 1497 | if (got,want) == ("False\n", "0\n"): |
| 1498 | return True |
| 1499 | |
| 1500 | # <BLANKLINE> can be used as a special sequence to signify a |
| 1501 | # blank line, unless the DONT_ACCEPT_BLANKLINE flag is used. |
| 1502 | if not (optionflags & DONT_ACCEPT_BLANKLINE): |
| 1503 | # Replace <BLANKLINE> in want with a blank line. |
| 1504 | want = re.sub('(?m)^%s\s*?$' % re.escape(BLANKLINE_MARKER), |
| 1505 | '', want) |
| 1506 | # If a line in got contains only spaces, then remove the |
| 1507 | # spaces. |
| 1508 | got = re.sub('(?m)^\s*?$', '', got) |
| 1509 | if got == want: |
| 1510 | return True |
| 1511 | |
| 1512 | # This flag causes doctest to ignore any differences in the |
| 1513 | # contents of whitespace strings. Note that this can be used |
| 1514 | # in conjunction with the ELLISPIS flag. |
Tim Peters | 1cf3aa6 | 2004-08-19 06:49:33 +0000 | [diff] [blame] | 1515 | if optionflags & NORMALIZE_WHITESPACE: |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 1516 | got = ' '.join(got.split()) |
| 1517 | want = ' '.join(want.split()) |
| 1518 | if got == want: |
| 1519 | return True |
| 1520 | |
| 1521 | # The ELLIPSIS flag says to let the sequence "..." in `want` |
Tim Peters | 26b3ebb | 2004-08-19 08:10:08 +0000 | [diff] [blame^] | 1522 | # match any substring in `got`. |
Tim Peters | 1cf3aa6 | 2004-08-19 06:49:33 +0000 | [diff] [blame] | 1523 | if optionflags & ELLIPSIS: |
Tim Peters | 26b3ebb | 2004-08-19 08:10:08 +0000 | [diff] [blame^] | 1524 | if ellipsis_match(want, got): |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 1525 | return True |
| 1526 | |
| 1527 | # We didn't find any match; return false. |
| 1528 | return False |
| 1529 | |
| 1530 | def output_difference(self, want, got, optionflags): |
| 1531 | """ |
| 1532 | Return a string describing the differences between the |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1533 | expected output for an example (`want`) and the actual output |
| 1534 | (`got`). `optionflags` is the set of option flags used to |
| 1535 | compare `want` and `got`. `indent` is the indentation of the |
| 1536 | original example. |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 1537 | """ |
Edward Loper | 68ba9a6 | 2004-08-12 02:43:49 +0000 | [diff] [blame] | 1538 | # If <BLANKLINE>s are being used, then replace blank lines |
| 1539 | # with <BLANKLINE> in the actual output string. |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 1540 | if not (optionflags & DONT_ACCEPT_BLANKLINE): |
Edward Loper | 68ba9a6 | 2004-08-12 02:43:49 +0000 | [diff] [blame] | 1541 | got = re.sub('(?m)^[ ]*(?=\n)', BLANKLINE_MARKER, got) |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 1542 | |
| 1543 | # Check if we should use diff. Don't use diff if the actual |
| 1544 | # or expected outputs are too short, or if the expected output |
| 1545 | # contains an ellipsis marker. |
| 1546 | if ((optionflags & (UNIFIED_DIFF | CONTEXT_DIFF)) and |
| 1547 | want.count('\n') > 2 and got.count('\n') > 2 and |
| 1548 | not (optionflags & ELLIPSIS and '...' in want)): |
| 1549 | # Split want & got into lines. |
| 1550 | want_lines = [l+'\n' for l in want.split('\n')] |
| 1551 | got_lines = [l+'\n' for l in got.split('\n')] |
| 1552 | # Use difflib to find their differences. |
| 1553 | if optionflags & UNIFIED_DIFF: |
| 1554 | diff = difflib.unified_diff(want_lines, got_lines, n=2, |
| 1555 | fromfile='Expected', tofile='Got') |
| 1556 | kind = 'unified' |
| 1557 | elif optionflags & CONTEXT_DIFF: |
| 1558 | diff = difflib.context_diff(want_lines, got_lines, n=2, |
| 1559 | fromfile='Expected', tofile='Got') |
| 1560 | kind = 'context' |
| 1561 | else: |
| 1562 | assert 0, 'Bad diff option' |
| 1563 | # Remove trailing whitespace on diff output. |
| 1564 | diff = [line.rstrip() + '\n' for line in diff] |
| 1565 | return _tag_msg("Differences (" + kind + " diff)", |
| 1566 | ''.join(diff)) |
| 1567 | |
| 1568 | # If we're not using diff, then simply list the expected |
| 1569 | # output followed by the actual output. |
| 1570 | return (_tag_msg("Expected", want or "Nothing") + |
| 1571 | _tag_msg("Got", got)) |
| 1572 | |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1573 | class DocTestFailure(Exception): |
| 1574 | """A DocTest example has failed in debugging mode. |
| 1575 | |
| 1576 | The exception instance has variables: |
| 1577 | |
| 1578 | - test: the DocTest object being run |
| 1579 | |
| 1580 | - excample: the Example object that failed |
| 1581 | |
| 1582 | - got: the actual output |
| 1583 | """ |
| 1584 | def __init__(self, test, example, got): |
| 1585 | self.test = test |
| 1586 | self.example = example |
| 1587 | self.got = got |
| 1588 | |
| 1589 | def __str__(self): |
| 1590 | return str(self.test) |
| 1591 | |
| 1592 | class UnexpectedException(Exception): |
| 1593 | """A DocTest example has encountered an unexpected exception |
| 1594 | |
| 1595 | The exception instance has variables: |
| 1596 | |
| 1597 | - test: the DocTest object being run |
| 1598 | |
| 1599 | - excample: the Example object that failed |
| 1600 | |
| 1601 | - exc_info: the exception info |
| 1602 | """ |
| 1603 | def __init__(self, test, example, exc_info): |
| 1604 | self.test = test |
| 1605 | self.example = example |
| 1606 | self.exc_info = exc_info |
| 1607 | |
| 1608 | def __str__(self): |
| 1609 | return str(self.test) |
Tim Peters | d1b7827 | 2004-08-07 06:03:09 +0000 | [diff] [blame] | 1610 | |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1611 | class DebugRunner(DocTestRunner): |
| 1612 | r"""Run doc tests but raise an exception as soon as there is a failure. |
| 1613 | |
| 1614 | If an unexpected exception occurs, an UnexpectedException is raised. |
| 1615 | It contains the test, the example, and the original exception: |
| 1616 | |
| 1617 | >>> runner = DebugRunner(verbose=False) |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 1618 | >>> test = DocTestParser().get_doctest('>>> raise KeyError\n42', |
| 1619 | ... {}, 'foo', 'foo.py', 0) |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1620 | >>> try: |
| 1621 | ... runner.run(test) |
| 1622 | ... except UnexpectedException, failure: |
| 1623 | ... pass |
| 1624 | |
| 1625 | >>> failure.test is test |
| 1626 | True |
| 1627 | |
| 1628 | >>> failure.example.want |
| 1629 | '42\n' |
| 1630 | |
| 1631 | >>> exc_info = failure.exc_info |
| 1632 | >>> raise exc_info[0], exc_info[1], exc_info[2] |
| 1633 | Traceback (most recent call last): |
| 1634 | ... |
| 1635 | KeyError |
| 1636 | |
| 1637 | We wrap the original exception to give the calling application |
| 1638 | access to the test and example information. |
| 1639 | |
| 1640 | If the output doesn't match, then a DocTestFailure is raised: |
| 1641 | |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 1642 | >>> test = DocTestParser().get_doctest(''' |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1643 | ... >>> x = 1 |
| 1644 | ... >>> x |
| 1645 | ... 2 |
| 1646 | ... ''', {}, 'foo', 'foo.py', 0) |
| 1647 | |
| 1648 | >>> try: |
| 1649 | ... runner.run(test) |
| 1650 | ... except DocTestFailure, failure: |
| 1651 | ... pass |
| 1652 | |
| 1653 | DocTestFailure objects provide access to the test: |
| 1654 | |
| 1655 | >>> failure.test is test |
| 1656 | True |
| 1657 | |
| 1658 | As well as to the example: |
| 1659 | |
| 1660 | >>> failure.example.want |
| 1661 | '2\n' |
| 1662 | |
| 1663 | and the actual output: |
| 1664 | |
| 1665 | >>> failure.got |
| 1666 | '1\n' |
| 1667 | |
| 1668 | If a failure or error occurs, the globals are left intact: |
| 1669 | |
| 1670 | >>> del test.globs['__builtins__'] |
| 1671 | >>> test.globs |
| 1672 | {'x': 1} |
| 1673 | |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 1674 | >>> test = DocTestParser().get_doctest(''' |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1675 | ... >>> x = 2 |
| 1676 | ... >>> raise KeyError |
| 1677 | ... ''', {}, 'foo', 'foo.py', 0) |
| 1678 | |
| 1679 | >>> runner.run(test) |
| 1680 | Traceback (most recent call last): |
| 1681 | ... |
| 1682 | UnexpectedException: <DocTest foo from foo.py:0 (2 examples)> |
Tim Peters | d1b7827 | 2004-08-07 06:03:09 +0000 | [diff] [blame] | 1683 | |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1684 | >>> del test.globs['__builtins__'] |
| 1685 | >>> test.globs |
| 1686 | {'x': 2} |
| 1687 | |
| 1688 | But the globals are cleared if there is no error: |
| 1689 | |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 1690 | >>> test = DocTestParser().get_doctest(''' |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1691 | ... >>> x = 2 |
| 1692 | ... ''', {}, 'foo', 'foo.py', 0) |
| 1693 | |
| 1694 | >>> runner.run(test) |
| 1695 | (0, 1) |
| 1696 | |
| 1697 | >>> test.globs |
| 1698 | {} |
| 1699 | |
| 1700 | """ |
| 1701 | |
| 1702 | def run(self, test, compileflags=None, out=None, clear_globs=True): |
| 1703 | r = DocTestRunner.run(self, test, compileflags, out, False) |
| 1704 | if clear_globs: |
| 1705 | test.globs.clear() |
| 1706 | return r |
| 1707 | |
| 1708 | def report_unexpected_exception(self, out, test, example, exc_info): |
| 1709 | raise UnexpectedException(test, example, exc_info) |
| 1710 | |
| 1711 | def report_failure(self, out, test, example, got): |
| 1712 | raise DocTestFailure(test, example, got) |
| 1713 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1714 | ###################################################################### |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 1715 | ## 6. Test Functions |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1716 | ###################################################################### |
| 1717 | # These should be backwards compatible. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1718 | |
Martin v. Löwis | 4581cfa | 2002-11-22 08:23:09 +0000 | [diff] [blame] | 1719 | def testmod(m=None, name=None, globs=None, verbose=None, isprivate=None, |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1720 | report=True, optionflags=0, extraglobs=None, |
| 1721 | raise_on_error=False): |
Tim Peters | 6ebe61f | 2003-06-27 20:48:05 +0000 | [diff] [blame] | 1722 | """m=None, name=None, globs=None, verbose=None, isprivate=None, |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1723 | report=True, optionflags=0, extraglobs=None |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1724 | |
Martin v. Löwis | 4581cfa | 2002-11-22 08:23:09 +0000 | [diff] [blame] | 1725 | Test examples in docstrings in functions and classes reachable |
| 1726 | from module m (or the current module if m is not supplied), starting |
Raymond Hettinger | 71adf7e | 2003-07-16 19:25:22 +0000 | [diff] [blame] | 1727 | with m.__doc__. Unless isprivate is specified, private names |
| 1728 | are not skipped. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1729 | |
| 1730 | Also test examples reachable from dict m.__test__ if it exists and is |
Tim Peters | c2388a2 | 2004-08-10 01:41:28 +0000 | [diff] [blame] | 1731 | not None. m.__test__ maps names to functions, classes and strings; |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1732 | function and class docstrings are tested even if the name is private; |
| 1733 | strings are tested directly, as if they were docstrings. |
| 1734 | |
| 1735 | Return (#failures, #tests). |
| 1736 | |
| 1737 | See doctest.__doc__ for an overview. |
| 1738 | |
| 1739 | Optional keyword arg "name" gives the name of the module; by default |
| 1740 | use m.__name__. |
| 1741 | |
| 1742 | Optional keyword arg "globs" gives a dict to be used as the globals |
| 1743 | when executing examples; by default, use m.__dict__. A copy of this |
| 1744 | dict is actually used for each docstring, so that each docstring's |
| 1745 | examples start with a clean slate. |
| 1746 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1747 | Optional keyword arg "extraglobs" gives a dictionary that should be |
| 1748 | merged into the globals that are used to execute examples. By |
| 1749 | default, no extra globals are used. This is new in 2.4. |
| 1750 | |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1751 | Optional keyword arg "verbose" prints lots of stuff if true, prints |
| 1752 | only failures if false; by default, it's true iff "-v" is in sys.argv. |
| 1753 | |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1754 | Optional keyword arg "report" prints a summary at the end when true, |
| 1755 | else prints nothing at the end. In verbose mode, the summary is |
| 1756 | detailed, else very brief (in fact, empty if all tests passed). |
| 1757 | |
Tim Peters | 6ebe61f | 2003-06-27 20:48:05 +0000 | [diff] [blame] | 1758 | Optional keyword arg "optionflags" or's together module constants, |
| 1759 | and defaults to 0. This is new in 2.3. Possible values: |
| 1760 | |
| 1761 | DONT_ACCEPT_TRUE_FOR_1 |
| 1762 | By default, if an expected output block contains just "1", |
| 1763 | an actual output block containing just "True" is considered |
| 1764 | to be a match, and similarly for "0" versus "False". When |
| 1765 | DONT_ACCEPT_TRUE_FOR_1 is specified, neither substitution |
| 1766 | is allowed. |
| 1767 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1768 | DONT_ACCEPT_BLANKLINE |
| 1769 | By default, if an expected output block contains a line |
| 1770 | containing only the string "<BLANKLINE>", then that line |
| 1771 | will match a blank line in the actual output. When |
| 1772 | DONT_ACCEPT_BLANKLINE is specified, this substitution is |
| 1773 | not allowed. |
| 1774 | |
| 1775 | NORMALIZE_WHITESPACE |
| 1776 | When NORMALIZE_WHITESPACE is specified, all sequences of |
| 1777 | whitespace are treated as equal. I.e., any sequence of |
| 1778 | whitespace within the expected output will match any |
| 1779 | sequence of whitespace within the actual output. |
| 1780 | |
| 1781 | ELLIPSIS |
| 1782 | When ELLIPSIS is specified, then an ellipsis marker |
| 1783 | ("...") in the expected output can match any substring in |
| 1784 | the actual output. |
| 1785 | |
| 1786 | UNIFIED_DIFF |
| 1787 | When UNIFIED_DIFF is specified, failures that involve |
| 1788 | multi-line expected and actual outputs will be displayed |
| 1789 | using a unified diff. |
| 1790 | |
| 1791 | CONTEXT_DIFF |
| 1792 | When CONTEXT_DIFF is specified, failures that involve |
| 1793 | multi-line expected and actual outputs will be displayed |
| 1794 | using a context diff. |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1795 | |
| 1796 | Optional keyword arg "raise_on_error" raises an exception on the |
| 1797 | first unexpected exception or failure. This allows failures to be |
| 1798 | post-mortem debugged. |
| 1799 | |
Tim Peters | f727c6c | 2004-08-08 01:48:59 +0000 | [diff] [blame] | 1800 | Deprecated in Python 2.4: |
| 1801 | Optional keyword arg "isprivate" specifies a function used to |
| 1802 | determine whether a name is private. The default function is |
| 1803 | treat all functions as public. Optionally, "isprivate" can be |
| 1804 | set to doctest.is_private to skip over functions marked as private |
| 1805 | using the underscore naming convention; see its docs for details. |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1806 | """ |
| 1807 | |
| 1808 | """ [XX] This is no longer true: |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1809 | Advanced tomfoolery: testmod runs methods of a local instance of |
| 1810 | class doctest.Tester, then merges the results into (or creates) |
| 1811 | global Tester instance doctest.master. Methods of doctest.master |
| 1812 | can be called directly too, if you want to do something unusual. |
| 1813 | Passing report=0 to testmod is especially useful then, to delay |
| 1814 | displaying a summary. Invoke doctest.master.summarize(verbose) |
| 1815 | when you're done fiddling. |
| 1816 | """ |
Tim Peters | f727c6c | 2004-08-08 01:48:59 +0000 | [diff] [blame] | 1817 | if isprivate is not None: |
| 1818 | warnings.warn("the isprivate argument is deprecated; " |
| 1819 | "examine DocTestFinder.find() lists instead", |
| 1820 | DeprecationWarning) |
| 1821 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1822 | # If no module was given, then use __main__. |
Martin v. Löwis | 4581cfa | 2002-11-22 08:23:09 +0000 | [diff] [blame] | 1823 | if m is None: |
Martin v. Löwis | 4581cfa | 2002-11-22 08:23:09 +0000 | [diff] [blame] | 1824 | # DWA - m will still be None if this wasn't invoked from the command |
| 1825 | # line, in which case the following TypeError is about as good an error |
| 1826 | # as we should expect |
| 1827 | m = sys.modules.get('__main__') |
| 1828 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1829 | # Check that we were actually given a module. |
| 1830 | if not inspect.ismodule(m): |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 1831 | raise TypeError("testmod: module required; %r" % (m,)) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1832 | |
| 1833 | # If no name was given, then use the module's name. |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1834 | if name is None: |
| 1835 | name = m.__name__ |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1836 | |
| 1837 | # Find, parse, and run all tests in the given module. |
Tim Peters | f727c6c | 2004-08-08 01:48:59 +0000 | [diff] [blame] | 1838 | finder = DocTestFinder(_namefilter=isprivate) |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1839 | |
| 1840 | if raise_on_error: |
| 1841 | runner = DebugRunner(verbose=verbose, optionflags=optionflags) |
| 1842 | else: |
| 1843 | runner = DocTestRunner(verbose=verbose, optionflags=optionflags) |
| 1844 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1845 | for test in finder.find(m, name, globs=globs, extraglobs=extraglobs): |
| 1846 | runner.run(test) |
| 1847 | |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1848 | if report: |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1849 | runner.summarize() |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 1850 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1851 | return runner.failures, runner.tries |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 1852 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1853 | def run_docstring_examples(f, globs, verbose=False, name="NoName", |
| 1854 | compileflags=None, optionflags=0): |
| 1855 | """ |
| 1856 | Test examples in the given object's docstring (`f`), using `globs` |
| 1857 | as globals. Optional argument `name` is used in failure messages. |
| 1858 | If the optional argument `verbose` is true, then generate output |
| 1859 | even if there are no failures. |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 1860 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1861 | `compileflags` gives the set of flags that should be used by the |
| 1862 | Python compiler when running the examples. If not specified, then |
| 1863 | it will default to the set of future-import flags that apply to |
| 1864 | `globs`. |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 1865 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1866 | Optional keyword arg `optionflags` specifies options for the |
| 1867 | testing and output. See the documentation for `testmod` for more |
| 1868 | information. |
| 1869 | """ |
| 1870 | # Find, parse, and run all tests in the given module. |
| 1871 | finder = DocTestFinder(verbose=verbose, recurse=False) |
| 1872 | runner = DocTestRunner(verbose=verbose, optionflags=optionflags) |
| 1873 | for test in finder.find(f, name, globs=globs): |
| 1874 | runner.run(test, compileflags=compileflags) |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 1875 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1876 | ###################################################################### |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 1877 | ## 7. Tester |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1878 | ###################################################################### |
| 1879 | # This is provided only for backwards compatibility. It's not |
| 1880 | # actually used in any way. |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 1881 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1882 | class Tester: |
| 1883 | def __init__(self, mod=None, globs=None, verbose=None, |
| 1884 | isprivate=None, optionflags=0): |
Tim Peters | 3ddd60a | 2004-08-08 02:43:33 +0000 | [diff] [blame] | 1885 | |
| 1886 | warnings.warn("class Tester is deprecated; " |
| 1887 | "use class doctest.DocTestRunner instead", |
| 1888 | DeprecationWarning, stacklevel=2) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1889 | if mod is None and globs is None: |
| 1890 | raise TypeError("Tester.__init__: must specify mod or globs") |
| 1891 | if mod is not None and not _ismodule(mod): |
| 1892 | raise TypeError("Tester.__init__: mod must be a module; %r" % |
| 1893 | (mod,)) |
| 1894 | if globs is None: |
| 1895 | globs = mod.__dict__ |
| 1896 | self.globs = globs |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 1897 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1898 | self.verbose = verbose |
| 1899 | self.isprivate = isprivate |
| 1900 | self.optionflags = optionflags |
Tim Peters | f727c6c | 2004-08-08 01:48:59 +0000 | [diff] [blame] | 1901 | self.testfinder = DocTestFinder(_namefilter=isprivate) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1902 | self.testrunner = DocTestRunner(verbose=verbose, |
| 1903 | optionflags=optionflags) |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 1904 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1905 | def runstring(self, s, name): |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 1906 | test = DocTestParser().get_doctest(s, self.globs, name, None, None) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1907 | if self.verbose: |
| 1908 | print "Running string", name |
| 1909 | (f,t) = self.testrunner.run(test) |
| 1910 | if self.verbose: |
| 1911 | print f, "of", t, "examples failed in string", name |
| 1912 | return (f,t) |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 1913 | |
Tim Peters | f3f5747 | 2004-08-08 06:11:48 +0000 | [diff] [blame] | 1914 | def rundoc(self, object, name=None, module=None): |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1915 | f = t = 0 |
| 1916 | tests = self.testfinder.find(object, name, module=module, |
Tim Peters | f3f5747 | 2004-08-08 06:11:48 +0000 | [diff] [blame] | 1917 | globs=self.globs) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1918 | for test in tests: |
| 1919 | (f2, t2) = self.testrunner.run(test) |
| 1920 | (f,t) = (f+f2, t+t2) |
| 1921 | return (f,t) |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 1922 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1923 | def rundict(self, d, name, module=None): |
| 1924 | import new |
| 1925 | m = new.module(name) |
| 1926 | m.__dict__.update(d) |
Tim Peters | f3f5747 | 2004-08-08 06:11:48 +0000 | [diff] [blame] | 1927 | if module is None: |
| 1928 | module = False |
| 1929 | return self.rundoc(m, name, module) |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 1930 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1931 | def run__test__(self, d, name): |
| 1932 | import new |
| 1933 | m = new.module(name) |
| 1934 | m.__test__ = d |
| 1935 | return self.rundoc(m, name, module) |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 1936 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1937 | def summarize(self, verbose=None): |
| 1938 | return self.testrunner.summarize(verbose) |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 1939 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1940 | def merge(self, other): |
| 1941 | d = self.testrunner._name2ft |
| 1942 | for name, (f, t) in other.testrunner._name2ft.items(): |
| 1943 | if name in d: |
| 1944 | print "*** Tester.merge: '" + name + "' in both" \ |
| 1945 | " testers; summing outcomes." |
| 1946 | f2, t2 = d[name] |
| 1947 | f = f + f2 |
| 1948 | t = t + t2 |
| 1949 | d[name] = f, t |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 1950 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1951 | ###################################################################### |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 1952 | ## 8. Unittest Support |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1953 | ###################################################################### |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 1954 | |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1955 | class DocTestCase(unittest.TestCase): |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 1956 | |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 1957 | def __init__(self, test, optionflags=0, setUp=None, tearDown=None, |
| 1958 | checker=None): |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 1959 | unittest.TestCase.__init__(self) |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1960 | self._dt_optionflags = optionflags |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 1961 | self._dt_checker = checker |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1962 | self._dt_test = test |
| 1963 | self._dt_setUp = setUp |
| 1964 | self._dt_tearDown = tearDown |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 1965 | |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 1966 | def setUp(self): |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1967 | if self._dt_setUp is not None: |
| 1968 | self._dt_setUp() |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 1969 | |
| 1970 | def tearDown(self): |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1971 | if self._dt_tearDown is not None: |
| 1972 | self._dt_tearDown() |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 1973 | |
| 1974 | def runTest(self): |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1975 | test = self._dt_test |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 1976 | old = sys.stdout |
| 1977 | new = StringIO() |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 1978 | runner = DocTestRunner(optionflags=self._dt_optionflags, |
| 1979 | checker=self._dt_checker, verbose=False) |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1980 | |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 1981 | try: |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1982 | runner.DIVIDER = "-"*70 |
| 1983 | failures, tries = runner.run(test, out=new.write) |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 1984 | finally: |
| 1985 | sys.stdout = old |
| 1986 | |
| 1987 | if failures: |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1988 | raise self.failureException(self.format_failure(new.getvalue())) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1989 | |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1990 | def format_failure(self, err): |
| 1991 | test = self._dt_test |
| 1992 | if test.lineno is None: |
| 1993 | lineno = 'unknown line number' |
| 1994 | else: |
| 1995 | lineno = 'line %s' % test.lineno |
| 1996 | lname = '.'.join(test.name.split('.')[-1:]) |
| 1997 | return ('Failed doctest test for %s\n' |
| 1998 | ' File "%s", line %s, in %s\n\n%s' |
| 1999 | % (test.name, test.filename, lineno, lname, err) |
| 2000 | ) |
| 2001 | |
| 2002 | def debug(self): |
| 2003 | r"""Run the test case without results and without catching exceptions |
| 2004 | |
| 2005 | The unit test framework includes a debug method on test cases |
| 2006 | and test suites to support post-mortem debugging. The test code |
| 2007 | is run in such a way that errors are not caught. This way a |
| 2008 | caller can catch the errors and initiate post-mortem debugging. |
| 2009 | |
| 2010 | The DocTestCase provides a debug method that raises |
| 2011 | UnexpectedException errors if there is an unexepcted |
| 2012 | exception: |
| 2013 | |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 2014 | >>> test = DocTestParser().get_doctest('>>> raise KeyError\n42', |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2015 | ... {}, 'foo', 'foo.py', 0) |
| 2016 | >>> case = DocTestCase(test) |
| 2017 | >>> try: |
| 2018 | ... case.debug() |
| 2019 | ... except UnexpectedException, failure: |
| 2020 | ... pass |
| 2021 | |
| 2022 | The UnexpectedException contains the test, the example, and |
| 2023 | the original exception: |
| 2024 | |
| 2025 | >>> failure.test is test |
| 2026 | True |
| 2027 | |
| 2028 | >>> failure.example.want |
| 2029 | '42\n' |
| 2030 | |
| 2031 | >>> exc_info = failure.exc_info |
| 2032 | >>> raise exc_info[0], exc_info[1], exc_info[2] |
| 2033 | Traceback (most recent call last): |
| 2034 | ... |
| 2035 | KeyError |
| 2036 | |
| 2037 | If the output doesn't match, then a DocTestFailure is raised: |
| 2038 | |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 2039 | >>> test = DocTestParser().get_doctest(''' |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2040 | ... >>> x = 1 |
| 2041 | ... >>> x |
| 2042 | ... 2 |
| 2043 | ... ''', {}, 'foo', 'foo.py', 0) |
| 2044 | >>> case = DocTestCase(test) |
| 2045 | |
| 2046 | >>> try: |
| 2047 | ... case.debug() |
| 2048 | ... except DocTestFailure, failure: |
| 2049 | ... pass |
| 2050 | |
| 2051 | DocTestFailure objects provide access to the test: |
| 2052 | |
| 2053 | >>> failure.test is test |
| 2054 | True |
| 2055 | |
| 2056 | As well as to the example: |
| 2057 | |
| 2058 | >>> failure.example.want |
| 2059 | '2\n' |
| 2060 | |
| 2061 | and the actual output: |
| 2062 | |
| 2063 | >>> failure.got |
| 2064 | '1\n' |
| 2065 | |
| 2066 | """ |
| 2067 | |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 2068 | runner = DebugRunner(optionflags=self._dt_optionflags, |
| 2069 | checker=self._dt_checker, verbose=False) |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2070 | runner.run(self._dt_test, out=nooutput) |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2071 | |
| 2072 | def id(self): |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2073 | return self._dt_test.name |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2074 | |
| 2075 | def __repr__(self): |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2076 | name = self._dt_test.name.split('.') |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2077 | return "%s (%s)" % (name[-1], '.'.join(name[:-1])) |
| 2078 | |
| 2079 | __str__ = __repr__ |
| 2080 | |
| 2081 | def shortDescription(self): |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2082 | return "Doctest: " + self._dt_test.name |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2083 | |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2084 | def nooutput(*args): |
| 2085 | pass |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2086 | |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2087 | def DocTestSuite(module=None, globs=None, extraglobs=None, |
| 2088 | optionflags=0, test_finder=None, |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 2089 | setUp=lambda: None, tearDown=lambda: None, |
| 2090 | checker=None): |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2091 | """ |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2092 | Convert doctest tests for a mudule to a unittest test suite. |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2093 | |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2094 | This converts each documentation string in a module that |
| 2095 | contains doctest tests to a unittest test case. If any of the |
| 2096 | tests in a doc string fail, then the test case fails. An exception |
| 2097 | is raised showing the name of the file containing the test and a |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2098 | (sometimes approximate) line number. |
| 2099 | |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2100 | The `module` argument provides the module to be tested. The argument |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2101 | can be either a module or a module name. |
| 2102 | |
| 2103 | If no argument is given, the calling module is used. |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2104 | """ |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2105 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2106 | if test_finder is None: |
| 2107 | test_finder = DocTestFinder() |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2108 | |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2109 | module = _normalize_module(module) |
| 2110 | tests = test_finder.find(module, globs=globs, extraglobs=extraglobs) |
| 2111 | if globs is None: |
| 2112 | globs = module.__dict__ |
| 2113 | if not tests: # [XX] why do we want to do this? |
| 2114 | raise ValueError(module, "has no tests") |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 2115 | |
| 2116 | tests.sort() |
| 2117 | suite = unittest.TestSuite() |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2118 | for test in tests: |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2119 | if len(test.examples) == 0: |
| 2120 | continue |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2121 | if not test.filename: |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 2122 | filename = module.__file__ |
| 2123 | if filename.endswith(".pyc"): |
| 2124 | filename = filename[:-1] |
| 2125 | elif filename.endswith(".pyo"): |
| 2126 | filename = filename[:-1] |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2127 | test.filename = filename |
Edward Loper | 34fcb14 | 2004-08-09 02:45:41 +0000 | [diff] [blame] | 2128 | suite.addTest(DocTestCase(test, optionflags, setUp, tearDown, |
| 2129 | checker)) |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2130 | |
| 2131 | return suite |
| 2132 | |
| 2133 | class DocFileCase(DocTestCase): |
| 2134 | |
| 2135 | def id(self): |
| 2136 | return '_'.join(self._dt_test.name.split('.')) |
| 2137 | |
| 2138 | def __repr__(self): |
| 2139 | return self._dt_test.filename |
| 2140 | __str__ = __repr__ |
| 2141 | |
| 2142 | def format_failure(self, err): |
| 2143 | return ('Failed doctest test for %s\n File "%s", line 0\n\n%s' |
| 2144 | % (self._dt_test.name, self._dt_test.filename, err) |
| 2145 | ) |
| 2146 | |
| 2147 | def DocFileTest(path, package=None, globs=None, |
| 2148 | setUp=None, tearDown=None, |
| 2149 | optionflags=0): |
| 2150 | package = _normalize_module(package) |
| 2151 | name = path.split('/')[-1] |
| 2152 | dir = os.path.split(package.__file__)[0] |
| 2153 | path = os.path.join(dir, *(path.split('/'))) |
| 2154 | doc = open(path).read() |
| 2155 | |
| 2156 | if globs is None: |
| 2157 | globs = {} |
| 2158 | |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 2159 | test = DocTestParser().get_doctest(doc, globs, name, path, 0) |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2160 | |
| 2161 | return DocFileCase(test, optionflags, setUp, tearDown) |
| 2162 | |
| 2163 | def DocFileSuite(*paths, **kw): |
| 2164 | """Creates a suite of doctest files. |
| 2165 | |
| 2166 | One or more text file paths are given as strings. These should |
| 2167 | use "/" characters to separate path segments. Paths are relative |
| 2168 | to the directory of the calling module, or relative to the package |
| 2169 | passed as a keyword argument. |
| 2170 | |
| 2171 | A number of options may be provided as keyword arguments: |
| 2172 | |
| 2173 | package |
| 2174 | The name of a Python package. Text-file paths will be |
| 2175 | interpreted relative to the directory containing this package. |
| 2176 | The package may be supplied as a package object or as a dotted |
| 2177 | package name. |
| 2178 | |
| 2179 | setUp |
| 2180 | The name of a set-up function. This is called before running the |
| 2181 | tests in each file. |
| 2182 | |
| 2183 | tearDown |
| 2184 | The name of a tear-down function. This is called after running the |
| 2185 | tests in each file. |
| 2186 | |
| 2187 | globs |
| 2188 | A dictionary containing initial global variables for the tests. |
| 2189 | """ |
| 2190 | suite = unittest.TestSuite() |
| 2191 | |
| 2192 | # We do this here so that _normalize_module is called at the right |
| 2193 | # level. If it were called in DocFileTest, then this function |
| 2194 | # would be the caller and we might guess the package incorrectly. |
| 2195 | kw['package'] = _normalize_module(kw.get('package')) |
| 2196 | |
| 2197 | for path in paths: |
| 2198 | suite.addTest(DocFileTest(path, **kw)) |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2199 | |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 2200 | return suite |
| 2201 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2202 | ###################################################################### |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 2203 | ## 9. Debugging Support |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2204 | ###################################################################### |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2205 | |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2206 | def script_from_examples(s): |
| 2207 | r"""Extract script from text with examples. |
| 2208 | |
| 2209 | Converts text with examples to a Python script. Example input is |
| 2210 | converted to regular code. Example output and all other words |
| 2211 | are converted to comments: |
| 2212 | |
| 2213 | >>> text = ''' |
| 2214 | ... Here are examples of simple math. |
| 2215 | ... |
| 2216 | ... Python has super accurate integer addition |
| 2217 | ... |
| 2218 | ... >>> 2 + 2 |
| 2219 | ... 5 |
| 2220 | ... |
| 2221 | ... And very friendly error messages: |
| 2222 | ... |
| 2223 | ... >>> 1/0 |
| 2224 | ... To Infinity |
| 2225 | ... And |
| 2226 | ... Beyond |
| 2227 | ... |
| 2228 | ... You can use logic if you want: |
| 2229 | ... |
| 2230 | ... >>> if 0: |
| 2231 | ... ... blah |
| 2232 | ... ... blah |
| 2233 | ... ... |
| 2234 | ... |
| 2235 | ... Ho hum |
| 2236 | ... ''' |
| 2237 | |
| 2238 | >>> print script_from_examples(text) |
Edward Loper | a5db600 | 2004-08-12 02:41:30 +0000 | [diff] [blame] | 2239 | # Here are examples of simple math. |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2240 | # |
Edward Loper | a5db600 | 2004-08-12 02:41:30 +0000 | [diff] [blame] | 2241 | # Python has super accurate integer addition |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2242 | # |
| 2243 | 2 + 2 |
| 2244 | # Expected: |
Edward Loper | a5db600 | 2004-08-12 02:41:30 +0000 | [diff] [blame] | 2245 | ## 5 |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2246 | # |
Edward Loper | a5db600 | 2004-08-12 02:41:30 +0000 | [diff] [blame] | 2247 | # And very friendly error messages: |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2248 | # |
| 2249 | 1/0 |
| 2250 | # Expected: |
Edward Loper | a5db600 | 2004-08-12 02:41:30 +0000 | [diff] [blame] | 2251 | ## To Infinity |
| 2252 | ## And |
| 2253 | ## Beyond |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2254 | # |
Edward Loper | a5db600 | 2004-08-12 02:41:30 +0000 | [diff] [blame] | 2255 | # You can use logic if you want: |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2256 | # |
| 2257 | if 0: |
| 2258 | blah |
| 2259 | blah |
| 2260 | <BLANKLINE> |
| 2261 | # |
Edward Loper | a5db600 | 2004-08-12 02:41:30 +0000 | [diff] [blame] | 2262 | # Ho hum |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2263 | """ |
| 2264 | |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 2265 | return DocTestParser().get_program(s) |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2266 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2267 | def _want_comment(example): |
| 2268 | """ |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2269 | Return a comment containing the expected output for the given example. |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2270 | """ |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2271 | # Return the expected output, if any |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2272 | want = example.want |
| 2273 | if want: |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2274 | if want[-1] == '\n': |
| 2275 | want = want[:-1] |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2276 | want = "\n# ".join(want.split("\n")) |
| 2277 | want = "\n# Expected:\n# %s" % want |
| 2278 | return want |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 2279 | |
| 2280 | def testsource(module, name): |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2281 | """Extract the test sources from a doctest docstring as a script. |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 2282 | |
| 2283 | Provide the module (or dotted name of the module) containing the |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2284 | test to be debugged and the name (within the module) of the object |
| 2285 | with the doc string with tests to be debugged. |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 2286 | """ |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2287 | module = _normalize_module(module) |
| 2288 | tests = DocTestFinder().find(module) |
| 2289 | test = [t for t in tests if t.name == name] |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 2290 | if not test: |
| 2291 | raise ValueError(name, "not found in tests") |
| 2292 | test = test[0] |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2293 | testsrc = script_from_examples(test.docstring) |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2294 | return testsrc |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 2295 | |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2296 | def debug_src(src, pm=False, globs=None): |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2297 | """Debug a single doctest docstring, in argument `src`'""" |
| 2298 | testsrc = script_from_examples(src) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2299 | debug_script(testsrc, pm, globs) |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 2300 | |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2301 | def debug_script(src, pm=False, globs=None): |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2302 | "Debug a test script. `src` is the script, as a string." |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 2303 | import pdb |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 2304 | |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 2305 | srcfilename = tempfile.mktemp("doctestdebug.py") |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2306 | f = open(srcfilename, 'w') |
| 2307 | f.write(src) |
| 2308 | f.close() |
| 2309 | |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2310 | if globs: |
| 2311 | globs = globs.copy() |
| 2312 | else: |
| 2313 | globs = {} |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 2314 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2315 | if pm: |
| 2316 | try: |
| 2317 | execfile(srcfilename, globs, globs) |
| 2318 | except: |
| 2319 | print sys.exc_info()[1] |
| 2320 | pdb.post_mortem(sys.exc_info()[2]) |
| 2321 | else: |
| 2322 | # Note that %r is vital here. '%s' instead can, e.g., cause |
| 2323 | # backslashes to get treated as metacharacters on Windows. |
| 2324 | pdb.run("execfile(%r)" % srcfilename, globs, globs) |
Tim Peters | db3756d | 2003-06-29 05:30:48 +0000 | [diff] [blame] | 2325 | |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2326 | def debug(module, name, pm=False): |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2327 | """Debug a single doctest docstring. |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2328 | |
| 2329 | Provide the module (or dotted name of the module) containing the |
| 2330 | test to be debugged and the name (within the module) of the object |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 2331 | with the docstring with tests to be debugged. |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2332 | """ |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2333 | module = _normalize_module(module) |
Jim Fulton | a643b65 | 2004-07-14 19:06:50 +0000 | [diff] [blame] | 2334 | testsrc = testsource(module, name) |
| 2335 | debug_script(testsrc, pm, module.__dict__) |
| 2336 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2337 | ###################################################################### |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 2338 | ## 10. Example Usage |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2339 | ###################################################################### |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 2340 | class _TestClass: |
| 2341 | """ |
| 2342 | A pointless class, for sanity-checking of docstring testing. |
| 2343 | |
| 2344 | Methods: |
| 2345 | square() |
| 2346 | get() |
| 2347 | |
| 2348 | >>> _TestClass(13).get() + _TestClass(-12).get() |
| 2349 | 1 |
| 2350 | >>> hex(_TestClass(13).square().get()) |
| 2351 | '0xa9' |
| 2352 | """ |
| 2353 | |
| 2354 | def __init__(self, val): |
| 2355 | """val -> _TestClass object with associated value val. |
| 2356 | |
| 2357 | >>> t = _TestClass(123) |
| 2358 | >>> print t.get() |
| 2359 | 123 |
| 2360 | """ |
| 2361 | |
| 2362 | self.val = val |
| 2363 | |
| 2364 | def square(self): |
| 2365 | """square() -> square TestClass's associated value |
| 2366 | |
| 2367 | >>> _TestClass(13).square().get() |
| 2368 | 169 |
| 2369 | """ |
| 2370 | |
| 2371 | self.val = self.val ** 2 |
| 2372 | return self |
| 2373 | |
| 2374 | def get(self): |
| 2375 | """get() -> return TestClass's associated value. |
| 2376 | |
| 2377 | >>> x = _TestClass(-42) |
| 2378 | >>> print x.get() |
| 2379 | -42 |
| 2380 | """ |
| 2381 | |
| 2382 | return self.val |
| 2383 | |
| 2384 | __test__ = {"_TestClass": _TestClass, |
| 2385 | "string": r""" |
| 2386 | Example of a string object, searched as-is. |
| 2387 | >>> x = 1; y = 2 |
| 2388 | >>> x + y, x * y |
| 2389 | (3, 2) |
Tim Peters | 6ebe61f | 2003-06-27 20:48:05 +0000 | [diff] [blame] | 2390 | """, |
| 2391 | "bool-int equivalence": r""" |
| 2392 | In 2.2, boolean expressions displayed |
| 2393 | 0 or 1. By default, we still accept |
| 2394 | them. This can be disabled by passing |
| 2395 | DONT_ACCEPT_TRUE_FOR_1 to the new |
| 2396 | optionflags argument. |
| 2397 | >>> 4 == 4 |
| 2398 | 1 |
| 2399 | >>> 4 == 4 |
| 2400 | True |
| 2401 | >>> 4 > 4 |
| 2402 | 0 |
| 2403 | >>> 4 > 4 |
| 2404 | False |
| 2405 | """, |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2406 | "blank lines": r""" |
| 2407 | Blank lines can be marked with <BLANKLINE>: |
| 2408 | >>> print 'foo\n\nbar\n' |
| 2409 | foo |
| 2410 | <BLANKLINE> |
| 2411 | bar |
| 2412 | <BLANKLINE> |
| 2413 | """, |
| 2414 | } |
| 2415 | # "ellipsis": r""" |
| 2416 | # If the ellipsis flag is used, then '...' can be used to |
| 2417 | # elide substrings in the desired output: |
| 2418 | # >>> print range(1000) |
| 2419 | # [0, 1, 2, ..., 999] |
| 2420 | # """, |
| 2421 | # "whitespace normalization": r""" |
| 2422 | # If the whitespace normalization flag is used, then |
| 2423 | # differences in whitespace are ignored. |
| 2424 | # >>> print range(30) |
| 2425 | # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, |
| 2426 | # 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, |
| 2427 | # 27, 28, 29] |
| 2428 | # """, |
| 2429 | # } |
| 2430 | |
| 2431 | def test1(): r""" |
Tim Peters | 3ddd60a | 2004-08-08 02:43:33 +0000 | [diff] [blame] | 2432 | >>> warnings.filterwarnings("ignore", "class Tester", DeprecationWarning, |
| 2433 | ... "doctest", 0) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2434 | >>> from doctest import Tester |
| 2435 | >>> t = Tester(globs={'x': 42}, verbose=0) |
| 2436 | >>> t.runstring(r''' |
| 2437 | ... >>> x = x * 2 |
| 2438 | ... >>> print x |
| 2439 | ... 42 |
| 2440 | ... ''', 'XYZ') |
| 2441 | ********************************************************************** |
| 2442 | Failure in example: print x |
| 2443 | from line #2 of XYZ |
| 2444 | Expected: 42 |
| 2445 | Got: 84 |
| 2446 | (1, 2) |
| 2447 | >>> t.runstring(">>> x = x * 2\n>>> print x\n84\n", 'example2') |
| 2448 | (0, 2) |
| 2449 | >>> t.summarize() |
| 2450 | ********************************************************************** |
| 2451 | 1 items had failures: |
| 2452 | 1 of 2 in XYZ |
| 2453 | ***Test Failed*** 1 failures. |
| 2454 | (1, 4) |
| 2455 | >>> t.summarize(verbose=1) |
| 2456 | 1 items passed all tests: |
| 2457 | 2 tests in example2 |
| 2458 | ********************************************************************** |
| 2459 | 1 items had failures: |
| 2460 | 1 of 2 in XYZ |
| 2461 | 4 tests in 2 items. |
| 2462 | 3 passed and 1 failed. |
| 2463 | ***Test Failed*** 1 failures. |
| 2464 | (1, 4) |
| 2465 | """ |
| 2466 | |
| 2467 | def test2(): r""" |
Tim Peters | 3ddd60a | 2004-08-08 02:43:33 +0000 | [diff] [blame] | 2468 | >>> warnings.filterwarnings("ignore", "class Tester", |
| 2469 | ... DeprecationWarning, "doctest", 0) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2470 | >>> t = Tester(globs={}, verbose=1) |
| 2471 | >>> test = r''' |
| 2472 | ... # just an example |
| 2473 | ... >>> x = 1 + 2 |
| 2474 | ... >>> x |
| 2475 | ... 3 |
| 2476 | ... ''' |
| 2477 | >>> t.runstring(test, "Example") |
| 2478 | Running string Example |
| 2479 | Trying: x = 1 + 2 |
| 2480 | Expecting: nothing |
| 2481 | ok |
| 2482 | Trying: x |
| 2483 | Expecting: 3 |
| 2484 | ok |
| 2485 | 0 of 2 examples failed in string Example |
| 2486 | (0, 2) |
| 2487 | """ |
| 2488 | def test3(): r""" |
Tim Peters | 3ddd60a | 2004-08-08 02:43:33 +0000 | [diff] [blame] | 2489 | >>> warnings.filterwarnings("ignore", "class Tester", |
| 2490 | ... DeprecationWarning, "doctest", 0) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2491 | >>> t = Tester(globs={}, verbose=0) |
| 2492 | >>> def _f(): |
| 2493 | ... '''Trivial docstring example. |
| 2494 | ... >>> assert 2 == 2 |
| 2495 | ... ''' |
| 2496 | ... return 32 |
| 2497 | ... |
| 2498 | >>> t.rundoc(_f) # expect 0 failures in 1 example |
| 2499 | (0, 1) |
| 2500 | """ |
| 2501 | def test4(): """ |
| 2502 | >>> import new |
| 2503 | >>> m1 = new.module('_m1') |
| 2504 | >>> m2 = new.module('_m2') |
| 2505 | >>> test_data = \""" |
| 2506 | ... def _f(): |
| 2507 | ... '''>>> assert 1 == 1 |
| 2508 | ... ''' |
| 2509 | ... def g(): |
| 2510 | ... '''>>> assert 2 != 1 |
| 2511 | ... ''' |
| 2512 | ... class H: |
| 2513 | ... '''>>> assert 2 > 1 |
| 2514 | ... ''' |
| 2515 | ... def bar(self): |
| 2516 | ... '''>>> assert 1 < 2 |
| 2517 | ... ''' |
| 2518 | ... \""" |
| 2519 | >>> exec test_data in m1.__dict__ |
| 2520 | >>> exec test_data in m2.__dict__ |
| 2521 | >>> m1.__dict__.update({"f2": m2._f, "g2": m2.g, "h2": m2.H}) |
| 2522 | |
| 2523 | Tests that objects outside m1 are excluded: |
| 2524 | |
Tim Peters | 3ddd60a | 2004-08-08 02:43:33 +0000 | [diff] [blame] | 2525 | >>> warnings.filterwarnings("ignore", "class Tester", |
| 2526 | ... DeprecationWarning, "doctest", 0) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2527 | >>> t = Tester(globs={}, verbose=0) |
Tim Peters | f727c6c | 2004-08-08 01:48:59 +0000 | [diff] [blame] | 2528 | >>> t.rundict(m1.__dict__, "rundict_test", m1) # f2 and g2 and h2 skipped |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2529 | (0, 4) |
| 2530 | |
Tim Peters | f727c6c | 2004-08-08 01:48:59 +0000 | [diff] [blame] | 2531 | Once more, not excluding stuff outside m1: |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2532 | |
| 2533 | >>> t = Tester(globs={}, verbose=0) |
| 2534 | >>> t.rundict(m1.__dict__, "rundict_test_pvt") # None are skipped. |
| 2535 | (0, 8) |
| 2536 | |
| 2537 | The exclusion of objects from outside the designated module is |
| 2538 | meant to be invoked automagically by testmod. |
| 2539 | |
Tim Peters | f727c6c | 2004-08-08 01:48:59 +0000 | [diff] [blame] | 2540 | >>> testmod(m1, verbose=False) |
| 2541 | (0, 4) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2542 | """ |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 2543 | |
| 2544 | def _test(): |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2545 | #import doctest |
| 2546 | #doctest.testmod(doctest, verbose=False, |
| 2547 | # optionflags=ELLIPSIS | NORMALIZE_WHITESPACE | |
| 2548 | # UNIFIED_DIFF) |
| 2549 | #print '~'*70 |
| 2550 | r = unittest.TextTestRunner() |
| 2551 | r.run(DocTestSuite()) |
Tim Peters | 8a7d2d5 | 2001-01-16 07:10:57 +0000 | [diff] [blame] | 2552 | |
| 2553 | if __name__ == "__main__": |
| 2554 | _test() |