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 |
| 7 | |
| 8 | ###################################################################### |
| 9 | ## Sample Objects (used by test cases) |
| 10 | ###################################################################### |
| 11 | |
| 12 | def sample_func(v): |
| 13 | """ |
| 14 | >>> print sample_func(22) |
| 15 | 44 |
| 16 | """ |
| 17 | return v+v |
| 18 | |
| 19 | class SampleClass: |
| 20 | """ |
| 21 | >>> print 1 |
| 22 | 1 |
| 23 | """ |
| 24 | def __init__(self, val): |
| 25 | """ |
| 26 | >>> print SampleClass(12).get() |
| 27 | 12 |
| 28 | """ |
| 29 | self.val = val |
| 30 | |
| 31 | def double(self): |
| 32 | """ |
| 33 | >>> print SampleClass(12).double().get() |
| 34 | 24 |
| 35 | """ |
| 36 | return SampleClass(self.val + self.val) |
| 37 | |
| 38 | def get(self): |
| 39 | """ |
| 40 | >>> print SampleClass(-5).get() |
| 41 | -5 |
| 42 | """ |
| 43 | return self.val |
| 44 | |
| 45 | def a_staticmethod(v): |
| 46 | """ |
| 47 | >>> print SampleClass.a_staticmethod(10) |
| 48 | 11 |
| 49 | """ |
| 50 | return v+1 |
| 51 | a_staticmethod = staticmethod(a_staticmethod) |
| 52 | |
| 53 | def a_classmethod(cls, v): |
| 54 | """ |
| 55 | >>> print SampleClass.a_classmethod(10) |
| 56 | 12 |
| 57 | >>> print SampleClass(0).a_classmethod(10) |
| 58 | 12 |
| 59 | """ |
| 60 | return v+2 |
| 61 | a_classmethod = classmethod(a_classmethod) |
| 62 | |
| 63 | a_property = property(get, doc=""" |
| 64 | >>> print SampleClass(22).a_property |
| 65 | 22 |
| 66 | """) |
| 67 | |
| 68 | class NestedClass: |
| 69 | """ |
| 70 | >>> x = SampleClass.NestedClass(5) |
| 71 | >>> y = x.square() |
| 72 | >>> print y.get() |
| 73 | 25 |
| 74 | """ |
| 75 | def __init__(self, val=0): |
| 76 | """ |
| 77 | >>> print SampleClass.NestedClass().get() |
| 78 | 0 |
| 79 | """ |
| 80 | self.val = val |
| 81 | def square(self): |
| 82 | return SampleClass.NestedClass(self.val*self.val) |
| 83 | def get(self): |
| 84 | return self.val |
| 85 | |
| 86 | class SampleNewStyleClass(object): |
| 87 | r""" |
| 88 | >>> print '1\n2\n3' |
| 89 | 1 |
| 90 | 2 |
| 91 | 3 |
| 92 | """ |
| 93 | def __init__(self, val): |
| 94 | """ |
| 95 | >>> print SampleNewStyleClass(12).get() |
| 96 | 12 |
| 97 | """ |
| 98 | self.val = val |
| 99 | |
| 100 | def double(self): |
| 101 | """ |
| 102 | >>> print SampleNewStyleClass(12).double().get() |
| 103 | 24 |
| 104 | """ |
| 105 | return SampleNewStyleClass(self.val + self.val) |
| 106 | |
| 107 | def get(self): |
| 108 | """ |
| 109 | >>> print SampleNewStyleClass(-5).get() |
| 110 | -5 |
| 111 | """ |
| 112 | return self.val |
| 113 | |
| 114 | ###################################################################### |
| 115 | ## Test Cases |
| 116 | ###################################################################### |
| 117 | |
| 118 | def test_Example(): r""" |
| 119 | Unit tests for the `Example` class. |
| 120 | |
| 121 | Example is a simple container class that holds a source code string, |
| 122 | an expected output string, and a line number (within the docstring): |
| 123 | |
| 124 | >>> example = doctest.Example('print 1', '1\n', 0) |
| 125 | >>> (example.source, example.want, example.lineno) |
| 126 | ('print 1', '1\n', 0) |
| 127 | |
| 128 | The `source` string should end in a newline iff the source spans more |
| 129 | than one line: |
| 130 | |
| 131 | >>> # Source spans a single line: no terminating newline. |
| 132 | >>> e = doctest.Example('print 1', '1\n', 0) |
| 133 | >>> e = doctest.Example('print 1\n', '1\n', 0) |
| 134 | Traceback (most recent call last): |
Tim Peters | 9b625d3 | 2004-08-04 20:04:32 +0000 | [diff] [blame] | 135 | AssertionError: source must end with newline iff source contains more than one line |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 136 | |
| 137 | >>> # Source spans multiple lines: require terminating newline. |
| 138 | >>> e = doctest.Example('print 1;\nprint 2\n', '1\n2\n', 0) |
| 139 | >>> e = doctest.Example('print 1;\nprint 2', '1\n2\n', 0) |
| 140 | Traceback (most recent call last): |
Tim Peters | 9b625d3 | 2004-08-04 20:04:32 +0000 | [diff] [blame] | 141 | AssertionError: source must end with newline iff source contains more than one line |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 142 | |
| 143 | The `want` string should be terminated by a newline, unless it's the |
| 144 | empty string: |
| 145 | |
| 146 | >>> e = doctest.Example('print 1', '1\n', 0) |
| 147 | >>> e = doctest.Example('print 1', '1', 0) |
| 148 | Traceback (most recent call last): |
Tim Peters | 9b625d3 | 2004-08-04 20:04:32 +0000 | [diff] [blame] | 149 | AssertionError: non-empty want must end with newline |
Tim Peters | 8485b56 | 2004-08-04 18:46:34 +0000 | [diff] [blame] | 150 | >>> e = doctest.Example('print', '', 0) |
| 151 | """ |
| 152 | |
| 153 | def test_DocTest(): r""" |
| 154 | Unit tests for the `DocTest` class. |
| 155 | |
| 156 | DocTest is a collection of examples, extracted from a docstring, along |
| 157 | with information about where the docstring comes from (a name, |
| 158 | filename, and line number). The docstring is parsed by the `DocTest` |
| 159 | constructor: |
| 160 | |
| 161 | >>> docstring = ''' |
| 162 | ... >>> print 12 |
| 163 | ... 12 |
| 164 | ... |
| 165 | ... Non-example text. |
| 166 | ... |
| 167 | ... >>> print 'another\example' |
| 168 | ... another |
| 169 | ... example |
| 170 | ... ''' |
| 171 | >>> globs = {} # globals to run the test in. |
| 172 | >>> test = doctest.DocTest(docstring, globs, 'some_test', 'some_file', 20) |
| 173 | >>> print test |
| 174 | <DocTest some_test from some_file:20 (2 examples)> |
| 175 | >>> len(test.examples) |
| 176 | 2 |
| 177 | >>> e1, e2 = test.examples |
| 178 | >>> (e1.source, e1.want, e1.lineno) |
| 179 | ('print 12', '12\n', 1) |
| 180 | >>> (e2.source, e2.want, e2.lineno) |
| 181 | ("print 'another\\example'", 'another\nexample\n', 6) |
| 182 | |
| 183 | Source information (name, filename, and line number) is available as |
| 184 | attributes on the doctest object: |
| 185 | |
| 186 | >>> (test.name, test.filename, test.lineno) |
| 187 | ('some_test', 'some_file', 20) |
| 188 | |
| 189 | The line number of an example within its containing file is found by |
| 190 | adding the line number of the example and the line number of its |
| 191 | containing test: |
| 192 | |
| 193 | >>> test.lineno + e1.lineno |
| 194 | 21 |
| 195 | >>> test.lineno + e2.lineno |
| 196 | 26 |
| 197 | |
| 198 | If the docstring contains inconsistant leading whitespace in the |
| 199 | expected output of an example, then `DocTest` will raise a ValueError: |
| 200 | |
| 201 | >>> docstring = r''' |
| 202 | ... >>> print 'bad\nindentation' |
| 203 | ... bad |
| 204 | ... indentation |
| 205 | ... ''' |
| 206 | >>> doctest.DocTest(docstring, globs, 'some_test', 'filename', 0) |
| 207 | Traceback (most recent call last): |
| 208 | ValueError: line 3 of the docstring for some_test has inconsistent leading whitespace: ' indentation' |
| 209 | |
| 210 | If the docstring contains inconsistent leading whitespace on |
| 211 | continuation lines, then `DocTest` will raise a ValueError: |
| 212 | |
| 213 | >>> docstring = r''' |
| 214 | ... >>> print ('bad indentation', |
| 215 | ... ... 2) |
| 216 | ... ('bad', 'indentation') |
| 217 | ... ''' |
| 218 | >>> doctest.DocTest(docstring, globs, 'some_test', 'filename', 0) |
| 219 | Traceback (most recent call last): |
| 220 | ValueError: line 2 of the docstring for some_test has inconsistent leading whitespace: ' ... 2)' |
| 221 | |
| 222 | If there's no blank space after a PS1 prompt ('>>>'), then `DocTest` |
| 223 | will raise a ValueError: |
| 224 | |
| 225 | >>> docstring = '>>>print 1\n1' |
| 226 | >>> doctest.DocTest(docstring, globs, 'some_test', 'filename', 0) |
| 227 | Traceback (most recent call last): |
| 228 | ValueError: line 0 of the docstring for some_test lacks blank after >>>: '>>>print 1' |
| 229 | """ |
| 230 | |
| 231 | # [XX] test that it's getting line numbers right. |
| 232 | def test_DocTestFinder(): r""" |
| 233 | Unit tests for the `DocTestFinder` class. |
| 234 | |
| 235 | DocTestFinder is used to extract DocTests from an object's docstring |
| 236 | and the docstrings of its contained objects. It can be used with |
| 237 | modules, functions, classes, methods, staticmethods, classmethods, and |
| 238 | properties. |
| 239 | |
| 240 | Finding Tests in Functions |
| 241 | ~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 242 | For a function whose docstring contains examples, DocTestFinder.find() |
| 243 | will return a single test (for that function's docstring): |
| 244 | |
| 245 | >>> # Allow ellipsis in the following examples (since the filename |
| 246 | >>> # and line number in the traceback can vary): |
| 247 | >>> doctest: +ELLIPSIS |
| 248 | |
| 249 | >>> finder = doctest.DocTestFinder() |
| 250 | >>> tests = finder.find(sample_func) |
| 251 | >>> print tests |
| 252 | [<DocTest sample_func from ...:12 (1 example)>] |
| 253 | >>> e = tests[0].examples[0] |
| 254 | >>> print (e.source, e.want, e.lineno) |
| 255 | ('print sample_func(22)', '44\n', 1) |
| 256 | |
| 257 | >>> doctest: -ELLIPSIS # Turn ellipsis back off |
| 258 | |
| 259 | If an object has no docstring, then a test is not created for it: |
| 260 | |
| 261 | >>> def no_docstring(v): |
| 262 | ... pass |
| 263 | >>> finder.find(no_docstring) |
| 264 | [] |
| 265 | |
| 266 | If the function has a docstring with no examples, then a test with no |
| 267 | examples is returned. (This lets `DocTestRunner` collect statistics |
| 268 | about which functions have no tests -- but is that useful? And should |
| 269 | an empty test also be created when there's no docstring?) |
| 270 | |
| 271 | >>> def no_examples(v): |
| 272 | ... ''' no doctest examples ''' |
| 273 | >>> finder.find(no_examples) |
| 274 | [<DocTest no_examples from None:1 (no examples)>] |
| 275 | |
| 276 | Finding Tests in Classes |
| 277 | ~~~~~~~~~~~~~~~~~~~~~~~~ |
| 278 | For a class, DocTestFinder will create a test for the class's |
| 279 | docstring, and will recursively explore its contents, including |
| 280 | methods, classmethods, staticmethods, properties, and nested classes. |
| 281 | |
| 282 | >>> finder = doctest.DocTestFinder() |
| 283 | >>> tests = finder.find(SampleClass) |
| 284 | >>> tests.sort() |
| 285 | >>> for t in tests: |
| 286 | ... print '%2s %s' % (len(t.examples), t.name) |
| 287 | 1 SampleClass |
| 288 | 3 SampleClass.NestedClass |
| 289 | 1 SampleClass.NestedClass.__init__ |
| 290 | 1 SampleClass.__init__ |
| 291 | 2 SampleClass.a_classmethod |
| 292 | 1 SampleClass.a_property |
| 293 | 1 SampleClass.a_staticmethod |
| 294 | 1 SampleClass.double |
| 295 | 1 SampleClass.get |
| 296 | |
| 297 | New-style classes are also supported: |
| 298 | |
| 299 | >>> tests = finder.find(SampleNewStyleClass) |
| 300 | >>> tests.sort() |
| 301 | >>> for t in tests: |
| 302 | ... print '%2s %s' % (len(t.examples), t.name) |
| 303 | 1 SampleNewStyleClass |
| 304 | 1 SampleNewStyleClass.__init__ |
| 305 | 1 SampleNewStyleClass.double |
| 306 | 1 SampleNewStyleClass.get |
| 307 | |
| 308 | Finding Tests in Modules |
| 309 | ~~~~~~~~~~~~~~~~~~~~~~~~ |
| 310 | For a module, DocTestFinder will create a test for the class's |
| 311 | docstring, and will recursively explore its contents, including |
| 312 | functions, classes, and the `__test__` dictionary, if it exists: |
| 313 | |
| 314 | >>> # A module |
| 315 | >>> import new |
| 316 | >>> m = new.module('some_module') |
| 317 | >>> def triple(val): |
| 318 | ... ''' |
| 319 | ... >>> print tripple(11) |
| 320 | ... 33 |
| 321 | ... ''' |
| 322 | ... return val*3 |
| 323 | >>> m.__dict__.update({ |
| 324 | ... 'sample_func': sample_func, |
| 325 | ... 'SampleClass': SampleClass, |
| 326 | ... '__doc__': ''' |
| 327 | ... Module docstring. |
| 328 | ... >>> print 'module' |
| 329 | ... module |
| 330 | ... ''', |
| 331 | ... '__test__': { |
| 332 | ... 'd': '>>> print 6\n6\n>>> print 7\n7\n', |
| 333 | ... 'c': triple}}) |
| 334 | |
| 335 | >>> finder = doctest.DocTestFinder() |
| 336 | >>> # Use module=test.test_doctest, to prevent doctest from |
| 337 | >>> # ignoring the objects since they weren't defined in m. |
| 338 | >>> import test.test_doctest |
| 339 | >>> tests = finder.find(m, module=test.test_doctest) |
| 340 | >>> tests.sort() |
| 341 | >>> for t in tests: |
| 342 | ... print '%2s %s' % (len(t.examples), t.name) |
| 343 | 1 some_module |
| 344 | 1 some_module.SampleClass |
| 345 | 3 some_module.SampleClass.NestedClass |
| 346 | 1 some_module.SampleClass.NestedClass.__init__ |
| 347 | 1 some_module.SampleClass.__init__ |
| 348 | 2 some_module.SampleClass.a_classmethod |
| 349 | 1 some_module.SampleClass.a_property |
| 350 | 1 some_module.SampleClass.a_staticmethod |
| 351 | 1 some_module.SampleClass.double |
| 352 | 1 some_module.SampleClass.get |
| 353 | 1 some_module.c |
| 354 | 2 some_module.d |
| 355 | 1 some_module.sample_func |
| 356 | |
| 357 | Duplicate Removal |
| 358 | ~~~~~~~~~~~~~~~~~ |
| 359 | If a single object is listed twice (under different names), then tests |
| 360 | will only be generated for it once: |
| 361 | |
| 362 | >>> class TwoNames: |
| 363 | ... '''f() and g() are two names for the same method''' |
| 364 | ... |
| 365 | ... def f(self): |
| 366 | ... ''' |
| 367 | ... >>> print TwoNames().f() |
| 368 | ... f |
| 369 | ... ''' |
| 370 | ... return 'f' |
| 371 | ... |
| 372 | ... g = f # define an alias for f. |
| 373 | |
| 374 | >>> finder = doctest.DocTestFinder() |
| 375 | >>> tests = finder.find(TwoNames, ignore_imports=False) |
| 376 | >>> tests.sort() |
| 377 | >>> print len(tests) |
| 378 | 2 |
| 379 | >>> print tests[0].name |
| 380 | TwoNames |
| 381 | >>> print tests[1].name in ('TwoNames.f', 'TwoNames.g') |
| 382 | True |
| 383 | |
| 384 | Filter Functions |
| 385 | ~~~~~~~~~~~~~~~~ |
| 386 | Two filter functions can be used to restrict which objects get |
| 387 | examined: a name-based filter and an object-based filter. |
| 388 | |
| 389 | >>> def namefilter(prefix, base): |
| 390 | ... return base.startswith('a_') |
| 391 | >>> tests = doctest.DocTestFinder(namefilter=namefilter).find(SampleClass) |
| 392 | >>> tests.sort() |
| 393 | >>> for t in tests: |
| 394 | ... print '%2s %s' % (len(t.examples), t.name) |
| 395 | 1 SampleClass |
| 396 | 3 SampleClass.NestedClass |
| 397 | 1 SampleClass.NestedClass.__init__ |
| 398 | 1 SampleClass.__init__ |
| 399 | 1 SampleClass.double |
| 400 | 1 SampleClass.get |
| 401 | |
| 402 | >>> def objfilter(obj): |
| 403 | ... return isinstance(obj, (staticmethod, classmethod)) |
| 404 | >>> tests = doctest.DocTestFinder(objfilter=objfilter).find(SampleClass) |
| 405 | >>> tests.sort() |
| 406 | >>> for t in tests: |
| 407 | ... print '%2s %s' % (len(t.examples), t.name) |
| 408 | 1 SampleClass |
| 409 | 3 SampleClass.NestedClass |
| 410 | 1 SampleClass.NestedClass.__init__ |
| 411 | 1 SampleClass.__init__ |
| 412 | 1 SampleClass.a_property |
| 413 | 1 SampleClass.double |
| 414 | 1 SampleClass.get |
| 415 | |
| 416 | If a given object is filtered out, then none of the objects that it |
| 417 | contains will be added either: |
| 418 | |
| 419 | >>> def namefilter(prefix, base): |
| 420 | ... return base == 'NestedClass' |
| 421 | >>> tests = doctest.DocTestFinder(namefilter=namefilter).find(SampleClass) |
| 422 | >>> tests.sort() |
| 423 | >>> for t in tests: |
| 424 | ... print '%2s %s' % (len(t.examples), t.name) |
| 425 | 1 SampleClass |
| 426 | 1 SampleClass.__init__ |
| 427 | 2 SampleClass.a_classmethod |
| 428 | 1 SampleClass.a_property |
| 429 | 1 SampleClass.a_staticmethod |
| 430 | 1 SampleClass.double |
| 431 | 1 SampleClass.get |
| 432 | |
| 433 | The filter functions apply to contained objects, and *not* to the |
| 434 | object explicitly passed to DocTestFinder: |
| 435 | |
| 436 | >>> def namefilter(prefix, base): |
| 437 | ... return base == 'SampleClass' |
| 438 | >>> tests = doctest.DocTestFinder(namefilter=namefilter).find(SampleClass) |
| 439 | >>> len(tests) |
| 440 | 9 |
| 441 | |
| 442 | Turning off Recursion |
| 443 | ~~~~~~~~~~~~~~~~~~~~~ |
| 444 | DocTestFinder can be told not to look for tests in contained objects |
| 445 | using the `recurse` flag: |
| 446 | |
| 447 | >>> tests = doctest.DocTestFinder(recurse=False).find(SampleClass) |
| 448 | >>> tests.sort() |
| 449 | >>> for t in tests: |
| 450 | ... print '%2s %s' % (len(t.examples), t.name) |
| 451 | 1 SampleClass |
| 452 | """ |
| 453 | |
| 454 | class test_DocTestRunner: |
| 455 | def basics(): r""" |
| 456 | Unit tests for the `DocTestRunner` class. |
| 457 | |
| 458 | DocTestRunner is used to run DocTest test cases, and to accumulate |
| 459 | statistics. Here's a simple DocTest case we can use: |
| 460 | |
| 461 | >>> def f(x): |
| 462 | ... ''' |
| 463 | ... >>> x = 12 |
| 464 | ... >>> print x |
| 465 | ... 12 |
| 466 | ... >>> x/2 |
| 467 | ... 6 |
| 468 | ... ''' |
| 469 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 470 | |
| 471 | The main DocTestRunner interface is the `run` method, which runs a |
| 472 | given DocTest case in a given namespace (globs). It returns a tuple |
| 473 | `(f,t)`, where `f` is the number of failed tests and `t` is the number |
| 474 | of tried tests. |
| 475 | |
| 476 | >>> doctest.DocTestRunner(verbose=False).run(test) |
| 477 | (0, 3) |
| 478 | |
| 479 | If any example produces incorrect output, then the test runner reports |
| 480 | the failure and proceeds to the next example: |
| 481 | |
| 482 | >>> def f(x): |
| 483 | ... ''' |
| 484 | ... >>> x = 12 |
| 485 | ... >>> print x |
| 486 | ... 14 |
| 487 | ... >>> x/2 |
| 488 | ... 6 |
| 489 | ... ''' |
| 490 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 491 | >>> doctest.DocTestRunner(verbose=True).run(test) |
| 492 | Trying: x = 12 |
| 493 | Expecting: nothing |
| 494 | ok |
| 495 | Trying: print x |
| 496 | Expecting: 14 |
| 497 | ********************************************************************** |
| 498 | Failure in example: print x |
| 499 | from line #2 of f |
| 500 | Expected: 14 |
| 501 | Got: 12 |
| 502 | Trying: x/2 |
| 503 | Expecting: 6 |
| 504 | ok |
| 505 | (1, 3) |
| 506 | """ |
| 507 | def verbose_flag(): r""" |
| 508 | The `verbose` flag makes the test runner generate more detailed |
| 509 | output: |
| 510 | |
| 511 | >>> def f(x): |
| 512 | ... ''' |
| 513 | ... >>> x = 12 |
| 514 | ... >>> print x |
| 515 | ... 12 |
| 516 | ... >>> x/2 |
| 517 | ... 6 |
| 518 | ... ''' |
| 519 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 520 | |
| 521 | >>> doctest.DocTestRunner(verbose=True).run(test) |
| 522 | Trying: x = 12 |
| 523 | Expecting: nothing |
| 524 | ok |
| 525 | Trying: print x |
| 526 | Expecting: 12 |
| 527 | ok |
| 528 | Trying: x/2 |
| 529 | Expecting: 6 |
| 530 | ok |
| 531 | (0, 3) |
| 532 | |
| 533 | If the `verbose` flag is unspecified, then the output will be verbose |
| 534 | iff `-v` appears in sys.argv: |
| 535 | |
| 536 | >>> # Save the real sys.argv list. |
| 537 | >>> old_argv = sys.argv |
| 538 | |
| 539 | >>> # If -v does not appear in sys.argv, then output isn't verbose. |
| 540 | >>> sys.argv = ['test'] |
| 541 | >>> doctest.DocTestRunner().run(test) |
| 542 | (0, 3) |
| 543 | |
| 544 | >>> # If -v does appear in sys.argv, then output is verbose. |
| 545 | >>> sys.argv = ['test', '-v'] |
| 546 | >>> doctest.DocTestRunner().run(test) |
| 547 | Trying: x = 12 |
| 548 | Expecting: nothing |
| 549 | ok |
| 550 | Trying: print x |
| 551 | Expecting: 12 |
| 552 | ok |
| 553 | Trying: x/2 |
| 554 | Expecting: 6 |
| 555 | ok |
| 556 | (0, 3) |
| 557 | |
| 558 | >>> # Restore sys.argv |
| 559 | >>> sys.argv = old_argv |
| 560 | |
| 561 | In the remaining examples, the test runner's verbosity will be |
| 562 | explicitly set, to ensure that the test behavior is consistent. |
| 563 | """ |
| 564 | def exceptions(): r""" |
| 565 | Tests of `DocTestRunner`'s exception handling. |
| 566 | |
| 567 | An expected exception is specified with a traceback message. The |
| 568 | lines between the first line and the type/value may be omitted or |
| 569 | replaced with any other string: |
| 570 | |
| 571 | >>> def f(x): |
| 572 | ... ''' |
| 573 | ... >>> x = 12 |
| 574 | ... >>> print x/0 |
| 575 | ... Traceback (most recent call last): |
| 576 | ... ZeroDivisionError: integer division or modulo by zero |
| 577 | ... ''' |
| 578 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 579 | >>> doctest.DocTestRunner(verbose=False).run(test) |
| 580 | (0, 2) |
| 581 | |
| 582 | An example may generate output before it raises an exception; if it |
| 583 | does, then the output must match the expected output: |
| 584 | |
| 585 | >>> def f(x): |
| 586 | ... ''' |
| 587 | ... >>> x = 12 |
| 588 | ... >>> print 'pre-exception output', x/0 |
| 589 | ... pre-exception output |
| 590 | ... Traceback (most recent call last): |
| 591 | ... ZeroDivisionError: integer division or modulo by zero |
| 592 | ... ''' |
| 593 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 594 | >>> doctest.DocTestRunner(verbose=False).run(test) |
| 595 | (0, 2) |
| 596 | |
| 597 | Exception messages may contain newlines: |
| 598 | |
| 599 | >>> def f(x): |
| 600 | ... r''' |
| 601 | ... >>> raise ValueError, 'multi\nline\nmessage' |
| 602 | ... Traceback (most recent call last): |
| 603 | ... ValueError: multi |
| 604 | ... line |
| 605 | ... message |
| 606 | ... ''' |
| 607 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 608 | >>> doctest.DocTestRunner(verbose=False).run(test) |
| 609 | (0, 1) |
| 610 | |
| 611 | If an exception is expected, but an exception with the wrong type or |
| 612 | message is raised, then it is reported as a failure: |
| 613 | |
| 614 | >>> def f(x): |
| 615 | ... r''' |
| 616 | ... >>> raise ValueError, 'message' |
| 617 | ... Traceback (most recent call last): |
| 618 | ... ValueError: wrong message |
| 619 | ... ''' |
| 620 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 621 | >>> doctest.DocTestRunner(verbose=False).run(test) |
| 622 | ********************************************************************** |
| 623 | Failure in example: raise ValueError, 'message' |
| 624 | from line #1 of f |
| 625 | Expected: |
| 626 | Traceback (most recent call last): |
| 627 | ValueError: wrong message |
| 628 | Got: |
| 629 | Traceback (most recent call last): |
| 630 | ValueError: message |
| 631 | (1, 1) |
| 632 | |
| 633 | If an exception is raised but not expected, then it is reported as an |
| 634 | unexpected exception: |
| 635 | |
| 636 | >>> # Allow ellipsis in the following examples (since the filename |
| 637 | >>> # and line number in the traceback can vary): |
| 638 | >>> doctest: +ELLIPSIS |
| 639 | |
| 640 | >>> def f(x): |
| 641 | ... r''' |
| 642 | ... >>> 1/0 |
| 643 | ... 0 |
| 644 | ... ''' |
| 645 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 646 | >>> doctest.DocTestRunner(verbose=False).run(test) |
| 647 | ********************************************************************** |
| 648 | Failure in example: 1/0 |
| 649 | from line #1 of f |
| 650 | Exception raised: |
| 651 | Traceback (most recent call last): |
| 652 | ... |
| 653 | ZeroDivisionError: integer division or modulo by zero |
| 654 | (1, 1) |
| 655 | |
| 656 | >>> doctest: -ELLIPSIS # Turn ellipsis back off: |
| 657 | """ |
| 658 | def optionflags(): r""" |
| 659 | Tests of `DocTestRunner`'s option flag handling. |
| 660 | |
| 661 | Several option flags can be used to customize the behavior of the test |
| 662 | runner. These are defined as module constants in doctest, and passed |
| 663 | to the DocTestRunner constructor (multiple constants should be or-ed |
| 664 | together). |
| 665 | |
| 666 | The DONT_ACCEPT_TRUE_FOR_1 flag disables matches between True/False |
| 667 | and 1/0: |
| 668 | |
| 669 | >>> def f(x): |
| 670 | ... '>>> True\n1\n' |
| 671 | |
| 672 | >>> # Without the flag: |
| 673 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 674 | >>> doctest.DocTestRunner(verbose=False).run(test) |
| 675 | (0, 1) |
| 676 | |
| 677 | >>> # With the flag: |
| 678 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 679 | >>> flags = doctest.DONT_ACCEPT_TRUE_FOR_1 |
| 680 | >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test) |
| 681 | ********************************************************************** |
| 682 | Failure in example: True |
| 683 | from line #0 of f |
| 684 | Expected: 1 |
| 685 | Got: True |
| 686 | (1, 1) |
| 687 | |
| 688 | The DONT_ACCEPT_BLANKLINE flag disables the match between blank lines |
| 689 | and the '<BLANKLINE>' marker: |
| 690 | |
| 691 | >>> def f(x): |
| 692 | ... '>>> print "a\\n\\nb"\na\n<BLANKLINE>\nb\n' |
| 693 | |
| 694 | >>> # Without the flag: |
| 695 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 696 | >>> doctest.DocTestRunner(verbose=False).run(test) |
| 697 | (0, 1) |
| 698 | |
| 699 | >>> # With the flag: |
| 700 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 701 | >>> flags = doctest.DONT_ACCEPT_BLANKLINE |
| 702 | >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test) |
| 703 | ********************************************************************** |
| 704 | Failure in example: print "a\n\nb" |
| 705 | from line #0 of f |
| 706 | Expected: |
| 707 | a |
| 708 | <BLANKLINE> |
| 709 | b |
| 710 | Got: |
| 711 | a |
| 712 | <BLANKLINE> |
| 713 | b |
| 714 | (1, 1) |
| 715 | |
| 716 | The NORMALIZE_WHITESPACE flag causes all sequences of whitespace to be |
| 717 | treated as equal: |
| 718 | |
| 719 | >>> def f(x): |
| 720 | ... '>>> print 1, 2, 3\n 1 2\n 3' |
| 721 | |
| 722 | >>> # Without the flag: |
| 723 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 724 | >>> doctest.DocTestRunner(verbose=False).run(test) |
| 725 | ********************************************************************** |
| 726 | Failure in example: print 1, 2, 3 |
| 727 | from line #0 of f |
| 728 | Expected: |
| 729 | 1 2 |
| 730 | 3 |
| 731 | Got: 1 2 3 |
| 732 | (1, 1) |
| 733 | |
| 734 | >>> # With the flag: |
| 735 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 736 | >>> flags = doctest.NORMALIZE_WHITESPACE |
| 737 | >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test) |
| 738 | (0, 1) |
| 739 | |
| 740 | The ELLIPSIS flag causes ellipsis marker ("...") in the expected |
| 741 | output to match any substring in the actual output: |
| 742 | |
| 743 | >>> def f(x): |
| 744 | ... '>>> print range(15)\n[0, 1, 2, ..., 14]\n' |
| 745 | |
| 746 | >>> # Without the flag: |
| 747 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 748 | >>> doctest.DocTestRunner(verbose=False).run(test) |
| 749 | ********************************************************************** |
| 750 | Failure in example: print range(15) |
| 751 | from line #0 of f |
| 752 | Expected: [0, 1, 2, ..., 14] |
| 753 | Got: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14] |
| 754 | (1, 1) |
| 755 | |
| 756 | >>> # With the flag: |
| 757 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 758 | >>> flags = doctest.ELLIPSIS |
| 759 | >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test) |
| 760 | (0, 1) |
| 761 | |
| 762 | The UNIFIED_DIFF flag causes failures that involve multi-line expected |
| 763 | and actual outputs to be displayed using a unified diff: |
| 764 | |
| 765 | >>> def f(x): |
| 766 | ... r''' |
| 767 | ... >>> print '\n'.join('abcdefg') |
| 768 | ... a |
| 769 | ... B |
| 770 | ... c |
| 771 | ... d |
| 772 | ... f |
| 773 | ... g |
| 774 | ... h |
| 775 | ... ''' |
| 776 | |
| 777 | >>> # Without the flag: |
| 778 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 779 | >>> doctest.DocTestRunner(verbose=False).run(test) |
| 780 | ********************************************************************** |
| 781 | Failure in example: print '\n'.join('abcdefg') |
| 782 | from line #1 of f |
| 783 | Expected: |
| 784 | a |
| 785 | B |
| 786 | c |
| 787 | d |
| 788 | f |
| 789 | g |
| 790 | h |
| 791 | Got: |
| 792 | a |
| 793 | b |
| 794 | c |
| 795 | d |
| 796 | e |
| 797 | f |
| 798 | g |
| 799 | (1, 1) |
| 800 | |
| 801 | >>> # With the flag: |
| 802 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 803 | >>> flags = doctest.UNIFIED_DIFF |
| 804 | >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test) |
| 805 | ********************************************************************** |
| 806 | Failure in example: print '\n'.join('abcdefg') |
| 807 | from line #1 of f |
| 808 | Differences (unified diff): |
| 809 | --- Expected |
| 810 | +++ Got |
| 811 | @@ -1,8 +1,8 @@ |
| 812 | a |
| 813 | -B |
| 814 | +b |
| 815 | c |
| 816 | d |
| 817 | +e |
| 818 | f |
| 819 | g |
| 820 | -h |
| 821 | <BLANKLINE> |
| 822 | (1, 1) |
| 823 | |
| 824 | The CONTEXT_DIFF flag causes failures that involve multi-line expected |
| 825 | and actual outputs to be displayed using a context diff: |
| 826 | |
| 827 | >>> # Reuse f() from the UNIFIED_DIFF example, above. |
| 828 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 829 | >>> flags = doctest.CONTEXT_DIFF |
| 830 | >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test) |
| 831 | ********************************************************************** |
| 832 | Failure in example: print '\n'.join('abcdefg') |
| 833 | from line #1 of f |
| 834 | Differences (context diff): |
| 835 | *** Expected |
| 836 | --- Got |
| 837 | *************** |
| 838 | *** 1,8 **** |
| 839 | a |
| 840 | ! B |
| 841 | c |
| 842 | d |
| 843 | f |
| 844 | g |
| 845 | - h |
| 846 | <BLANKLINE> |
| 847 | --- 1,8 ---- |
| 848 | a |
| 849 | ! b |
| 850 | c |
| 851 | d |
| 852 | + e |
| 853 | f |
| 854 | g |
| 855 | <BLANKLINE> |
| 856 | (1, 1) |
| 857 | """ |
| 858 | def option_directives(): r""" |
| 859 | Tests of `DocTestRunner`'s option directive mechanism. |
| 860 | |
| 861 | Option directives can be used to turn option flags on or off from |
| 862 | within a DocTest case. The following example shows how a flag can be |
| 863 | turned on and off. Note that comments on the same line as the option |
| 864 | directive are ignored. |
| 865 | |
| 866 | >>> def f(x): r''' |
| 867 | ... >>> print range(10) # Should fail: no ellipsis |
| 868 | ... [0, 1, ..., 9] |
| 869 | ... |
| 870 | ... >>> doctest: +ELLIPSIS # turn ellipsis on. |
| 871 | ... >>> print range(10) # Should succeed |
| 872 | ... [0, 1, ..., 9] |
| 873 | ... |
| 874 | ... >>> doctest: -ELLIPSIS # turn ellipsis back off. |
| 875 | ... >>> print range(10) # Should fail: no ellipsis |
| 876 | ... [0, 1, ..., 9] |
| 877 | ... ''' |
| 878 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 879 | >>> doctest.DocTestRunner(verbose=False).run(test) |
| 880 | ********************************************************************** |
| 881 | Failure in example: print range(10) # Should fail: no ellipsis |
| 882 | from line #1 of f |
| 883 | Expected: [0, 1, ..., 9] |
| 884 | Got: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 885 | ********************************************************************** |
| 886 | Failure in example: print range(10) # Should fail: no ellipsis |
| 887 | from line #9 of f |
| 888 | Expected: [0, 1, ..., 9] |
| 889 | Got: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 890 | (2, 3) |
| 891 | |
| 892 | Multiple flags can be toggled by a single option directive: |
| 893 | |
| 894 | >>> def f(x): r''' |
| 895 | ... >>> print range(10) # Should fail |
| 896 | ... [0, 1, ..., 9] |
| 897 | ... >>> doctest: +ELLIPSIS +NORMALIZE_WHITESPACE |
| 898 | ... >>> print range(10) # Should succeed |
| 899 | ... [0, 1, ..., 9] |
| 900 | ... ''' |
| 901 | >>> test = doctest.DocTestFinder().find(f)[0] |
| 902 | >>> doctest.DocTestRunner(verbose=False).run(test) |
| 903 | ********************************************************************** |
| 904 | Failure in example: print range(10) # Should fail |
| 905 | from line #1 of f |
| 906 | Expected: [0, 1, ..., 9] |
| 907 | Got: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 908 | (1, 2) |
| 909 | """ |
| 910 | |
| 911 | def test_testsource(): r""" |
| 912 | Unit tests for `testsource()`. |
| 913 | |
| 914 | The testsource() function takes a module and a name, finds the (first) |
| 915 | test with that name in that module, and converts it to an |
| 916 | |
| 917 | >>> import test.test_doctest |
| 918 | >>> name = 'test.test_doctest.sample_func' |
| 919 | >>> print doctest.testsource(test.test_doctest, name) |
| 920 | print sample_func(22) |
| 921 | # Expected: |
| 922 | # 44 |
| 923 | |
| 924 | >>> name = 'test.test_doctest.SampleNewStyleClass' |
| 925 | >>> print doctest.testsource(test.test_doctest, name) |
| 926 | print '1\n2\n3' |
| 927 | # Expected: |
| 928 | # 1 |
| 929 | # 2 |
| 930 | # 3 |
| 931 | |
| 932 | >>> name = 'test.test_doctest.SampleClass.a_classmethod' |
| 933 | >>> print doctest.testsource(test.test_doctest, name) |
| 934 | print SampleClass.a_classmethod(10) |
| 935 | # Expected: |
| 936 | # 12 |
| 937 | print SampleClass(0).a_classmethod(10) |
| 938 | # Expected: |
| 939 | # 12 |
| 940 | """ |
| 941 | |
| 942 | def test_debug(): r""" |
| 943 | |
| 944 | Create a docstring that we want to debug: |
| 945 | |
| 946 | >>> s = ''' |
| 947 | ... >>> x = 12 |
| 948 | ... >>> print x |
| 949 | ... 12 |
| 950 | ... ''' |
| 951 | |
| 952 | Create some fake stdin input, to feed to the debugger: |
| 953 | |
| 954 | >>> import tempfile |
| 955 | >>> fake_stdin = tempfile.TemporaryFile(mode='w+') |
| 956 | >>> fake_stdin.write('\n'.join(['next', 'print x', 'continue', ''])) |
| 957 | >>> fake_stdin.seek(0) |
| 958 | >>> real_stdin = sys.stdin |
| 959 | >>> sys.stdin = fake_stdin |
| 960 | |
| 961 | Run the debugger on the docstring, and then restore sys.stdin. |
| 962 | |
| 963 | >>> doctest: +NORMALIZE_WHITESPACE |
| 964 | >>> try: |
| 965 | ... doctest.debug_src(s) |
| 966 | ... finally: |
| 967 | ... sys.stdin = real_stdin |
| 968 | ... fake_stdin.close() |
| 969 | > <string>(1)?() |
| 970 | (Pdb) 12 |
| 971 | --Return-- |
| 972 | > <string>(1)?()->None |
| 973 | (Pdb) 12 |
| 974 | (Pdb) |
| 975 | |
| 976 | """ |
| 977 | |
| 978 | ###################################################################### |
| 979 | ## Main |
| 980 | ###################################################################### |
| 981 | |
| 982 | def test_main(): |
| 983 | # Check the doctest cases in doctest itself: |
| 984 | test_support.run_doctest(doctest, verbosity=True) |
| 985 | # Check the doctest cases defined here: |
| 986 | from test import test_doctest |
| 987 | test_support.run_doctest(test_doctest, verbosity=True) |
| 988 | |
| 989 | import trace, sys, re, StringIO |
| 990 | def test_coverage(coverdir): |
| 991 | tracer = trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix,], |
| 992 | trace=0, count=1) |
| 993 | tracer.run('reload(doctest); test_main()') |
| 994 | r = tracer.results() |
| 995 | print 'Writing coverage results...' |
| 996 | r.write_results(show_missing=True, summary=True, |
| 997 | coverdir=coverdir) |
| 998 | |
| 999 | if __name__ == '__main__': |
| 1000 | if '-c' in sys.argv: |
| 1001 | test_coverage('/tmp/doctest.cover') |
| 1002 | else: |
| 1003 | test_main() |