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