Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1 | """ |
| 2 | Test script for doctest. |
| 3 | """ |
| 4 | |
Barry Warsaw | 04f357c | 2002-07-23 19:04:11 +0000 | [diff] [blame] | 5 | from test import test_support |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 6 | import doctest |
Tim Peters | a7def72 | 2004-08-23 22:13:22 +0000 | [diff] [blame] | 7 | import warnings |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 8 | |
| 9 | ###################################################################### |
| 10 | ## Sample Objects (used by test cases) |
| 11 | ###################################################################### |
| 12 | |
| 13 | def sample_func(v): |
| 14 | """ |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 15 | Blah blah |
| 16 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 17 | >>> print sample_func(22) |
| 18 | 44 |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 19 | |
| 20 | Yee ha! |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 21 | """ |
| 22 | return v+v |
| 23 | |
| 24 | class SampleClass: |
| 25 | """ |
| 26 | >>> print 1 |
| 27 | 1 |
| 28 | """ |
| 29 | def __init__(self, val): |
| 30 | """ |
| 31 | >>> print SampleClass(12).get() |
| 32 | 12 |
| 33 | """ |
| 34 | self.val = val |
| 35 | |
| 36 | def double(self): |
| 37 | """ |
| 38 | >>> print SampleClass(12).double().get() |
| 39 | 24 |
| 40 | """ |
| 41 | return SampleClass(self.val + self.val) |
| 42 | |
| 43 | def get(self): |
| 44 | """ |
| 45 | >>> print SampleClass(-5).get() |
| 46 | -5 |
| 47 | """ |
| 48 | return self.val |
| 49 | |
| 50 | def a_staticmethod(v): |
| 51 | """ |
| 52 | >>> print SampleClass.a_staticmethod(10) |
| 53 | 11 |
| 54 | """ |
| 55 | return v+1 |
| 56 | a_staticmethod = staticmethod(a_staticmethod) |
| 57 | |
| 58 | def a_classmethod(cls, v): |
| 59 | """ |
| 60 | >>> print SampleClass.a_classmethod(10) |
| 61 | 12 |
| 62 | >>> print SampleClass(0).a_classmethod(10) |
| 63 | 12 |
| 64 | """ |
| 65 | return v+2 |
| 66 | a_classmethod = classmethod(a_classmethod) |
| 67 | |
| 68 | a_property = property(get, doc=""" |
| 69 | >>> print SampleClass(22).a_property |
| 70 | 22 |
| 71 | """) |
| 72 | |
| 73 | class NestedClass: |
| 74 | """ |
| 75 | >>> x = SampleClass.NestedClass(5) |
| 76 | >>> y = x.square() |
| 77 | >>> print y.get() |
| 78 | 25 |
| 79 | """ |
| 80 | def __init__(self, val=0): |
| 81 | """ |
| 82 | >>> print SampleClass.NestedClass().get() |
| 83 | 0 |
| 84 | """ |
| 85 | self.val = val |
| 86 | def square(self): |
| 87 | return SampleClass.NestedClass(self.val*self.val) |
| 88 | def get(self): |
| 89 | return self.val |
| 90 | |
| 91 | class SampleNewStyleClass(object): |
| 92 | r""" |
| 93 | >>> print '1\n2\n3' |
| 94 | 1 |
| 95 | 2 |
| 96 | 3 |
| 97 | """ |
| 98 | def __init__(self, val): |
| 99 | """ |
| 100 | >>> print SampleNewStyleClass(12).get() |
| 101 | 12 |
| 102 | """ |
| 103 | self.val = val |
| 104 | |
| 105 | def double(self): |
| 106 | """ |
| 107 | >>> print SampleNewStyleClass(12).double().get() |
| 108 | 24 |
| 109 | """ |
| 110 | return SampleNewStyleClass(self.val + self.val) |
| 111 | |
| 112 | def get(self): |
| 113 | """ |
| 114 | >>> print SampleNewStyleClass(-5).get() |
| 115 | -5 |
| 116 | """ |
| 117 | return self.val |
| 118 | |
| 119 | ###################################################################### |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 120 | ## Fake stdin (for testing interactive debugging) |
| 121 | ###################################################################### |
| 122 | |
| 123 | class _FakeInput: |
| 124 | """ |
| 125 | A fake input stream for pdb's interactive debugger. Whenever a |
| 126 | line is read, print it (to simulate the user typing it), and then |
| 127 | return it. The set of lines to return is specified in the |
| 128 | constructor; they should not have trailing newlines. |
| 129 | """ |
| 130 | def __init__(self, lines): |
| 131 | self.lines = lines |
| 132 | |
| 133 | def readline(self): |
| 134 | line = self.lines.pop(0) |
| 135 | print line |
| 136 | return line+'\n' |
| 137 | |
| 138 | ###################################################################### |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 139 | ## Test Cases |
| 140 | ###################################################################### |
| 141 | |
| 142 | def test_Example(): r""" |
| 143 | Unit tests for the `Example` class. |
| 144 | |
Edward Loper | a6b6832 | 2004-08-26 00:05:43 +0000 | [diff] [blame] | 145 | Example is a simple container class that holds: |
| 146 | - `source`: A source string. |
| 147 | - `want`: An expected output string. |
| 148 | - `exc_msg`: An expected exception message string (or None if no |
| 149 | exception is expected). |
| 150 | - `lineno`: A line number (within the docstring). |
| 151 | - `indent`: The example's indentation in the input string. |
| 152 | - `options`: An option dictionary, mapping option flags to True or |
| 153 | False. |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 154 | |
Edward Loper | a6b6832 | 2004-08-26 00:05:43 +0000 | [diff] [blame] | 155 | These attributes are set by the constructor. `source` and `want` are |
| 156 | required; the other attributes all have default values: |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 157 | |
Edward Loper | a6b6832 | 2004-08-26 00:05:43 +0000 | [diff] [blame] | 158 | >>> example = doctest.Example('print 1', '1\n') |
| 159 | >>> (example.source, example.want, example.exc_msg, |
| 160 | ... example.lineno, example.indent, example.options) |
| 161 | ('print 1\n', '1\n', None, 0, 0, {}) |
| 162 | |
| 163 | The first three attributes (`source`, `want`, and `exc_msg`) may be |
| 164 | specified positionally; the remaining arguments should be specified as |
| 165 | keyword arguments: |
| 166 | |
| 167 | >>> exc_msg = 'IndexError: pop from an empty list' |
| 168 | >>> example = doctest.Example('[].pop()', '', exc_msg, |
| 169 | ... lineno=5, indent=4, |
| 170 | ... options={doctest.ELLIPSIS: True}) |
| 171 | >>> (example.source, example.want, example.exc_msg, |
| 172 | ... example.lineno, example.indent, example.options) |
| 173 | ('[].pop()\n', '', 'IndexError: pop from an empty list\n', 5, 4, {8: True}) |
| 174 | |
| 175 | The constructor normalizes the `source` string to end in a newline: |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 176 | |
Tim Peters | bb43147 | 2004-08-09 03:51:46 +0000 | [diff] [blame] | 177 | Source spans a single line: no terminating newline. |
Edward Loper | a6b6832 | 2004-08-26 00:05:43 +0000 | [diff] [blame] | 178 | >>> e = doctest.Example('print 1', '1\n') |
Tim Peters | bb43147 | 2004-08-09 03:51:46 +0000 | [diff] [blame] | 179 | >>> e.source, e.want |
| 180 | ('print 1\n', '1\n') |
| 181 | |
Edward Loper | a6b6832 | 2004-08-26 00:05:43 +0000 | [diff] [blame] | 182 | >>> e = doctest.Example('print 1\n', '1\n') |
Tim Peters | bb43147 | 2004-08-09 03:51:46 +0000 | [diff] [blame] | 183 | >>> e.source, e.want |
| 184 | ('print 1\n', '1\n') |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 185 | |
Tim Peters | bb43147 | 2004-08-09 03:51:46 +0000 | [diff] [blame] | 186 | Source spans multiple lines: require terminating newline. |
Edward Loper | a6b6832 | 2004-08-26 00:05:43 +0000 | [diff] [blame] | 187 | >>> e = doctest.Example('print 1;\nprint 2\n', '1\n2\n') |
Tim Peters | bb43147 | 2004-08-09 03:51:46 +0000 | [diff] [blame] | 188 | >>> e.source, e.want |
| 189 | ('print 1;\nprint 2\n', '1\n2\n') |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 190 | |
Edward Loper | a6b6832 | 2004-08-26 00:05:43 +0000 | [diff] [blame] | 191 | >>> e = doctest.Example('print 1;\nprint 2', '1\n2\n') |
Tim Peters | bb43147 | 2004-08-09 03:51:46 +0000 | [diff] [blame] | 192 | >>> e.source, e.want |
| 193 | ('print 1;\nprint 2\n', '1\n2\n') |
| 194 | |
Edward Loper | a6b6832 | 2004-08-26 00:05:43 +0000 | [diff] [blame] | 195 | Empty source string (which should never appear in real examples) |
| 196 | >>> e = doctest.Example('', '') |
| 197 | >>> e.source, e.want |
| 198 | ('\n', '') |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 199 | |
Edward Loper | a6b6832 | 2004-08-26 00:05:43 +0000 | [diff] [blame] | 200 | The constructor normalizes the `want` string to end in a newline, |
| 201 | unless it's the empty string: |
| 202 | |
| 203 | >>> e = doctest.Example('print 1', '1\n') |
Tim Peters | bb43147 | 2004-08-09 03:51:46 +0000 | [diff] [blame] | 204 | >>> e.source, e.want |
| 205 | ('print 1\n', '1\n') |
| 206 | |
Edward Loper | a6b6832 | 2004-08-26 00:05:43 +0000 | [diff] [blame] | 207 | >>> e = doctest.Example('print 1', '1') |
Tim Peters | bb43147 | 2004-08-09 03:51:46 +0000 | [diff] [blame] | 208 | >>> e.source, e.want |
| 209 | ('print 1\n', '1\n') |
| 210 | |
Edward Loper | a6b6832 | 2004-08-26 00:05:43 +0000 | [diff] [blame] | 211 | >>> e = doctest.Example('print', '') |
Tim Peters | bb43147 | 2004-08-09 03:51:46 +0000 | [diff] [blame] | 212 | >>> e.source, e.want |
| 213 | ('print\n', '') |
Edward Loper | a6b6832 | 2004-08-26 00:05:43 +0000 | [diff] [blame] | 214 | |
| 215 | The constructor normalizes the `exc_msg` string to end in a newline, |
| 216 | unless it's `None`: |
| 217 | |
| 218 | Message spans one line |
| 219 | >>> exc_msg = 'IndexError: pop from an empty list' |
| 220 | >>> e = doctest.Example('[].pop()', '', exc_msg) |
| 221 | >>> e.exc_msg |
| 222 | 'IndexError: pop from an empty list\n' |
| 223 | |
| 224 | >>> exc_msg = 'IndexError: pop from an empty list\n' |
| 225 | >>> e = doctest.Example('[].pop()', '', exc_msg) |
| 226 | >>> e.exc_msg |
| 227 | 'IndexError: pop from an empty list\n' |
| 228 | |
| 229 | Message spans multiple lines |
| 230 | >>> exc_msg = 'ValueError: 1\n 2' |
| 231 | >>> e = doctest.Example('raise ValueError("1\n 2")', '', exc_msg) |
| 232 | >>> e.exc_msg |
| 233 | 'ValueError: 1\n 2\n' |
| 234 | |
| 235 | >>> exc_msg = 'ValueError: 1\n 2\n' |
| 236 | >>> e = doctest.Example('raise ValueError("1\n 2")', '', exc_msg) |
| 237 | >>> e.exc_msg |
| 238 | 'ValueError: 1\n 2\n' |
| 239 | |
| 240 | Empty (but non-None) exception message (which should never appear |
| 241 | in real examples) |
| 242 | >>> exc_msg = '' |
| 243 | >>> e = doctest.Example('raise X()', '', exc_msg) |
| 244 | >>> e.exc_msg |
| 245 | '\n' |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 246 | """ |
| 247 | |
| 248 | def test_DocTest(): r""" |
| 249 | Unit tests for the `DocTest` class. |
| 250 | |
| 251 | DocTest is a collection of examples, extracted from a docstring, along |
| 252 | with information about where the docstring comes from (a name, |
| 253 | filename, and line number). The docstring is parsed by the `DocTest` |
| 254 | constructor: |
| 255 | |
| 256 | >>> docstring = ''' |
| 257 | ... >>> print 12 |
| 258 | ... 12 |
| 259 | ... |
| 260 | ... Non-example text. |
| 261 | ... |
| 262 | ... >>> print 'another\example' |
| 263 | ... another |
| 264 | ... example |
| 265 | ... ''' |
| 266 | >>> globs = {} # globals to run the test in. |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 267 | >>> parser = doctest.DocTestParser() |
| 268 | >>> test = parser.get_doctest(docstring, globs, 'some_test', |
| 269 | ... 'some_file', 20) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 270 | >>> print test |
| 271 | <DocTest some_test from some_file:20 (2 examples)> |
| 272 | >>> len(test.examples) |
| 273 | 2 |
| 274 | >>> e1, e2 = test.examples |
| 275 | >>> (e1.source, e1.want, e1.lineno) |
Tim Peters | bb43147 | 2004-08-09 03:51:46 +0000 | [diff] [blame] | 276 | ('print 12\n', '12\n', 1) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 277 | >>> (e2.source, e2.want, e2.lineno) |
Tim Peters | bb43147 | 2004-08-09 03:51:46 +0000 | [diff] [blame] | 278 | ("print 'another\\example'\n", 'another\nexample\n', 6) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 279 | |
| 280 | Source information (name, filename, and line number) is available as |
| 281 | attributes on the doctest object: |
| 282 | |
| 283 | >>> (test.name, test.filename, test.lineno) |
| 284 | ('some_test', 'some_file', 20) |
| 285 | |
| 286 | The line number of an example within its containing file is found by |
| 287 | adding the line number of the example and the line number of its |
| 288 | containing test: |
| 289 | |
| 290 | >>> test.lineno + e1.lineno |
| 291 | 21 |
| 292 | >>> test.lineno + e2.lineno |
| 293 | 26 |
| 294 | |
| 295 | If the docstring contains inconsistant leading whitespace in the |
| 296 | expected output of an example, then `DocTest` will raise a ValueError: |
| 297 | |
| 298 | >>> docstring = r''' |
| 299 | ... >>> print 'bad\nindentation' |
| 300 | ... bad |
| 301 | ... indentation |
| 302 | ... ''' |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 303 | >>> parser.get_doctest(docstring, globs, 'some_test', 'filename', 0) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 304 | Traceback (most recent call last): |
Edward Loper | 00f8da7 | 2004-08-26 18:05:07 +0000 | [diff] [blame] | 305 | ValueError: line 4 of the docstring for some_test has inconsistent leading whitespace: 'indentation' |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 306 | |
| 307 | If the docstring contains inconsistent leading whitespace on |
| 308 | continuation lines, then `DocTest` will raise a ValueError: |
| 309 | |
| 310 | >>> docstring = r''' |
| 311 | ... >>> print ('bad indentation', |
| 312 | ... ... 2) |
| 313 | ... ('bad', 'indentation') |
| 314 | ... ''' |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 315 | >>> parser.get_doctest(docstring, globs, 'some_test', 'filename', 0) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 316 | Traceback (most recent call last): |
Edward Loper | 00f8da7 | 2004-08-26 18:05:07 +0000 | [diff] [blame] | 317 | ValueError: line 2 of the docstring for some_test has inconsistent leading whitespace: '... 2)' |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 318 | |
| 319 | If there's no blank space after a PS1 prompt ('>>>'), then `DocTest` |
| 320 | will raise a ValueError: |
| 321 | |
| 322 | >>> docstring = '>>>print 1\n1' |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 323 | >>> parser.get_doctest(docstring, globs, 'some_test', 'filename', 0) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 324 | Traceback (most recent call last): |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 325 | ValueError: line 1 of the docstring for some_test lacks blank after >>>: '>>>print 1' |
| 326 | |
| 327 | If there's no blank space after a PS2 prompt ('...'), then `DocTest` |
| 328 | will raise a ValueError: |
| 329 | |
| 330 | >>> docstring = '>>> if 1:\n...print 1\n1' |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 331 | >>> parser.get_doctest(docstring, globs, 'some_test', 'filename', 0) |
Edward Loper | 7c74846 | 2004-08-09 02:06:06 +0000 | [diff] [blame] | 332 | Traceback (most recent call last): |
| 333 | ValueError: line 2 of the docstring for some_test lacks blank after ...: '...print 1' |
| 334 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 335 | """ |
| 336 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 337 | def test_DocTestFinder(): r""" |
| 338 | Unit tests for the `DocTestFinder` class. |
| 339 | |
| 340 | DocTestFinder is used to extract DocTests from an object's docstring |
| 341 | and the docstrings of its contained objects. It can be used with |
| 342 | modules, functions, classes, methods, staticmethods, classmethods, and |
| 343 | properties. |
| 344 | |
| 345 | Finding Tests in Functions |
| 346 | ~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 347 | For a function whose docstring contains examples, DocTestFinder.find() |
| 348 | will return a single test (for that function's docstring): |
| 349 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 350 | >>> finder = doctest.DocTestFinder() |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 351 | |
| 352 | We'll simulate a __file__ attr that ends in pyc: |
| 353 | |
| 354 | >>> import test.test_doctest |
| 355 | >>> old = test.test_doctest.__file__ |
| 356 | >>> test.test_doctest.__file__ = 'test_doctest.pyc' |
| 357 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 358 | >>> tests = finder.find(sample_func) |
Edward Loper | 8e4a34b | 2004-08-12 02:34:27 +0000 | [diff] [blame] | 359 | |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 360 | >>> print tests # doctest: +ELLIPSIS |
Tim Peters | a7def72 | 2004-08-23 22:13:22 +0000 | [diff] [blame] | 361 | [<DocTest sample_func from ...:13 (1 example)>] |
Edward Loper | 8e4a34b | 2004-08-12 02:34:27 +0000 | [diff] [blame] | 362 | |
Tim Peters | 4de7c5c | 2004-08-23 22:38:05 +0000 | [diff] [blame] | 363 | The exact name depends on how test_doctest was invoked, so allow for |
| 364 | leading path components. |
| 365 | |
| 366 | >>> tests[0].filename # doctest: +ELLIPSIS |
| 367 | '...test_doctest.py' |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 368 | |
| 369 | >>> test.test_doctest.__file__ = old |
Tim Peters | c6cbab0 | 2004-08-22 19:43:28 +0000 | [diff] [blame] | 370 | |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 371 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 372 | >>> e = tests[0].examples[0] |
Tim Peters | bb43147 | 2004-08-09 03:51:46 +0000 | [diff] [blame] | 373 | >>> (e.source, e.want, e.lineno) |
| 374 | ('print sample_func(22)\n', '44\n', 3) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 375 | |
Edward Loper | 32ddbf7 | 2004-09-13 05:47:24 +0000 | [diff] [blame] | 376 | By default, tests are created for objects with no docstring: |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 377 | |
| 378 | >>> def no_docstring(v): |
| 379 | ... pass |
Tim Peters | 958cc89 | 2004-09-13 14:53:28 +0000 | [diff] [blame^] | 380 | >>> finder.find(no_docstring) |
| 381 | [] |
Edward Loper | 32ddbf7 | 2004-09-13 05:47:24 +0000 | [diff] [blame] | 382 | |
| 383 | However, the optional argument `exclude_empty` to the DocTestFinder |
| 384 | constructor can be used to exclude tests for objects with empty |
| 385 | docstrings: |
| 386 | |
| 387 | >>> def no_docstring(v): |
| 388 | ... pass |
| 389 | >>> excl_empty_finder = doctest.DocTestFinder(exclude_empty=True) |
| 390 | >>> excl_empty_finder.find(no_docstring) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 391 | [] |
| 392 | |
| 393 | If the function has a docstring with no examples, then a test with no |
| 394 | examples is returned. (This lets `DocTestRunner` collect statistics |
| 395 | about which functions have no tests -- but is that useful? And should |
| 396 | an empty test also be created when there's no docstring?) |
| 397 | |
| 398 | >>> def no_examples(v): |
| 399 | ... ''' no doctest examples ''' |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 400 | >>> finder.find(no_examples) # doctest: +ELLIPSIS |
| 401 | [<DocTest no_examples from ...:1 (no examples)>] |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 402 | |
| 403 | Finding Tests in Classes |
| 404 | ~~~~~~~~~~~~~~~~~~~~~~~~ |
| 405 | For a class, DocTestFinder will create a test for the class's |
| 406 | docstring, and will recursively explore its contents, including |
| 407 | methods, classmethods, staticmethods, properties, and nested classes. |
| 408 | |
| 409 | >>> finder = doctest.DocTestFinder() |
| 410 | >>> tests = finder.find(SampleClass) |
| 411 | >>> tests.sort() |
| 412 | >>> for t in tests: |
| 413 | ... print '%2s %s' % (len(t.examples), t.name) |
| 414 | 1 SampleClass |
| 415 | 3 SampleClass.NestedClass |
| 416 | 1 SampleClass.NestedClass.__init__ |
| 417 | 1 SampleClass.__init__ |
| 418 | 2 SampleClass.a_classmethod |
| 419 | 1 SampleClass.a_property |
| 420 | 1 SampleClass.a_staticmethod |
| 421 | 1 SampleClass.double |
| 422 | 1 SampleClass.get |
| 423 | |
| 424 | New-style classes are also supported: |
| 425 | |
| 426 | >>> tests = finder.find(SampleNewStyleClass) |
| 427 | >>> tests.sort() |
| 428 | >>> for t in tests: |
| 429 | ... print '%2s %s' % (len(t.examples), t.name) |
| 430 | 1 SampleNewStyleClass |
| 431 | 1 SampleNewStyleClass.__init__ |
| 432 | 1 SampleNewStyleClass.double |
| 433 | 1 SampleNewStyleClass.get |
| 434 | |
| 435 | Finding Tests in Modules |
| 436 | ~~~~~~~~~~~~~~~~~~~~~~~~ |
| 437 | For a module, DocTestFinder will create a test for the class's |
| 438 | docstring, and will recursively explore its contents, including |
| 439 | functions, classes, and the `__test__` dictionary, if it exists: |
| 440 | |
| 441 | >>> # A module |
| 442 | >>> import new |
| 443 | >>> m = new.module('some_module') |
| 444 | >>> def triple(val): |
| 445 | ... ''' |
| 446 | ... >>> print tripple(11) |
| 447 | ... 33 |
| 448 | ... ''' |
| 449 | ... return val*3 |
| 450 | >>> m.__dict__.update({ |
| 451 | ... 'sample_func': sample_func, |
| 452 | ... 'SampleClass': SampleClass, |
| 453 | ... '__doc__': ''' |
| 454 | ... Module docstring. |
| 455 | ... >>> print 'module' |
| 456 | ... module |
| 457 | ... ''', |
| 458 | ... '__test__': { |
| 459 | ... 'd': '>>> print 6\n6\n>>> print 7\n7\n', |
| 460 | ... 'c': triple}}) |
| 461 | |
| 462 | >>> finder = doctest.DocTestFinder() |
| 463 | >>> # Use module=test.test_doctest, to prevent doctest from |
| 464 | >>> # ignoring the objects since they weren't defined in m. |
| 465 | >>> import test.test_doctest |
| 466 | >>> tests = finder.find(m, module=test.test_doctest) |
| 467 | >>> tests.sort() |
| 468 | >>> for t in tests: |
| 469 | ... print '%2s %s' % (len(t.examples), t.name) |
| 470 | 1 some_module |
| 471 | 1 some_module.SampleClass |
| 472 | 3 some_module.SampleClass.NestedClass |
| 473 | 1 some_module.SampleClass.NestedClass.__init__ |
| 474 | 1 some_module.SampleClass.__init__ |
| 475 | 2 some_module.SampleClass.a_classmethod |
| 476 | 1 some_module.SampleClass.a_property |
| 477 | 1 some_module.SampleClass.a_staticmethod |
| 478 | 1 some_module.SampleClass.double |
| 479 | 1 some_module.SampleClass.get |
Tim Peters | c568478 | 2004-09-13 01:07:12 +0000 | [diff] [blame] | 480 | 1 some_module.__test__.c |
| 481 | 2 some_module.__test__.d |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 482 | 1 some_module.sample_func |
| 483 | |
| 484 | Duplicate Removal |
| 485 | ~~~~~~~~~~~~~~~~~ |
| 486 | If a single object is listed twice (under different names), then tests |
| 487 | will only be generated for it once: |
| 488 | |
Tim Peters | f3f5747 | 2004-08-08 06:11:48 +0000 | [diff] [blame] | 489 | >>> from test import doctest_aliases |
Edward Loper | 32ddbf7 | 2004-09-13 05:47:24 +0000 | [diff] [blame] | 490 | >>> tests = excl_empty_finder.find(doctest_aliases) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 491 | >>> tests.sort() |
| 492 | >>> print len(tests) |
| 493 | 2 |
| 494 | >>> print tests[0].name |
Tim Peters | f3f5747 | 2004-08-08 06:11:48 +0000 | [diff] [blame] | 495 | test.doctest_aliases.TwoNames |
| 496 | |
| 497 | TwoNames.f and TwoNames.g are bound to the same object. |
| 498 | We can't guess which will be found in doctest's traversal of |
| 499 | TwoNames.__dict__ first, so we have to allow for either. |
| 500 | |
| 501 | >>> tests[1].name.split('.')[-1] in ['f', 'g'] |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 502 | True |
| 503 | |
| 504 | Filter Functions |
| 505 | ~~~~~~~~~~~~~~~~ |
Tim Peters | f727c6c | 2004-08-08 01:48:59 +0000 | [diff] [blame] | 506 | A filter function can be used to restrict which objects get examined, |
| 507 | but this is temporary, undocumented internal support for testmod's |
| 508 | deprecated isprivate gimmick. |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 509 | |
| 510 | >>> def namefilter(prefix, base): |
| 511 | ... return base.startswith('a_') |
Tim Peters | f727c6c | 2004-08-08 01:48:59 +0000 | [diff] [blame] | 512 | >>> tests = doctest.DocTestFinder(_namefilter=namefilter).find(SampleClass) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 513 | >>> tests.sort() |
| 514 | >>> for t in tests: |
| 515 | ... print '%2s %s' % (len(t.examples), t.name) |
| 516 | 1 SampleClass |
| 517 | 3 SampleClass.NestedClass |
| 518 | 1 SampleClass.NestedClass.__init__ |
Tim Peters | 958cc89 | 2004-09-13 14:53:28 +0000 | [diff] [blame^] | 519 | 1 SampleClass.__init__ |
| 520 | 1 SampleClass.double |
| 521 | 1 SampleClass.get |
| 522 | |
| 523 | By default, that excluded objects with no doctests. exclude_empty=False |
| 524 | tells it to include (empty) tests for objects with no doctests. This feature |
| 525 | is really to support backward compatibility in what doctest.master.summarize() |
| 526 | displays. |
| 527 | |
| 528 | >>> tests = doctest.DocTestFinder(_namefilter=namefilter, |
| 529 | ... exclude_empty=False).find(SampleClass) |
| 530 | >>> tests.sort() |
| 531 | >>> for t in tests: |
| 532 | ... print '%2s %s' % (len(t.examples), t.name) |
| 533 | 1 SampleClass |
| 534 | 3 SampleClass.NestedClass |
| 535 | 1 SampleClass.NestedClass.__init__ |
Edward Loper | 32ddbf7 | 2004-09-13 05:47:24 +0000 | [diff] [blame] | 536 | 0 SampleClass.NestedClass.get |
| 537 | 0 SampleClass.NestedClass.square |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 538 | 1 SampleClass.__init__ |
| 539 | 1 SampleClass.double |
| 540 | 1 SampleClass.get |
| 541 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 542 | If a given object is filtered out, then none of the objects that it |
| 543 | contains will be added either: |
| 544 | |
| 545 | >>> def namefilter(prefix, base): |
| 546 | ... return base == 'NestedClass' |
Tim Peters | f727c6c | 2004-08-08 01:48:59 +0000 | [diff] [blame] | 547 | >>> tests = doctest.DocTestFinder(_namefilter=namefilter).find(SampleClass) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 548 | >>> tests.sort() |
| 549 | >>> for t in tests: |
| 550 | ... print '%2s %s' % (len(t.examples), t.name) |
| 551 | 1 SampleClass |
| 552 | 1 SampleClass.__init__ |
| 553 | 2 SampleClass.a_classmethod |
| 554 | 1 SampleClass.a_property |
| 555 | 1 SampleClass.a_staticmethod |
| 556 | 1 SampleClass.double |
| 557 | 1 SampleClass.get |
| 558 | |
Tim Peters | f727c6c | 2004-08-08 01:48:59 +0000 | [diff] [blame] | 559 | The filter function apply to contained objects, and *not* to the |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 560 | object explicitly passed to DocTestFinder: |
| 561 | |
| 562 | >>> def namefilter(prefix, base): |
| 563 | ... return base == 'SampleClass' |
Tim Peters | f727c6c | 2004-08-08 01:48:59 +0000 | [diff] [blame] | 564 | >>> tests = doctest.DocTestFinder(_namefilter=namefilter).find(SampleClass) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 565 | >>> len(tests) |
Tim Peters | 958cc89 | 2004-09-13 14:53:28 +0000 | [diff] [blame^] | 566 | 9 |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 567 | |
| 568 | Turning off Recursion |
| 569 | ~~~~~~~~~~~~~~~~~~~~~ |
| 570 | DocTestFinder can be told not to look for tests in contained objects |
| 571 | using the `recurse` flag: |
| 572 | |
| 573 | >>> tests = doctest.DocTestFinder(recurse=False).find(SampleClass) |
| 574 | >>> tests.sort() |
| 575 | >>> for t in tests: |
| 576 | ... print '%2s %s' % (len(t.examples), t.name) |
| 577 | 1 SampleClass |
Edward Loper | b51b234 | 2004-08-17 16:37:12 +0000 | [diff] [blame] | 578 | |
| 579 | Line numbers |
| 580 | ~~~~~~~~~~~~ |
| 581 | DocTestFinder finds the line number of each example: |
| 582 | |
| 583 | >>> def f(x): |
| 584 | ... ''' |
| 585 | ... >>> x = 12 |
| 586 | ... |
| 587 | ... some text |
| 588 | ... |
| 589 | ... >>> # examples are not created for comments & bare prompts. |
| 590 | ... >>> |
| 591 | ... ... |
| 592 | ... |
| 593 | ... >>> for x in range(10): |
| 594 | ... ... print x, |
| 595 | ... 0 1 2 3 4 5 6 7 8 9 |
| 596 | ... >>> x/2 |
| 597 | ... 6 |
| 598 | ... ''' |
| 599 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 600 | >>> [e.lineno for e in test.examples] |
| 601 | [1, 9, 12] |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 602 | """ |
| 603 | |
Edward Loper | 00f8da7 | 2004-08-26 18:05:07 +0000 | [diff] [blame] | 604 | def test_DocTestParser(): r""" |
| 605 | Unit tests for the `DocTestParser` class. |
| 606 | |
| 607 | DocTestParser is used to parse docstrings containing doctest examples. |
| 608 | |
| 609 | The `parse` method divides a docstring into examples and intervening |
| 610 | text: |
| 611 | |
| 612 | >>> s = ''' |
| 613 | ... >>> x, y = 2, 3 # no output expected |
| 614 | ... >>> if 1: |
| 615 | ... ... print x |
| 616 | ... ... print y |
| 617 | ... 2 |
| 618 | ... 3 |
| 619 | ... |
| 620 | ... Some text. |
| 621 | ... >>> x+y |
| 622 | ... 5 |
| 623 | ... ''' |
| 624 | >>> parser = doctest.DocTestParser() |
| 625 | >>> for piece in parser.parse(s): |
| 626 | ... if isinstance(piece, doctest.Example): |
| 627 | ... print 'Example:', (piece.source, piece.want, piece.lineno) |
| 628 | ... else: |
| 629 | ... print ' Text:', `piece` |
| 630 | Text: '\n' |
| 631 | Example: ('x, y = 2, 3 # no output expected\n', '', 1) |
| 632 | Text: '' |
| 633 | Example: ('if 1:\n print x\n print y\n', '2\n3\n', 2) |
| 634 | Text: '\nSome text.\n' |
| 635 | Example: ('x+y\n', '5\n', 9) |
| 636 | Text: '' |
| 637 | |
| 638 | The `get_examples` method returns just the examples: |
| 639 | |
| 640 | >>> for piece in parser.get_examples(s): |
| 641 | ... print (piece.source, piece.want, piece.lineno) |
| 642 | ('x, y = 2, 3 # no output expected\n', '', 1) |
| 643 | ('if 1:\n print x\n print y\n', '2\n3\n', 2) |
| 644 | ('x+y\n', '5\n', 9) |
| 645 | |
| 646 | The `get_doctest` method creates a Test from the examples, along with the |
| 647 | given arguments: |
| 648 | |
| 649 | >>> test = parser.get_doctest(s, {}, 'name', 'filename', lineno=5) |
| 650 | >>> (test.name, test.filename, test.lineno) |
| 651 | ('name', 'filename', 5) |
| 652 | >>> for piece in test.examples: |
| 653 | ... print (piece.source, piece.want, piece.lineno) |
| 654 | ('x, y = 2, 3 # no output expected\n', '', 1) |
| 655 | ('if 1:\n print x\n print y\n', '2\n3\n', 2) |
| 656 | ('x+y\n', '5\n', 9) |
| 657 | """ |
| 658 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 659 | class test_DocTestRunner: |
| 660 | def basics(): r""" |
| 661 | Unit tests for the `DocTestRunner` class. |
| 662 | |
| 663 | DocTestRunner is used to run DocTest test cases, and to accumulate |
| 664 | statistics. Here's a simple DocTest case we can use: |
| 665 | |
| 666 | >>> def f(x): |
| 667 | ... ''' |
| 668 | ... >>> x = 12 |
| 669 | ... >>> print x |
| 670 | ... 12 |
| 671 | ... >>> x/2 |
| 672 | ... 6 |
| 673 | ... ''' |
| 674 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 675 | |
| 676 | The main DocTestRunner interface is the `run` method, which runs a |
| 677 | given DocTest case in a given namespace (globs). It returns a tuple |
| 678 | `(f,t)`, where `f` is the number of failed tests and `t` is the number |
| 679 | of tried tests. |
| 680 | |
| 681 | >>> doctest.DocTestRunner(verbose=False).run(test) |
| 682 | (0, 3) |
| 683 | |
| 684 | If any example produces incorrect output, then the test runner reports |
| 685 | the failure and proceeds to the next example: |
| 686 | |
| 687 | >>> def f(x): |
| 688 | ... ''' |
| 689 | ... >>> x = 12 |
| 690 | ... >>> print x |
| 691 | ... 14 |
| 692 | ... >>> x/2 |
| 693 | ... 6 |
| 694 | ... ''' |
| 695 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 696 | >>> doctest.DocTestRunner(verbose=True).run(test) |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 697 | ... # doctest: +ELLIPSIS |
Edward Loper | aacf083 | 2004-08-26 01:19:50 +0000 | [diff] [blame] | 698 | Trying: |
| 699 | x = 12 |
| 700 | Expecting nothing |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 701 | ok |
Edward Loper | aacf083 | 2004-08-26 01:19:50 +0000 | [diff] [blame] | 702 | Trying: |
| 703 | print x |
| 704 | Expecting: |
| 705 | 14 |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 706 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 707 | File ..., line 4, in f |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 708 | Failed example: |
| 709 | print x |
| 710 | Expected: |
| 711 | 14 |
| 712 | Got: |
| 713 | 12 |
Edward Loper | aacf083 | 2004-08-26 01:19:50 +0000 | [diff] [blame] | 714 | Trying: |
| 715 | x/2 |
| 716 | Expecting: |
| 717 | 6 |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 718 | ok |
| 719 | (1, 3) |
| 720 | """ |
| 721 | def verbose_flag(): r""" |
| 722 | The `verbose` flag makes the test runner generate more detailed |
| 723 | output: |
| 724 | |
| 725 | >>> def f(x): |
| 726 | ... ''' |
| 727 | ... >>> x = 12 |
| 728 | ... >>> print x |
| 729 | ... 12 |
| 730 | ... >>> x/2 |
| 731 | ... 6 |
| 732 | ... ''' |
| 733 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 734 | |
| 735 | >>> doctest.DocTestRunner(verbose=True).run(test) |
Edward Loper | aacf083 | 2004-08-26 01:19:50 +0000 | [diff] [blame] | 736 | Trying: |
| 737 | x = 12 |
| 738 | Expecting nothing |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 739 | ok |
Edward Loper | aacf083 | 2004-08-26 01:19:50 +0000 | [diff] [blame] | 740 | Trying: |
| 741 | print x |
| 742 | Expecting: |
| 743 | 12 |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 744 | ok |
Edward Loper | aacf083 | 2004-08-26 01:19:50 +0000 | [diff] [blame] | 745 | Trying: |
| 746 | x/2 |
| 747 | Expecting: |
| 748 | 6 |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 749 | ok |
| 750 | (0, 3) |
| 751 | |
| 752 | If the `verbose` flag is unspecified, then the output will be verbose |
| 753 | iff `-v` appears in sys.argv: |
| 754 | |
| 755 | >>> # Save the real sys.argv list. |
| 756 | >>> old_argv = sys.argv |
| 757 | |
| 758 | >>> # If -v does not appear in sys.argv, then output isn't verbose. |
| 759 | >>> sys.argv = ['test'] |
| 760 | >>> doctest.DocTestRunner().run(test) |
| 761 | (0, 3) |
| 762 | |
| 763 | >>> # If -v does appear in sys.argv, then output is verbose. |
| 764 | >>> sys.argv = ['test', '-v'] |
| 765 | >>> doctest.DocTestRunner().run(test) |
Edward Loper | aacf083 | 2004-08-26 01:19:50 +0000 | [diff] [blame] | 766 | Trying: |
| 767 | x = 12 |
| 768 | Expecting nothing |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 769 | ok |
Edward Loper | aacf083 | 2004-08-26 01:19:50 +0000 | [diff] [blame] | 770 | Trying: |
| 771 | print x |
| 772 | Expecting: |
| 773 | 12 |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 774 | ok |
Edward Loper | aacf083 | 2004-08-26 01:19:50 +0000 | [diff] [blame] | 775 | Trying: |
| 776 | x/2 |
| 777 | Expecting: |
| 778 | 6 |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 779 | ok |
| 780 | (0, 3) |
| 781 | |
| 782 | >>> # Restore sys.argv |
| 783 | >>> sys.argv = old_argv |
| 784 | |
| 785 | In the remaining examples, the test runner's verbosity will be |
| 786 | explicitly set, to ensure that the test behavior is consistent. |
| 787 | """ |
| 788 | def exceptions(): r""" |
| 789 | Tests of `DocTestRunner`'s exception handling. |
| 790 | |
| 791 | An expected exception is specified with a traceback message. The |
| 792 | lines between the first line and the type/value may be omitted or |
| 793 | replaced with any other string: |
| 794 | |
| 795 | >>> def f(x): |
| 796 | ... ''' |
| 797 | ... >>> x = 12 |
| 798 | ... >>> print x/0 |
| 799 | ... Traceback (most recent call last): |
| 800 | ... ZeroDivisionError: integer division or modulo by zero |
| 801 | ... ''' |
| 802 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 803 | >>> doctest.DocTestRunner(verbose=False).run(test) |
| 804 | (0, 2) |
| 805 | |
Edward Loper | 19b1958 | 2004-08-25 23:07:03 +0000 | [diff] [blame] | 806 | An example may not generate output before it raises an exception; if |
| 807 | it does, then the traceback message will not be recognized as |
| 808 | signaling an expected exception, so the example will be reported as an |
| 809 | unexpected exception: |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 810 | |
| 811 | >>> def f(x): |
| 812 | ... ''' |
| 813 | ... >>> x = 12 |
| 814 | ... >>> print 'pre-exception output', x/0 |
| 815 | ... pre-exception output |
| 816 | ... Traceback (most recent call last): |
| 817 | ... ZeroDivisionError: integer division or modulo by zero |
| 818 | ... ''' |
| 819 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 820 | >>> doctest.DocTestRunner(verbose=False).run(test) |
Edward Loper | 19b1958 | 2004-08-25 23:07:03 +0000 | [diff] [blame] | 821 | ... # doctest: +ELLIPSIS |
| 822 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 823 | File ..., line 4, in f |
Edward Loper | 19b1958 | 2004-08-25 23:07:03 +0000 | [diff] [blame] | 824 | Failed example: |
| 825 | print 'pre-exception output', x/0 |
| 826 | Exception raised: |
| 827 | ... |
| 828 | ZeroDivisionError: integer division or modulo by zero |
| 829 | (1, 2) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 830 | |
| 831 | Exception messages may contain newlines: |
| 832 | |
| 833 | >>> def f(x): |
| 834 | ... r''' |
| 835 | ... >>> raise ValueError, 'multi\nline\nmessage' |
| 836 | ... Traceback (most recent call last): |
| 837 | ... ValueError: multi |
| 838 | ... line |
| 839 | ... message |
| 840 | ... ''' |
| 841 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 842 | >>> doctest.DocTestRunner(verbose=False).run(test) |
| 843 | (0, 1) |
| 844 | |
| 845 | If an exception is expected, but an exception with the wrong type or |
| 846 | message is raised, then it is reported as a failure: |
| 847 | |
| 848 | >>> def f(x): |
| 849 | ... r''' |
| 850 | ... >>> raise ValueError, 'message' |
| 851 | ... Traceback (most recent call last): |
| 852 | ... ValueError: wrong message |
| 853 | ... ''' |
| 854 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 855 | >>> doctest.DocTestRunner(verbose=False).run(test) |
Edward Loper | 8e4a34b | 2004-08-12 02:34:27 +0000 | [diff] [blame] | 856 | ... # doctest: +ELLIPSIS |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 857 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 858 | File ..., line 3, in f |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 859 | Failed example: |
| 860 | raise ValueError, 'message' |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 861 | Expected: |
| 862 | Traceback (most recent call last): |
| 863 | ValueError: wrong message |
| 864 | Got: |
| 865 | Traceback (most recent call last): |
Edward Loper | 8e4a34b | 2004-08-12 02:34:27 +0000 | [diff] [blame] | 866 | ... |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 867 | ValueError: message |
| 868 | (1, 1) |
| 869 | |
Tim Peters | 1fbf9c5 | 2004-09-04 17:21:02 +0000 | [diff] [blame] | 870 | However, IGNORE_EXCEPTION_DETAIL can be used to allow a mismatch in the |
| 871 | detail: |
| 872 | |
| 873 | >>> def f(x): |
| 874 | ... r''' |
| 875 | ... >>> raise ValueError, 'message' #doctest: +IGNORE_EXCEPTION_DETAIL |
| 876 | ... Traceback (most recent call last): |
| 877 | ... ValueError: wrong message |
| 878 | ... ''' |
| 879 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 880 | >>> doctest.DocTestRunner(verbose=False).run(test) |
| 881 | (0, 1) |
| 882 | |
| 883 | But IGNORE_EXCEPTION_DETAIL does not allow a mismatch in the exception type: |
| 884 | |
| 885 | >>> def f(x): |
| 886 | ... r''' |
| 887 | ... >>> raise ValueError, 'message' #doctest: +IGNORE_EXCEPTION_DETAIL |
| 888 | ... Traceback (most recent call last): |
| 889 | ... TypeError: wrong type |
| 890 | ... ''' |
| 891 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 892 | >>> doctest.DocTestRunner(verbose=False).run(test) |
| 893 | ... # doctest: +ELLIPSIS |
| 894 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 895 | File ..., line 3, in f |
Tim Peters | 1fbf9c5 | 2004-09-04 17:21:02 +0000 | [diff] [blame] | 896 | Failed example: |
| 897 | raise ValueError, 'message' #doctest: +IGNORE_EXCEPTION_DETAIL |
| 898 | Expected: |
| 899 | Traceback (most recent call last): |
| 900 | TypeError: wrong type |
| 901 | Got: |
| 902 | Traceback (most recent call last): |
| 903 | ... |
| 904 | ValueError: message |
| 905 | (1, 1) |
| 906 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 907 | If an exception is raised but not expected, then it is reported as an |
| 908 | unexpected exception: |
| 909 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 910 | >>> def f(x): |
| 911 | ... r''' |
| 912 | ... >>> 1/0 |
| 913 | ... 0 |
| 914 | ... ''' |
| 915 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 916 | >>> doctest.DocTestRunner(verbose=False).run(test) |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 917 | ... # doctest: +ELLIPSIS |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 918 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 919 | File ..., line 3, in f |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 920 | Failed example: |
| 921 | 1/0 |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 922 | Exception raised: |
| 923 | Traceback (most recent call last): |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 924 | ... |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 925 | ZeroDivisionError: integer division or modulo by zero |
| 926 | (1, 1) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 927 | """ |
| 928 | def optionflags(): r""" |
| 929 | Tests of `DocTestRunner`'s option flag handling. |
| 930 | |
| 931 | Several option flags can be used to customize the behavior of the test |
| 932 | runner. These are defined as module constants in doctest, and passed |
| 933 | to the DocTestRunner constructor (multiple constants should be or-ed |
| 934 | together). |
| 935 | |
| 936 | The DONT_ACCEPT_TRUE_FOR_1 flag disables matches between True/False |
| 937 | and 1/0: |
| 938 | |
| 939 | >>> def f(x): |
| 940 | ... '>>> True\n1\n' |
| 941 | |
| 942 | >>> # Without the flag: |
| 943 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 944 | >>> doctest.DocTestRunner(verbose=False).run(test) |
| 945 | (0, 1) |
| 946 | |
| 947 | >>> # With the flag: |
| 948 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 949 | >>> flags = doctest.DONT_ACCEPT_TRUE_FOR_1 |
| 950 | >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test) |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 951 | ... # doctest: +ELLIPSIS |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 952 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 953 | File ..., line 2, in f |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 954 | Failed example: |
| 955 | True |
| 956 | Expected: |
| 957 | 1 |
| 958 | Got: |
| 959 | True |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 960 | (1, 1) |
| 961 | |
| 962 | The DONT_ACCEPT_BLANKLINE flag disables the match between blank lines |
| 963 | and the '<BLANKLINE>' marker: |
| 964 | |
| 965 | >>> def f(x): |
| 966 | ... '>>> print "a\\n\\nb"\na\n<BLANKLINE>\nb\n' |
| 967 | |
| 968 | >>> # Without the flag: |
| 969 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 970 | >>> doctest.DocTestRunner(verbose=False).run(test) |
| 971 | (0, 1) |
| 972 | |
| 973 | >>> # With the flag: |
| 974 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 975 | >>> flags = doctest.DONT_ACCEPT_BLANKLINE |
| 976 | >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test) |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 977 | ... # doctest: +ELLIPSIS |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 978 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 979 | File ..., line 2, in f |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 980 | Failed example: |
| 981 | print "a\n\nb" |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 982 | Expected: |
| 983 | a |
| 984 | <BLANKLINE> |
| 985 | b |
| 986 | Got: |
| 987 | a |
| 988 | <BLANKLINE> |
| 989 | b |
| 990 | (1, 1) |
| 991 | |
| 992 | The NORMALIZE_WHITESPACE flag causes all sequences of whitespace to be |
| 993 | treated as equal: |
| 994 | |
| 995 | >>> def f(x): |
| 996 | ... '>>> print 1, 2, 3\n 1 2\n 3' |
| 997 | |
| 998 | >>> # Without the flag: |
| 999 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1000 | >>> doctest.DocTestRunner(verbose=False).run(test) |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1001 | ... # doctest: +ELLIPSIS |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1002 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1003 | File ..., line 2, in f |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1004 | Failed example: |
| 1005 | print 1, 2, 3 |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1006 | Expected: |
| 1007 | 1 2 |
| 1008 | 3 |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1009 | Got: |
| 1010 | 1 2 3 |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1011 | (1, 1) |
| 1012 | |
| 1013 | >>> # With the flag: |
| 1014 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1015 | >>> flags = doctest.NORMALIZE_WHITESPACE |
| 1016 | >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test) |
| 1017 | (0, 1) |
| 1018 | |
Tim Peters | 026f8dc | 2004-08-19 16:38:58 +0000 | [diff] [blame] | 1019 | An example from the docs: |
| 1020 | >>> print range(20) #doctest: +NORMALIZE_WHITESPACE |
| 1021 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, |
| 1022 | 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] |
| 1023 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1024 | The ELLIPSIS flag causes ellipsis marker ("...") in the expected |
| 1025 | output to match any substring in the actual output: |
| 1026 | |
| 1027 | >>> def f(x): |
| 1028 | ... '>>> print range(15)\n[0, 1, 2, ..., 14]\n' |
| 1029 | |
| 1030 | >>> # Without the flag: |
| 1031 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1032 | >>> doctest.DocTestRunner(verbose=False).run(test) |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1033 | ... # doctest: +ELLIPSIS |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1034 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1035 | File ..., line 2, in f |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1036 | Failed example: |
| 1037 | print range(15) |
| 1038 | Expected: |
| 1039 | [0, 1, 2, ..., 14] |
| 1040 | Got: |
| 1041 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1042 | (1, 1) |
| 1043 | |
| 1044 | >>> # With the flag: |
| 1045 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1046 | >>> flags = doctest.ELLIPSIS |
| 1047 | >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test) |
| 1048 | (0, 1) |
| 1049 | |
Tim Peters | e594bee | 2004-08-22 01:47:51 +0000 | [diff] [blame] | 1050 | ... also matches nothing: |
Tim Peters | 1cf3aa6 | 2004-08-19 06:49:33 +0000 | [diff] [blame] | 1051 | |
| 1052 | >>> for i in range(100): |
Tim Peters | e594bee | 2004-08-22 01:47:51 +0000 | [diff] [blame] | 1053 | ... print i**2, #doctest: +ELLIPSIS |
| 1054 | 0 1...4...9 16 ... 36 49 64 ... 9801 |
Tim Peters | 1cf3aa6 | 2004-08-19 06:49:33 +0000 | [diff] [blame] | 1055 | |
Tim Peters | 026f8dc | 2004-08-19 16:38:58 +0000 | [diff] [blame] | 1056 | ... can be surprising; e.g., this test passes: |
Tim Peters | 26b3ebb | 2004-08-19 08:10:08 +0000 | [diff] [blame] | 1057 | |
| 1058 | >>> for i in range(21): #doctest: +ELLIPSIS |
Tim Peters | e594bee | 2004-08-22 01:47:51 +0000 | [diff] [blame] | 1059 | ... print i, |
| 1060 | 0 1 2 ...1...2...0 |
Tim Peters | 26b3ebb | 2004-08-19 08:10:08 +0000 | [diff] [blame] | 1061 | |
Tim Peters | 026f8dc | 2004-08-19 16:38:58 +0000 | [diff] [blame] | 1062 | Examples from the docs: |
| 1063 | |
| 1064 | >>> print range(20) # doctest:+ELLIPSIS |
| 1065 | [0, 1, ..., 18, 19] |
| 1066 | |
| 1067 | >>> print range(20) # doctest: +ELLIPSIS |
| 1068 | ... # doctest: +NORMALIZE_WHITESPACE |
| 1069 | [0, 1, ..., 18, 19] |
| 1070 | |
Edward Loper | 71f55af | 2004-08-26 01:41:51 +0000 | [diff] [blame] | 1071 | The REPORT_UDIFF flag causes failures that involve multi-line expected |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1072 | and actual outputs to be displayed using a unified diff: |
| 1073 | |
| 1074 | >>> def f(x): |
| 1075 | ... r''' |
| 1076 | ... >>> print '\n'.join('abcdefg') |
| 1077 | ... a |
| 1078 | ... B |
| 1079 | ... c |
| 1080 | ... d |
| 1081 | ... f |
| 1082 | ... g |
| 1083 | ... h |
| 1084 | ... ''' |
| 1085 | |
| 1086 | >>> # Without the flag: |
| 1087 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1088 | >>> doctest.DocTestRunner(verbose=False).run(test) |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1089 | ... # doctest: +ELLIPSIS |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1090 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1091 | File ..., line 3, in f |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1092 | Failed example: |
| 1093 | print '\n'.join('abcdefg') |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1094 | Expected: |
| 1095 | a |
| 1096 | B |
| 1097 | c |
| 1098 | d |
| 1099 | f |
| 1100 | g |
| 1101 | h |
| 1102 | Got: |
| 1103 | a |
| 1104 | b |
| 1105 | c |
| 1106 | d |
| 1107 | e |
| 1108 | f |
| 1109 | g |
| 1110 | (1, 1) |
| 1111 | |
| 1112 | >>> # With the flag: |
| 1113 | >>> test = doctest.DocTestFinder().find(f)[0] |
Edward Loper | 71f55af | 2004-08-26 01:41:51 +0000 | [diff] [blame] | 1114 | >>> flags = doctest.REPORT_UDIFF |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1115 | >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test) |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1116 | ... # doctest: +ELLIPSIS |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1117 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1118 | File ..., line 3, in f |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1119 | Failed example: |
| 1120 | print '\n'.join('abcdefg') |
Edward Loper | 5662929 | 2004-08-26 01:31:56 +0000 | [diff] [blame] | 1121 | Differences (unified diff with -expected +actual): |
Tim Peters | e7edcb8 | 2004-08-26 05:44:27 +0000 | [diff] [blame] | 1122 | @@ -1,7 +1,7 @@ |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1123 | a |
| 1124 | -B |
| 1125 | +b |
| 1126 | c |
| 1127 | d |
| 1128 | +e |
| 1129 | f |
| 1130 | g |
| 1131 | -h |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1132 | (1, 1) |
| 1133 | |
Edward Loper | 71f55af | 2004-08-26 01:41:51 +0000 | [diff] [blame] | 1134 | The REPORT_CDIFF flag causes failures that involve multi-line expected |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1135 | and actual outputs to be displayed using a context diff: |
| 1136 | |
Edward Loper | 71f55af | 2004-08-26 01:41:51 +0000 | [diff] [blame] | 1137 | >>> # Reuse f() from the REPORT_UDIFF example, above. |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1138 | >>> test = doctest.DocTestFinder().find(f)[0] |
Edward Loper | 71f55af | 2004-08-26 01:41:51 +0000 | [diff] [blame] | 1139 | >>> flags = doctest.REPORT_CDIFF |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1140 | >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test) |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1141 | ... # doctest: +ELLIPSIS |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1142 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1143 | File ..., line 3, in f |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1144 | Failed example: |
| 1145 | print '\n'.join('abcdefg') |
Edward Loper | 5662929 | 2004-08-26 01:31:56 +0000 | [diff] [blame] | 1146 | Differences (context diff with expected followed by actual): |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1147 | *************** |
Tim Peters | e7edcb8 | 2004-08-26 05:44:27 +0000 | [diff] [blame] | 1148 | *** 1,7 **** |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1149 | a |
| 1150 | ! B |
| 1151 | c |
| 1152 | d |
| 1153 | f |
| 1154 | g |
| 1155 | - h |
Tim Peters | e7edcb8 | 2004-08-26 05:44:27 +0000 | [diff] [blame] | 1156 | --- 1,7 ---- |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1157 | a |
| 1158 | ! b |
| 1159 | c |
| 1160 | d |
| 1161 | + e |
| 1162 | f |
| 1163 | g |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1164 | (1, 1) |
Tim Peters | c6cbab0 | 2004-08-22 19:43:28 +0000 | [diff] [blame] | 1165 | |
| 1166 | |
Edward Loper | 71f55af | 2004-08-26 01:41:51 +0000 | [diff] [blame] | 1167 | The REPORT_NDIFF flag causes failures to use the difflib.Differ algorithm |
Tim Peters | c6cbab0 | 2004-08-22 19:43:28 +0000 | [diff] [blame] | 1168 | used by the popular ndiff.py utility. This does intraline difference |
| 1169 | marking, as well as interline differences. |
| 1170 | |
| 1171 | >>> def f(x): |
| 1172 | ... r''' |
| 1173 | ... >>> print "a b c d e f g h i j k l m" |
| 1174 | ... a b c d e f g h i j k 1 m |
| 1175 | ... ''' |
| 1176 | >>> test = doctest.DocTestFinder().find(f)[0] |
Edward Loper | 71f55af | 2004-08-26 01:41:51 +0000 | [diff] [blame] | 1177 | >>> flags = doctest.REPORT_NDIFF |
Tim Peters | c6cbab0 | 2004-08-22 19:43:28 +0000 | [diff] [blame] | 1178 | >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test) |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1179 | ... # doctest: +ELLIPSIS |
Tim Peters | c6cbab0 | 2004-08-22 19:43:28 +0000 | [diff] [blame] | 1180 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1181 | File ..., line 3, in f |
Tim Peters | c6cbab0 | 2004-08-22 19:43:28 +0000 | [diff] [blame] | 1182 | Failed example: |
| 1183 | print "a b c d e f g h i j k l m" |
| 1184 | Differences (ndiff with -expected +actual): |
| 1185 | - a b c d e f g h i j k 1 m |
| 1186 | ? ^ |
| 1187 | + a b c d e f g h i j k l m |
| 1188 | ? + ++ ^ |
Tim Peters | c6cbab0 | 2004-08-22 19:43:28 +0000 | [diff] [blame] | 1189 | (1, 1) |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1190 | |
| 1191 | The REPORT_ONLY_FIRST_FAILURE supresses result output after the first |
| 1192 | failing example: |
| 1193 | |
| 1194 | >>> def f(x): |
| 1195 | ... r''' |
| 1196 | ... >>> print 1 # first success |
| 1197 | ... 1 |
| 1198 | ... >>> print 2 # first failure |
| 1199 | ... 200 |
| 1200 | ... >>> print 3 # second failure |
| 1201 | ... 300 |
| 1202 | ... >>> print 4 # second success |
| 1203 | ... 4 |
| 1204 | ... >>> print 5 # third failure |
| 1205 | ... 500 |
| 1206 | ... ''' |
| 1207 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1208 | >>> flags = doctest.REPORT_ONLY_FIRST_FAILURE |
| 1209 | >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test) |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1210 | ... # doctest: +ELLIPSIS |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1211 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1212 | File ..., line 5, in f |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1213 | Failed example: |
| 1214 | print 2 # first failure |
| 1215 | Expected: |
| 1216 | 200 |
| 1217 | Got: |
| 1218 | 2 |
| 1219 | (3, 5) |
| 1220 | |
| 1221 | However, output from `report_start` is not supressed: |
| 1222 | |
| 1223 | >>> doctest.DocTestRunner(verbose=True, optionflags=flags).run(test) |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1224 | ... # doctest: +ELLIPSIS |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1225 | Trying: |
| 1226 | print 1 # first success |
| 1227 | Expecting: |
| 1228 | 1 |
| 1229 | ok |
| 1230 | Trying: |
| 1231 | print 2 # first failure |
| 1232 | Expecting: |
| 1233 | 200 |
| 1234 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1235 | File ..., line 5, in f |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1236 | Failed example: |
| 1237 | print 2 # first failure |
| 1238 | Expected: |
| 1239 | 200 |
| 1240 | Got: |
| 1241 | 2 |
| 1242 | (3, 5) |
| 1243 | |
| 1244 | For the purposes of REPORT_ONLY_FIRST_FAILURE, unexpected exceptions |
| 1245 | count as failures: |
| 1246 | |
| 1247 | >>> def f(x): |
| 1248 | ... r''' |
| 1249 | ... >>> print 1 # first success |
| 1250 | ... 1 |
| 1251 | ... >>> raise ValueError(2) # first failure |
| 1252 | ... 200 |
| 1253 | ... >>> print 3 # second failure |
| 1254 | ... 300 |
| 1255 | ... >>> print 4 # second success |
| 1256 | ... 4 |
| 1257 | ... >>> print 5 # third failure |
| 1258 | ... 500 |
| 1259 | ... ''' |
| 1260 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1261 | >>> flags = doctest.REPORT_ONLY_FIRST_FAILURE |
| 1262 | >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test) |
| 1263 | ... # doctest: +ELLIPSIS |
| 1264 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1265 | File ..., line 5, in f |
Edward Loper | a89f88d | 2004-08-26 02:45:51 +0000 | [diff] [blame] | 1266 | Failed example: |
| 1267 | raise ValueError(2) # first failure |
| 1268 | Exception raised: |
| 1269 | ... |
| 1270 | ValueError: 2 |
| 1271 | (3, 5) |
| 1272 | |
Tim Peters | c6cbab0 | 2004-08-22 19:43:28 +0000 | [diff] [blame] | 1273 | """ |
| 1274 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1275 | def option_directives(): r""" |
| 1276 | Tests of `DocTestRunner`'s option directive mechanism. |
| 1277 | |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1278 | Option directives can be used to turn option flags on or off for a |
| 1279 | single example. To turn an option on for an example, follow that |
| 1280 | example with a comment of the form ``# doctest: +OPTION``: |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1281 | |
| 1282 | >>> def f(x): r''' |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1283 | ... >>> print range(10) # should fail: no ellipsis |
| 1284 | ... [0, 1, ..., 9] |
| 1285 | ... |
| 1286 | ... >>> print range(10) # doctest: +ELLIPSIS |
| 1287 | ... [0, 1, ..., 9] |
| 1288 | ... ''' |
| 1289 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1290 | >>> doctest.DocTestRunner(verbose=False).run(test) |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1291 | ... # doctest: +ELLIPSIS |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1292 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1293 | File ..., line 2, in f |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1294 | Failed example: |
| 1295 | print range(10) # should fail: no ellipsis |
| 1296 | Expected: |
| 1297 | [0, 1, ..., 9] |
| 1298 | Got: |
| 1299 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1300 | (1, 2) |
| 1301 | |
| 1302 | To turn an option off for an example, follow that example with a |
| 1303 | comment of the form ``# doctest: -OPTION``: |
| 1304 | |
| 1305 | >>> def f(x): r''' |
| 1306 | ... >>> print range(10) |
| 1307 | ... [0, 1, ..., 9] |
| 1308 | ... |
| 1309 | ... >>> # should fail: no ellipsis |
| 1310 | ... >>> print range(10) # doctest: -ELLIPSIS |
| 1311 | ... [0, 1, ..., 9] |
| 1312 | ... ''' |
| 1313 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1314 | >>> doctest.DocTestRunner(verbose=False, |
| 1315 | ... optionflags=doctest.ELLIPSIS).run(test) |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1316 | ... # doctest: +ELLIPSIS |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1317 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1318 | File ..., line 6, in f |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1319 | Failed example: |
| 1320 | print range(10) # doctest: -ELLIPSIS |
| 1321 | Expected: |
| 1322 | [0, 1, ..., 9] |
| 1323 | Got: |
| 1324 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1325 | (1, 2) |
| 1326 | |
| 1327 | Option directives affect only the example that they appear with; they |
| 1328 | do not change the options for surrounding examples: |
Edward Loper | 8e4a34b | 2004-08-12 02:34:27 +0000 | [diff] [blame] | 1329 | |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1330 | >>> def f(x): r''' |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1331 | ... >>> print range(10) # Should fail: no ellipsis |
| 1332 | ... [0, 1, ..., 9] |
| 1333 | ... |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1334 | ... >>> print range(10) # doctest: +ELLIPSIS |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1335 | ... [0, 1, ..., 9] |
| 1336 | ... |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1337 | ... >>> print range(10) # Should fail: no ellipsis |
| 1338 | ... [0, 1, ..., 9] |
| 1339 | ... ''' |
| 1340 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1341 | >>> doctest.DocTestRunner(verbose=False).run(test) |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1342 | ... # doctest: +ELLIPSIS |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1343 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1344 | File ..., line 2, in f |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1345 | Failed example: |
| 1346 | print range(10) # Should fail: no ellipsis |
| 1347 | Expected: |
| 1348 | [0, 1, ..., 9] |
| 1349 | Got: |
| 1350 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1351 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1352 | File ..., line 8, in f |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1353 | Failed example: |
| 1354 | print range(10) # Should fail: no ellipsis |
| 1355 | Expected: |
| 1356 | [0, 1, ..., 9] |
| 1357 | Got: |
| 1358 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1359 | (2, 3) |
| 1360 | |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1361 | Multiple options may be modified by a single option directive. They |
| 1362 | may be separated by whitespace, commas, or both: |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1363 | |
| 1364 | >>> def f(x): r''' |
| 1365 | ... >>> print range(10) # Should fail |
| 1366 | ... [0, 1, ..., 9] |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1367 | ... >>> print range(10) # Should succeed |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1368 | ... ... # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1369 | ... [0, 1, ..., 9] |
| 1370 | ... ''' |
| 1371 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1372 | >>> doctest.DocTestRunner(verbose=False).run(test) |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1373 | ... # doctest: +ELLIPSIS |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1374 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1375 | File ..., line 2, in f |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1376 | Failed example: |
| 1377 | print range(10) # Should fail |
| 1378 | Expected: |
| 1379 | [0, 1, ..., 9] |
| 1380 | Got: |
| 1381 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1382 | (1, 2) |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1383 | |
| 1384 | >>> def f(x): r''' |
| 1385 | ... >>> print range(10) # Should fail |
| 1386 | ... [0, 1, ..., 9] |
| 1387 | ... >>> print range(10) # Should succeed |
| 1388 | ... ... # doctest: +ELLIPSIS,+NORMALIZE_WHITESPACE |
| 1389 | ... [0, 1, ..., 9] |
| 1390 | ... ''' |
| 1391 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1392 | >>> doctest.DocTestRunner(verbose=False).run(test) |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1393 | ... # doctest: +ELLIPSIS |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1394 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1395 | File ..., line 2, in f |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1396 | Failed example: |
| 1397 | print range(10) # Should fail |
| 1398 | Expected: |
| 1399 | [0, 1, ..., 9] |
| 1400 | Got: |
| 1401 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1402 | (1, 2) |
| 1403 | |
| 1404 | >>> def f(x): r''' |
| 1405 | ... >>> print range(10) # Should fail |
| 1406 | ... [0, 1, ..., 9] |
| 1407 | ... >>> print range(10) # Should succeed |
| 1408 | ... ... # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE |
| 1409 | ... [0, 1, ..., 9] |
| 1410 | ... ''' |
| 1411 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1412 | >>> doctest.DocTestRunner(verbose=False).run(test) |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1413 | ... # doctest: +ELLIPSIS |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1414 | ********************************************************************** |
Tim Peters | 17b5637 | 2004-09-11 17:33:27 +0000 | [diff] [blame] | 1415 | File ..., line 2, in f |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1416 | Failed example: |
| 1417 | print range(10) # Should fail |
| 1418 | Expected: |
| 1419 | [0, 1, ..., 9] |
| 1420 | Got: |
| 1421 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1422 | (1, 2) |
| 1423 | |
| 1424 | The option directive may be put on the line following the source, as |
| 1425 | long as a continuation prompt is used: |
| 1426 | |
| 1427 | >>> def f(x): r''' |
| 1428 | ... >>> print range(10) |
| 1429 | ... ... # doctest: +ELLIPSIS |
| 1430 | ... [0, 1, ..., 9] |
| 1431 | ... ''' |
| 1432 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1433 | >>> doctest.DocTestRunner(verbose=False).run(test) |
| 1434 | (0, 1) |
Edward Loper | 8e4a34b | 2004-08-12 02:34:27 +0000 | [diff] [blame] | 1435 | |
Edward Loper | 74bca7a | 2004-08-12 02:27:44 +0000 | [diff] [blame] | 1436 | For examples with multi-line source, the option directive may appear |
| 1437 | at the end of any line: |
| 1438 | |
| 1439 | >>> def f(x): r''' |
| 1440 | ... >>> for x in range(10): # doctest: +ELLIPSIS |
| 1441 | ... ... print x, |
| 1442 | ... 0 1 2 ... 9 |
| 1443 | ... |
| 1444 | ... >>> for x in range(10): |
| 1445 | ... ... print x, # doctest: +ELLIPSIS |
| 1446 | ... 0 1 2 ... 9 |
| 1447 | ... ''' |
| 1448 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1449 | >>> doctest.DocTestRunner(verbose=False).run(test) |
| 1450 | (0, 2) |
| 1451 | |
| 1452 | If more than one line of an example with multi-line source has an |
| 1453 | option directive, then they are combined: |
| 1454 | |
| 1455 | >>> def f(x): r''' |
| 1456 | ... Should fail (option directive not on the last line): |
| 1457 | ... >>> for x in range(10): # doctest: +ELLIPSIS |
| 1458 | ... ... print x, # doctest: +NORMALIZE_WHITESPACE |
| 1459 | ... 0 1 2...9 |
| 1460 | ... ''' |
| 1461 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 1462 | >>> doctest.DocTestRunner(verbose=False).run(test) |
| 1463 | (0, 1) |
| 1464 | |
| 1465 | It is an error to have a comment of the form ``# doctest:`` that is |
| 1466 | *not* followed by words of the form ``+OPTION`` or ``-OPTION``, where |
| 1467 | ``OPTION`` is an option that has been registered with |
| 1468 | `register_option`: |
| 1469 | |
| 1470 | >>> # Error: Option not registered |
| 1471 | >>> s = '>>> print 12 #doctest: +BADOPTION' |
| 1472 | >>> test = doctest.DocTestParser().get_doctest(s, {}, 's', 's.py', 0) |
| 1473 | Traceback (most recent call last): |
| 1474 | ValueError: line 1 of the doctest for s has an invalid option: '+BADOPTION' |
| 1475 | |
| 1476 | >>> # Error: No + or - prefix |
| 1477 | >>> s = '>>> print 12 #doctest: ELLIPSIS' |
| 1478 | >>> test = doctest.DocTestParser().get_doctest(s, {}, 's', 's.py', 0) |
| 1479 | Traceback (most recent call last): |
| 1480 | ValueError: line 1 of the doctest for s has an invalid option: 'ELLIPSIS' |
| 1481 | |
| 1482 | It is an error to use an option directive on a line that contains no |
| 1483 | source: |
| 1484 | |
| 1485 | >>> s = '>>> # doctest: +ELLIPSIS' |
| 1486 | >>> test = doctest.DocTestParser().get_doctest(s, {}, 's', 's.py', 0) |
| 1487 | Traceback (most recent call last): |
| 1488 | ValueError: line 0 of the doctest for s has an option directive on a line with no example: '# doctest: +ELLIPSIS' |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1489 | """ |
| 1490 | |
| 1491 | def test_testsource(): r""" |
| 1492 | Unit tests for `testsource()`. |
| 1493 | |
| 1494 | The testsource() function takes a module and a name, finds the (first) |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1495 | test with that name in that module, and converts it to a script. The |
| 1496 | example code is converted to regular Python code. The surrounding |
| 1497 | words and expected output are converted to comments: |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1498 | |
| 1499 | >>> import test.test_doctest |
| 1500 | >>> name = 'test.test_doctest.sample_func' |
| 1501 | >>> print doctest.testsource(test.test_doctest, name) |
Edward Loper | a5db600 | 2004-08-12 02:41:30 +0000 | [diff] [blame] | 1502 | # Blah blah |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1503 | # |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1504 | print sample_func(22) |
| 1505 | # Expected: |
Edward Loper | a5db600 | 2004-08-12 02:41:30 +0000 | [diff] [blame] | 1506 | ## 44 |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1507 | # |
Edward Loper | a5db600 | 2004-08-12 02:41:30 +0000 | [diff] [blame] | 1508 | # Yee ha! |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1509 | |
| 1510 | >>> name = 'test.test_doctest.SampleNewStyleClass' |
| 1511 | >>> print doctest.testsource(test.test_doctest, name) |
| 1512 | print '1\n2\n3' |
| 1513 | # Expected: |
Edward Loper | a5db600 | 2004-08-12 02:41:30 +0000 | [diff] [blame] | 1514 | ## 1 |
| 1515 | ## 2 |
| 1516 | ## 3 |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1517 | |
| 1518 | >>> name = 'test.test_doctest.SampleClass.a_classmethod' |
| 1519 | >>> print doctest.testsource(test.test_doctest, name) |
| 1520 | print SampleClass.a_classmethod(10) |
| 1521 | # Expected: |
Edward Loper | a5db600 | 2004-08-12 02:41:30 +0000 | [diff] [blame] | 1522 | ## 12 |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1523 | print SampleClass(0).a_classmethod(10) |
| 1524 | # Expected: |
Edward Loper | a5db600 | 2004-08-12 02:41:30 +0000 | [diff] [blame] | 1525 | ## 12 |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1526 | """ |
| 1527 | |
| 1528 | def test_debug(): r""" |
| 1529 | |
| 1530 | Create a docstring that we want to debug: |
| 1531 | |
| 1532 | >>> s = ''' |
| 1533 | ... >>> x = 12 |
| 1534 | ... >>> print x |
| 1535 | ... 12 |
| 1536 | ... ''' |
| 1537 | |
| 1538 | Create some fake stdin input, to feed to the debugger: |
| 1539 | |
| 1540 | >>> import tempfile |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1541 | >>> real_stdin = sys.stdin |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 1542 | >>> sys.stdin = _FakeInput(['next', 'print x', 'continue']) |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1543 | |
| 1544 | Run the debugger on the docstring, and then restore sys.stdin. |
| 1545 | |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 1546 | >>> try: doctest.debug_src(s) |
| 1547 | ... finally: sys.stdin = real_stdin |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1548 | > <string>(1)?() |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 1549 | (Pdb) next |
| 1550 | 12 |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1551 | --Return-- |
| 1552 | > <string>(1)?()->None |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 1553 | (Pdb) print x |
| 1554 | 12 |
| 1555 | (Pdb) continue |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 1556 | |
| 1557 | """ |
| 1558 | |
Jim Fulton | 356fd19 | 2004-08-09 11:34:47 +0000 | [diff] [blame] | 1559 | def test_pdb_set_trace(): |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 1560 | """Using pdb.set_trace from a doctest |
Jim Fulton | 356fd19 | 2004-08-09 11:34:47 +0000 | [diff] [blame] | 1561 | |
Tim Peters | 413ced6 | 2004-08-09 15:43:47 +0000 | [diff] [blame] | 1562 | You can use pdb.set_trace from a doctest. To do so, you must |
Jim Fulton | 356fd19 | 2004-08-09 11:34:47 +0000 | [diff] [blame] | 1563 | retrieve the set_trace function from the pdb module at the time |
Tim Peters | 413ced6 | 2004-08-09 15:43:47 +0000 | [diff] [blame] | 1564 | you use it. The doctest module changes sys.stdout so that it can |
| 1565 | capture program output. It also temporarily replaces pdb.set_trace |
| 1566 | with a version that restores stdout. This is necessary for you to |
Jim Fulton | 356fd19 | 2004-08-09 11:34:47 +0000 | [diff] [blame] | 1567 | see debugger output. |
| 1568 | |
| 1569 | >>> doc = ''' |
| 1570 | ... >>> x = 42 |
| 1571 | ... >>> import pdb; pdb.set_trace() |
| 1572 | ... ''' |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 1573 | >>> parser = doctest.DocTestParser() |
| 1574 | >>> test = parser.get_doctest(doc, {}, "foo", "foo.py", 0) |
Jim Fulton | 356fd19 | 2004-08-09 11:34:47 +0000 | [diff] [blame] | 1575 | >>> runner = doctest.DocTestRunner(verbose=False) |
| 1576 | |
| 1577 | To demonstrate this, we'll create a fake standard input that |
| 1578 | captures our debugger input: |
| 1579 | |
| 1580 | >>> import tempfile |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 1581 | >>> real_stdin = sys.stdin |
| 1582 | >>> sys.stdin = _FakeInput([ |
Jim Fulton | 356fd19 | 2004-08-09 11:34:47 +0000 | [diff] [blame] | 1583 | ... 'print x', # print data defined by the example |
| 1584 | ... 'continue', # stop debugging |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 1585 | ... '']) |
Jim Fulton | 356fd19 | 2004-08-09 11:34:47 +0000 | [diff] [blame] | 1586 | |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 1587 | >>> try: runner.run(test) |
| 1588 | ... finally: sys.stdin = real_stdin |
Jim Fulton | 356fd19 | 2004-08-09 11:34:47 +0000 | [diff] [blame] | 1589 | --Return-- |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 1590 | > <doctest foo[1]>(1)?()->None |
| 1591 | -> import pdb; pdb.set_trace() |
| 1592 | (Pdb) print x |
| 1593 | 42 |
| 1594 | (Pdb) continue |
| 1595 | (0, 2) |
Jim Fulton | 356fd19 | 2004-08-09 11:34:47 +0000 | [diff] [blame] | 1596 | |
| 1597 | You can also put pdb.set_trace in a function called from a test: |
| 1598 | |
| 1599 | >>> def calls_set_trace(): |
| 1600 | ... y=2 |
| 1601 | ... import pdb; pdb.set_trace() |
| 1602 | |
| 1603 | >>> doc = ''' |
| 1604 | ... >>> x=1 |
| 1605 | ... >>> calls_set_trace() |
| 1606 | ... ''' |
Edward Loper | a1ef611 | 2004-08-09 16:14:41 +0000 | [diff] [blame] | 1607 | >>> test = parser.get_doctest(doc, globals(), "foo", "foo.py", 0) |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 1608 | >>> real_stdin = sys.stdin |
| 1609 | >>> sys.stdin = _FakeInput([ |
Jim Fulton | 356fd19 | 2004-08-09 11:34:47 +0000 | [diff] [blame] | 1610 | ... 'print y', # print data defined in the function |
| 1611 | ... 'up', # out of function |
| 1612 | ... 'print x', # print data defined by the example |
| 1613 | ... 'continue', # stop debugging |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 1614 | ... '']) |
Jim Fulton | 356fd19 | 2004-08-09 11:34:47 +0000 | [diff] [blame] | 1615 | |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 1616 | >>> try: runner.run(test) |
| 1617 | ... finally: sys.stdin = real_stdin |
Jim Fulton | 356fd19 | 2004-08-09 11:34:47 +0000 | [diff] [blame] | 1618 | --Return-- |
Edward Loper | 2de91ba | 2004-08-27 02:07:46 +0000 | [diff] [blame] | 1619 | > <doctest test.test_doctest.test_pdb_set_trace[8]>(3)calls_set_trace()->None |
| 1620 | -> import pdb; pdb.set_trace() |
| 1621 | (Pdb) print y |
| 1622 | 2 |
| 1623 | (Pdb) up |
| 1624 | > <doctest foo[1]>(1)?() |
| 1625 | -> calls_set_trace() |
| 1626 | (Pdb) print x |
| 1627 | 1 |
| 1628 | (Pdb) continue |
| 1629 | (0, 2) |
| 1630 | |
| 1631 | During interactive debugging, source code is shown, even for |
| 1632 | doctest examples: |
| 1633 | |
| 1634 | >>> doc = ''' |
| 1635 | ... >>> def f(x): |
| 1636 | ... ... g(x*2) |
| 1637 | ... >>> def g(x): |
| 1638 | ... ... print x+3 |
| 1639 | ... ... import pdb; pdb.set_trace() |
| 1640 | ... >>> f(3) |
| 1641 | ... ''' |
| 1642 | >>> test = parser.get_doctest(doc, globals(), "foo", "foo.py", 0) |
| 1643 | >>> real_stdin = sys.stdin |
| 1644 | >>> sys.stdin = _FakeInput([ |
| 1645 | ... 'list', # list source from example 2 |
| 1646 | ... 'next', # return from g() |
| 1647 | ... 'list', # list source from example 1 |
| 1648 | ... 'next', # return from f() |
| 1649 | ... 'list', # list source from example 3 |
| 1650 | ... 'continue', # stop debugging |
| 1651 | ... '']) |
| 1652 | >>> try: runner.run(test) |
| 1653 | ... finally: sys.stdin = real_stdin |
| 1654 | ... # doctest: +NORMALIZE_WHITESPACE |
| 1655 | --Return-- |
| 1656 | > <doctest foo[1]>(3)g()->None |
| 1657 | -> import pdb; pdb.set_trace() |
| 1658 | (Pdb) list |
| 1659 | 1 def g(x): |
| 1660 | 2 print x+3 |
| 1661 | 3 -> import pdb; pdb.set_trace() |
| 1662 | [EOF] |
| 1663 | (Pdb) next |
| 1664 | --Return-- |
| 1665 | > <doctest foo[0]>(2)f()->None |
| 1666 | -> g(x*2) |
| 1667 | (Pdb) list |
| 1668 | 1 def f(x): |
| 1669 | 2 -> g(x*2) |
| 1670 | [EOF] |
| 1671 | (Pdb) next |
| 1672 | --Return-- |
| 1673 | > <doctest foo[2]>(1)?()->None |
| 1674 | -> f(3) |
| 1675 | (Pdb) list |
| 1676 | 1 -> f(3) |
| 1677 | [EOF] |
| 1678 | (Pdb) continue |
| 1679 | ********************************************************************** |
| 1680 | File "foo.py", line 7, in foo |
| 1681 | Failed example: |
| 1682 | f(3) |
| 1683 | Expected nothing |
| 1684 | Got: |
| 1685 | 9 |
| 1686 | (1, 3) |
Jim Fulton | 356fd19 | 2004-08-09 11:34:47 +0000 | [diff] [blame] | 1687 | """ |
| 1688 | |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1689 | def test_DocTestSuite(): |
Tim Peters | 1e277ee | 2004-08-07 05:37:52 +0000 | [diff] [blame] | 1690 | """DocTestSuite creates a unittest test suite from a doctest. |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1691 | |
| 1692 | We create a Suite by providing a module. A module can be provided |
| 1693 | by passing a module object: |
| 1694 | |
| 1695 | >>> import unittest |
| 1696 | >>> import test.sample_doctest |
| 1697 | >>> suite = doctest.DocTestSuite(test.sample_doctest) |
| 1698 | >>> suite.run(unittest.TestResult()) |
Tim Peters | 1e277ee | 2004-08-07 05:37:52 +0000 | [diff] [blame] | 1699 | <unittest.TestResult run=9 errors=0 failures=4> |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1700 | |
| 1701 | We can also supply the module by name: |
| 1702 | |
| 1703 | >>> suite = doctest.DocTestSuite('test.sample_doctest') |
| 1704 | >>> suite.run(unittest.TestResult()) |
Tim Peters | 1e277ee | 2004-08-07 05:37:52 +0000 | [diff] [blame] | 1705 | <unittest.TestResult run=9 errors=0 failures=4> |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1706 | |
| 1707 | We can use the current module: |
| 1708 | |
| 1709 | >>> suite = test.sample_doctest.test_suite() |
| 1710 | >>> suite.run(unittest.TestResult()) |
Tim Peters | 1e277ee | 2004-08-07 05:37:52 +0000 | [diff] [blame] | 1711 | <unittest.TestResult run=9 errors=0 failures=4> |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1712 | |
| 1713 | We can supply global variables. If we pass globs, they will be |
| 1714 | used instead of the module globals. Here we'll pass an empty |
| 1715 | globals, triggering an extra error: |
| 1716 | |
| 1717 | >>> suite = doctest.DocTestSuite('test.sample_doctest', globs={}) |
| 1718 | >>> suite.run(unittest.TestResult()) |
Tim Peters | 1e277ee | 2004-08-07 05:37:52 +0000 | [diff] [blame] | 1719 | <unittest.TestResult run=9 errors=0 failures=5> |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1720 | |
| 1721 | Alternatively, we can provide extra globals. Here we'll make an |
| 1722 | error go away by providing an extra global variable: |
| 1723 | |
| 1724 | >>> suite = doctest.DocTestSuite('test.sample_doctest', |
| 1725 | ... extraglobs={'y': 1}) |
| 1726 | >>> suite.run(unittest.TestResult()) |
Tim Peters | 1e277ee | 2004-08-07 05:37:52 +0000 | [diff] [blame] | 1727 | <unittest.TestResult run=9 errors=0 failures=3> |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1728 | |
| 1729 | You can pass option flags. Here we'll cause an extra error |
| 1730 | by disabling the blank-line feature: |
| 1731 | |
| 1732 | >>> suite = doctest.DocTestSuite('test.sample_doctest', |
Tim Peters | 1e277ee | 2004-08-07 05:37:52 +0000 | [diff] [blame] | 1733 | ... optionflags=doctest.DONT_ACCEPT_BLANKLINE) |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1734 | >>> suite.run(unittest.TestResult()) |
Tim Peters | 1e277ee | 2004-08-07 05:37:52 +0000 | [diff] [blame] | 1735 | <unittest.TestResult run=9 errors=0 failures=5> |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1736 | |
Tim Peters | 1e277ee | 2004-08-07 05:37:52 +0000 | [diff] [blame] | 1737 | You can supply setUp and tearDown functions: |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1738 | |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 1739 | >>> def setUp(t): |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1740 | ... import test.test_doctest |
| 1741 | ... test.test_doctest.sillySetup = True |
| 1742 | |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 1743 | >>> def tearDown(t): |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1744 | ... import test.test_doctest |
| 1745 | ... del test.test_doctest.sillySetup |
| 1746 | |
| 1747 | Here, we installed a silly variable that the test expects: |
| 1748 | |
| 1749 | >>> suite = doctest.DocTestSuite('test.sample_doctest', |
| 1750 | ... setUp=setUp, tearDown=tearDown) |
| 1751 | >>> suite.run(unittest.TestResult()) |
Tim Peters | 1e277ee | 2004-08-07 05:37:52 +0000 | [diff] [blame] | 1752 | <unittest.TestResult run=9 errors=0 failures=3> |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1753 | |
| 1754 | But the tearDown restores sanity: |
| 1755 | |
| 1756 | >>> import test.test_doctest |
| 1757 | >>> test.test_doctest.sillySetup |
| 1758 | Traceback (most recent call last): |
| 1759 | ... |
| 1760 | AttributeError: 'module' object has no attribute 'sillySetup' |
| 1761 | |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 1762 | The setUp and tearDown funtions are passed test objects. Here |
| 1763 | we'll use the setUp function to supply the missing variable y: |
| 1764 | |
| 1765 | >>> def setUp(test): |
| 1766 | ... test.globs['y'] = 1 |
| 1767 | |
| 1768 | >>> suite = doctest.DocTestSuite('test.sample_doctest', setUp=setUp) |
| 1769 | >>> suite.run(unittest.TestResult()) |
| 1770 | <unittest.TestResult run=9 errors=0 failures=3> |
| 1771 | |
| 1772 | Here, we didn't need to use a tearDown function because we |
| 1773 | modified the test globals, which are a copy of the |
| 1774 | sample_doctest module dictionary. The test globals are |
| 1775 | automatically cleared for us after a test. |
| 1776 | |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1777 | Finally, you can provide an alternate test finder. Here we'll |
Tim Peters | 1e277ee | 2004-08-07 05:37:52 +0000 | [diff] [blame] | 1778 | use a custom test_finder to to run just the test named bar. |
| 1779 | However, the test in the module docstring, and the two tests |
| 1780 | in the module __test__ dict, aren't filtered, so we actually |
| 1781 | run three tests besides bar's. The filtering mechanisms are |
| 1782 | poorly conceived, and will go away someday. |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1783 | |
| 1784 | >>> finder = doctest.DocTestFinder( |
Tim Peters | f727c6c | 2004-08-08 01:48:59 +0000 | [diff] [blame] | 1785 | ... _namefilter=lambda prefix, base: base!='bar') |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1786 | >>> suite = doctest.DocTestSuite('test.sample_doctest', |
| 1787 | ... test_finder=finder) |
| 1788 | >>> suite.run(unittest.TestResult()) |
Tim Peters | 1e277ee | 2004-08-07 05:37:52 +0000 | [diff] [blame] | 1789 | <unittest.TestResult run=4 errors=0 failures=1> |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1790 | """ |
| 1791 | |
| 1792 | def test_DocFileSuite(): |
| 1793 | """We can test tests found in text files using a DocFileSuite. |
| 1794 | |
| 1795 | We create a suite by providing the names of one or more text |
| 1796 | files that include examples: |
| 1797 | |
| 1798 | >>> import unittest |
| 1799 | >>> suite = doctest.DocFileSuite('test_doctest.txt', |
| 1800 | ... 'test_doctest2.txt') |
| 1801 | >>> suite.run(unittest.TestResult()) |
| 1802 | <unittest.TestResult run=2 errors=0 failures=2> |
| 1803 | |
| 1804 | The test files are looked for in the directory containing the |
| 1805 | calling module. A package keyword argument can be provided to |
| 1806 | specify a different relative location. |
| 1807 | |
| 1808 | >>> import unittest |
| 1809 | >>> suite = doctest.DocFileSuite('test_doctest.txt', |
| 1810 | ... 'test_doctest2.txt', |
| 1811 | ... package='test') |
| 1812 | >>> suite.run(unittest.TestResult()) |
| 1813 | <unittest.TestResult run=2 errors=0 failures=2> |
| 1814 | |
| 1815 | Note that '/' should be used as a path separator. It will be |
| 1816 | converted to a native separator at run time: |
| 1817 | |
| 1818 | |
| 1819 | >>> suite = doctest.DocFileSuite('../test/test_doctest.txt') |
| 1820 | >>> suite.run(unittest.TestResult()) |
| 1821 | <unittest.TestResult run=1 errors=0 failures=1> |
| 1822 | |
| 1823 | You can specify initial global variables: |
| 1824 | |
| 1825 | >>> suite = doctest.DocFileSuite('test_doctest.txt', |
| 1826 | ... 'test_doctest2.txt', |
| 1827 | ... globs={'favorite_color': 'blue'}) |
| 1828 | >>> suite.run(unittest.TestResult()) |
| 1829 | <unittest.TestResult run=2 errors=0 failures=1> |
| 1830 | |
| 1831 | In this case, we supplied a missing favorite color. You can |
| 1832 | provide doctest options: |
| 1833 | |
| 1834 | >>> suite = doctest.DocFileSuite('test_doctest.txt', |
| 1835 | ... 'test_doctest2.txt', |
| 1836 | ... optionflags=doctest.DONT_ACCEPT_BLANKLINE, |
| 1837 | ... globs={'favorite_color': 'blue'}) |
| 1838 | >>> suite.run(unittest.TestResult()) |
| 1839 | <unittest.TestResult run=2 errors=0 failures=2> |
| 1840 | |
| 1841 | And, you can provide setUp and tearDown functions: |
| 1842 | |
| 1843 | You can supply setUp and teatDoen functions: |
| 1844 | |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 1845 | >>> def setUp(t): |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1846 | ... import test.test_doctest |
| 1847 | ... test.test_doctest.sillySetup = True |
| 1848 | |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 1849 | >>> def tearDown(t): |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1850 | ... import test.test_doctest |
| 1851 | ... del test.test_doctest.sillySetup |
| 1852 | |
| 1853 | Here, we installed a silly variable that the test expects: |
| 1854 | |
| 1855 | >>> suite = doctest.DocFileSuite('test_doctest.txt', |
| 1856 | ... 'test_doctest2.txt', |
| 1857 | ... setUp=setUp, tearDown=tearDown) |
| 1858 | >>> suite.run(unittest.TestResult()) |
| 1859 | <unittest.TestResult run=2 errors=0 failures=1> |
| 1860 | |
| 1861 | But the tearDown restores sanity: |
| 1862 | |
| 1863 | >>> import test.test_doctest |
| 1864 | >>> test.test_doctest.sillySetup |
| 1865 | Traceback (most recent call last): |
| 1866 | ... |
| 1867 | AttributeError: 'module' object has no attribute 'sillySetup' |
| 1868 | |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 1869 | The setUp and tearDown funtions are passed test objects. |
| 1870 | Here, we'll use a setUp function to set the favorite color in |
| 1871 | test_doctest.txt: |
| 1872 | |
| 1873 | >>> def setUp(test): |
| 1874 | ... test.globs['favorite_color'] = 'blue' |
| 1875 | |
| 1876 | >>> suite = doctest.DocFileSuite('test_doctest.txt', setUp=setUp) |
| 1877 | >>> suite.run(unittest.TestResult()) |
| 1878 | <unittest.TestResult run=1 errors=0 failures=0> |
| 1879 | |
| 1880 | Here, we didn't need to use a tearDown function because we |
| 1881 | modified the test globals. The test globals are |
| 1882 | automatically cleared for us after a test. |
Tim Peters | df7a208 | 2004-08-29 00:38:17 +0000 | [diff] [blame] | 1883 | |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 1884 | """ |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1885 | |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1886 | def test_trailing_space_in_test(): |
| 1887 | """ |
Tim Peters | a7def72 | 2004-08-23 22:13:22 +0000 | [diff] [blame] | 1888 | Trailing spaces in expected output are significant: |
Tim Peters | c6cbab0 | 2004-08-22 19:43:28 +0000 | [diff] [blame] | 1889 | |
Jim Fulton | 07a349c | 2004-08-22 14:10:00 +0000 | [diff] [blame] | 1890 | >>> x, y = 'foo', '' |
| 1891 | >>> print x, y |
| 1892 | foo \n |
| 1893 | """ |
Tim Peters | 19397e5 | 2004-08-06 22:02:59 +0000 | [diff] [blame] | 1894 | |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 1895 | |
| 1896 | def test_unittest_reportflags(): |
| 1897 | """Default unittest reporting flags can be set to control reporting |
| 1898 | |
| 1899 | Here, we'll set the REPORT_ONLY_FIRST_FAILURE option so we see |
| 1900 | only the first failure of each test. First, we'll look at the |
| 1901 | output without the flag. The file test_doctest.txt file has two |
| 1902 | tests. They both fail if blank lines are disabled: |
| 1903 | |
| 1904 | >>> suite = doctest.DocFileSuite('test_doctest.txt', |
| 1905 | ... optionflags=doctest.DONT_ACCEPT_BLANKLINE) |
| 1906 | >>> import unittest |
| 1907 | >>> result = suite.run(unittest.TestResult()) |
| 1908 | >>> print result.failures[0][1] # doctest: +ELLIPSIS |
| 1909 | Traceback ... |
| 1910 | Failed example: |
| 1911 | favorite_color |
| 1912 | ... |
| 1913 | Failed example: |
| 1914 | if 1: |
| 1915 | ... |
| 1916 | |
| 1917 | Note that we see both failures displayed. |
| 1918 | |
| 1919 | >>> old = doctest.set_unittest_reportflags( |
| 1920 | ... doctest.REPORT_ONLY_FIRST_FAILURE) |
| 1921 | |
| 1922 | Now, when we run the test: |
| 1923 | |
| 1924 | >>> result = suite.run(unittest.TestResult()) |
| 1925 | >>> print result.failures[0][1] # doctest: +ELLIPSIS |
| 1926 | Traceback ... |
| 1927 | Failed example: |
| 1928 | favorite_color |
| 1929 | Exception raised: |
| 1930 | ... |
| 1931 | NameError: name 'favorite_color' is not defined |
| 1932 | <BLANKLINE> |
| 1933 | <BLANKLINE> |
Tim Peters | df7a208 | 2004-08-29 00:38:17 +0000 | [diff] [blame] | 1934 | |
Jim Fulton | f54bad4 | 2004-08-28 14:57:56 +0000 | [diff] [blame] | 1935 | We get only the first failure. |
| 1936 | |
| 1937 | If we give any reporting options when we set up the tests, |
| 1938 | however: |
| 1939 | |
| 1940 | >>> suite = doctest.DocFileSuite('test_doctest.txt', |
| 1941 | ... optionflags=doctest.DONT_ACCEPT_BLANKLINE | doctest.REPORT_NDIFF) |
| 1942 | |
| 1943 | Then the default eporting options are ignored: |
| 1944 | |
| 1945 | >>> result = suite.run(unittest.TestResult()) |
| 1946 | >>> print result.failures[0][1] # doctest: +ELLIPSIS |
| 1947 | Traceback ... |
| 1948 | Failed example: |
| 1949 | favorite_color |
| 1950 | ... |
| 1951 | Failed example: |
| 1952 | if 1: |
| 1953 | print 'a' |
| 1954 | print |
| 1955 | print 'b' |
| 1956 | Differences (ndiff with -expected +actual): |
| 1957 | a |
| 1958 | - <BLANKLINE> |
| 1959 | + |
| 1960 | b |
| 1961 | <BLANKLINE> |
| 1962 | <BLANKLINE> |
| 1963 | |
| 1964 | |
| 1965 | Test runners can restore the formatting flags after they run: |
| 1966 | |
| 1967 | >>> ignored = doctest.set_unittest_reportflags(old) |
| 1968 | |
| 1969 | """ |
| 1970 | |
Tim Peters | a7def72 | 2004-08-23 22:13:22 +0000 | [diff] [blame] | 1971 | # old_test1, ... used to live in doctest.py, but cluttered it. Note |
| 1972 | # that these use the deprecated doctest.Tester, so should go away (or |
| 1973 | # be rewritten) someday. |
| 1974 | |
| 1975 | # Ignore all warnings about the use of class Tester in this module. |
| 1976 | # Note that the name of this module may differ depending on how it's |
| 1977 | # imported, so the use of __name__ is important. |
| 1978 | warnings.filterwarnings("ignore", "class Tester", DeprecationWarning, |
| 1979 | __name__, 0) |
| 1980 | |
| 1981 | def old_test1(): r""" |
| 1982 | >>> from doctest import Tester |
| 1983 | >>> t = Tester(globs={'x': 42}, verbose=0) |
| 1984 | >>> t.runstring(r''' |
| 1985 | ... >>> x = x * 2 |
| 1986 | ... >>> print x |
| 1987 | ... 42 |
| 1988 | ... ''', 'XYZ') |
| 1989 | ********************************************************************** |
| 1990 | Line 3, in XYZ |
| 1991 | Failed example: |
| 1992 | print x |
| 1993 | Expected: |
| 1994 | 42 |
| 1995 | Got: |
| 1996 | 84 |
| 1997 | (1, 2) |
| 1998 | >>> t.runstring(">>> x = x * 2\n>>> print x\n84\n", 'example2') |
| 1999 | (0, 2) |
| 2000 | >>> t.summarize() |
| 2001 | ********************************************************************** |
| 2002 | 1 items had failures: |
| 2003 | 1 of 2 in XYZ |
| 2004 | ***Test Failed*** 1 failures. |
| 2005 | (1, 4) |
| 2006 | >>> t.summarize(verbose=1) |
| 2007 | 1 items passed all tests: |
| 2008 | 2 tests in example2 |
| 2009 | ********************************************************************** |
| 2010 | 1 items had failures: |
| 2011 | 1 of 2 in XYZ |
| 2012 | 4 tests in 2 items. |
| 2013 | 3 passed and 1 failed. |
| 2014 | ***Test Failed*** 1 failures. |
| 2015 | (1, 4) |
| 2016 | """ |
| 2017 | |
| 2018 | def old_test2(): r""" |
| 2019 | >>> from doctest import Tester |
| 2020 | >>> t = Tester(globs={}, verbose=1) |
| 2021 | >>> test = r''' |
| 2022 | ... # just an example |
| 2023 | ... >>> x = 1 + 2 |
| 2024 | ... >>> x |
| 2025 | ... 3 |
| 2026 | ... ''' |
| 2027 | >>> t.runstring(test, "Example") |
| 2028 | Running string Example |
Edward Loper | aacf083 | 2004-08-26 01:19:50 +0000 | [diff] [blame] | 2029 | Trying: |
| 2030 | x = 1 + 2 |
| 2031 | Expecting nothing |
Tim Peters | a7def72 | 2004-08-23 22:13:22 +0000 | [diff] [blame] | 2032 | ok |
Edward Loper | aacf083 | 2004-08-26 01:19:50 +0000 | [diff] [blame] | 2033 | Trying: |
| 2034 | x |
| 2035 | Expecting: |
| 2036 | 3 |
Tim Peters | a7def72 | 2004-08-23 22:13:22 +0000 | [diff] [blame] | 2037 | ok |
| 2038 | 0 of 2 examples failed in string Example |
| 2039 | (0, 2) |
| 2040 | """ |
| 2041 | |
| 2042 | def old_test3(): r""" |
| 2043 | >>> from doctest import Tester |
| 2044 | >>> t = Tester(globs={}, verbose=0) |
| 2045 | >>> def _f(): |
| 2046 | ... '''Trivial docstring example. |
| 2047 | ... >>> assert 2 == 2 |
| 2048 | ... ''' |
| 2049 | ... return 32 |
| 2050 | ... |
| 2051 | >>> t.rundoc(_f) # expect 0 failures in 1 example |
| 2052 | (0, 1) |
| 2053 | """ |
| 2054 | |
| 2055 | def old_test4(): """ |
| 2056 | >>> import new |
| 2057 | >>> m1 = new.module('_m1') |
| 2058 | >>> m2 = new.module('_m2') |
| 2059 | >>> test_data = \""" |
| 2060 | ... def _f(): |
| 2061 | ... '''>>> assert 1 == 1 |
| 2062 | ... ''' |
| 2063 | ... def g(): |
| 2064 | ... '''>>> assert 2 != 1 |
| 2065 | ... ''' |
| 2066 | ... class H: |
| 2067 | ... '''>>> assert 2 > 1 |
| 2068 | ... ''' |
| 2069 | ... def bar(self): |
| 2070 | ... '''>>> assert 1 < 2 |
| 2071 | ... ''' |
| 2072 | ... \""" |
| 2073 | >>> exec test_data in m1.__dict__ |
| 2074 | >>> exec test_data in m2.__dict__ |
| 2075 | >>> m1.__dict__.update({"f2": m2._f, "g2": m2.g, "h2": m2.H}) |
| 2076 | |
| 2077 | Tests that objects outside m1 are excluded: |
| 2078 | |
| 2079 | >>> from doctest import Tester |
| 2080 | >>> t = Tester(globs={}, verbose=0) |
| 2081 | >>> t.rundict(m1.__dict__, "rundict_test", m1) # f2 and g2 and h2 skipped |
| 2082 | (0, 4) |
| 2083 | |
| 2084 | Once more, not excluding stuff outside m1: |
| 2085 | |
| 2086 | >>> t = Tester(globs={}, verbose=0) |
| 2087 | >>> t.rundict(m1.__dict__, "rundict_test_pvt") # None are skipped. |
| 2088 | (0, 8) |
| 2089 | |
| 2090 | The exclusion of objects from outside the designated module is |
| 2091 | meant to be invoked automagically by testmod. |
| 2092 | |
| 2093 | >>> doctest.testmod(m1, verbose=False) |
| 2094 | (0, 4) |
| 2095 | """ |
| 2096 | |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 2097 | ###################################################################### |
| 2098 | ## Main |
| 2099 | ###################################################################### |
| 2100 | |
| 2101 | def test_main(): |
| 2102 | # Check the doctest cases in doctest itself: |
| 2103 | test_support.run_doctest(doctest, verbosity=True) |
| 2104 | # Check the doctest cases defined here: |
| 2105 | from test import test_doctest |
| 2106 | test_support.run_doctest(test_doctest, verbosity=True) |
| 2107 | |
| 2108 | import trace, sys, re, StringIO |
| 2109 | def test_coverage(coverdir): |
| 2110 | tracer = trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix,], |
| 2111 | trace=0, count=1) |
| 2112 | tracer.run('reload(doctest); test_main()') |
| 2113 | r = tracer.results() |
| 2114 | print 'Writing coverage results...' |
| 2115 | r.write_results(show_missing=True, summary=True, |
| 2116 | coverdir=coverdir) |
| 2117 | |
| 2118 | if __name__ == '__main__': |
| 2119 | if '-c' in sys.argv: |
| 2120 | test_coverage('/tmp/doctest.cover') |
| 2121 | else: |
| 2122 | test_main() |