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