blob: d6830510bbfa34e55e50617260fce48aab0cf4b0 [file] [log] [blame]
Tim Peters8485b562004-08-04 18:46:34 +00001"""
2Test script for doctest.
3"""
4
Barry Warsaw04f357c2002-07-23 19:04:11 +00005from test import test_support
Tim Peters8485b562004-08-04 18:46:34 +00006import doctest
Tim Petersa7def722004-08-23 22:13:22 +00007import warnings
Tim Peters8485b562004-08-04 18:46:34 +00008
9######################################################################
10## Sample Objects (used by test cases)
11######################################################################
12
13def sample_func(v):
14 """
Tim Peters19397e52004-08-06 22:02:59 +000015 Blah blah
16
Tim Peters8485b562004-08-04 18:46:34 +000017 >>> print sample_func(22)
18 44
Tim Peters19397e52004-08-06 22:02:59 +000019
20 Yee ha!
Tim Peters8485b562004-08-04 18:46:34 +000021 """
22 return v+v
23
24class 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
91class 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######################################################################
120## Test Cases
121######################################################################
122
123def test_Example(): r"""
124Unit tests for the `Example` class.
125
Edward Lopera6b68322004-08-26 00:05:43 +0000126Example is a simple container class that holds:
127 - `source`: A source string.
128 - `want`: An expected output string.
129 - `exc_msg`: An expected exception message string (or None if no
130 exception is expected).
131 - `lineno`: A line number (within the docstring).
132 - `indent`: The example's indentation in the input string.
133 - `options`: An option dictionary, mapping option flags to True or
134 False.
Tim Peters8485b562004-08-04 18:46:34 +0000135
Edward Lopera6b68322004-08-26 00:05:43 +0000136These attributes are set by the constructor. `source` and `want` are
137required; the other attributes all have default values:
Tim Peters8485b562004-08-04 18:46:34 +0000138
Edward Lopera6b68322004-08-26 00:05:43 +0000139 >>> example = doctest.Example('print 1', '1\n')
140 >>> (example.source, example.want, example.exc_msg,
141 ... example.lineno, example.indent, example.options)
142 ('print 1\n', '1\n', None, 0, 0, {})
143
144The first three attributes (`source`, `want`, and `exc_msg`) may be
145specified positionally; the remaining arguments should be specified as
146keyword arguments:
147
148 >>> exc_msg = 'IndexError: pop from an empty list'
149 >>> example = doctest.Example('[].pop()', '', exc_msg,
150 ... lineno=5, indent=4,
151 ... options={doctest.ELLIPSIS: True})
152 >>> (example.source, example.want, example.exc_msg,
153 ... example.lineno, example.indent, example.options)
154 ('[].pop()\n', '', 'IndexError: pop from an empty list\n', 5, 4, {8: True})
155
156The constructor normalizes the `source` string to end in a newline:
Tim Peters8485b562004-08-04 18:46:34 +0000157
Tim Petersbb431472004-08-09 03:51:46 +0000158 Source spans a single line: no terminating newline.
Edward Lopera6b68322004-08-26 00:05:43 +0000159 >>> e = doctest.Example('print 1', '1\n')
Tim Petersbb431472004-08-09 03:51:46 +0000160 >>> e.source, e.want
161 ('print 1\n', '1\n')
162
Edward Lopera6b68322004-08-26 00:05:43 +0000163 >>> e = doctest.Example('print 1\n', '1\n')
Tim Petersbb431472004-08-09 03:51:46 +0000164 >>> e.source, e.want
165 ('print 1\n', '1\n')
Tim Peters8485b562004-08-04 18:46:34 +0000166
Tim Petersbb431472004-08-09 03:51:46 +0000167 Source spans multiple lines: require terminating newline.
Edward Lopera6b68322004-08-26 00:05:43 +0000168 >>> e = doctest.Example('print 1;\nprint 2\n', '1\n2\n')
Tim Petersbb431472004-08-09 03:51:46 +0000169 >>> e.source, e.want
170 ('print 1;\nprint 2\n', '1\n2\n')
Tim Peters8485b562004-08-04 18:46:34 +0000171
Edward Lopera6b68322004-08-26 00:05:43 +0000172 >>> e = doctest.Example('print 1;\nprint 2', '1\n2\n')
Tim Petersbb431472004-08-09 03:51:46 +0000173 >>> e.source, e.want
174 ('print 1;\nprint 2\n', '1\n2\n')
175
Edward Lopera6b68322004-08-26 00:05:43 +0000176 Empty source string (which should never appear in real examples)
177 >>> e = doctest.Example('', '')
178 >>> e.source, e.want
179 ('\n', '')
Tim Peters8485b562004-08-04 18:46:34 +0000180
Edward Lopera6b68322004-08-26 00:05:43 +0000181The constructor normalizes the `want` string to end in a newline,
182unless it's the empty string:
183
184 >>> e = doctest.Example('print 1', '1\n')
Tim Petersbb431472004-08-09 03:51:46 +0000185 >>> e.source, e.want
186 ('print 1\n', '1\n')
187
Edward Lopera6b68322004-08-26 00:05:43 +0000188 >>> e = doctest.Example('print 1', '1')
Tim Petersbb431472004-08-09 03:51:46 +0000189 >>> e.source, e.want
190 ('print 1\n', '1\n')
191
Edward Lopera6b68322004-08-26 00:05:43 +0000192 >>> e = doctest.Example('print', '')
Tim Petersbb431472004-08-09 03:51:46 +0000193 >>> e.source, e.want
194 ('print\n', '')
Edward Lopera6b68322004-08-26 00:05:43 +0000195
196The constructor normalizes the `exc_msg` string to end in a newline,
197unless it's `None`:
198
199 Message spans one line
200 >>> exc_msg = 'IndexError: pop from an empty list'
201 >>> e = doctest.Example('[].pop()', '', exc_msg)
202 >>> e.exc_msg
203 'IndexError: pop from an empty list\n'
204
205 >>> exc_msg = 'IndexError: pop from an empty list\n'
206 >>> e = doctest.Example('[].pop()', '', exc_msg)
207 >>> e.exc_msg
208 'IndexError: pop from an empty list\n'
209
210 Message spans multiple lines
211 >>> exc_msg = 'ValueError: 1\n 2'
212 >>> e = doctest.Example('raise ValueError("1\n 2")', '', exc_msg)
213 >>> e.exc_msg
214 'ValueError: 1\n 2\n'
215
216 >>> exc_msg = 'ValueError: 1\n 2\n'
217 >>> e = doctest.Example('raise ValueError("1\n 2")', '', exc_msg)
218 >>> e.exc_msg
219 'ValueError: 1\n 2\n'
220
221 Empty (but non-None) exception message (which should never appear
222 in real examples)
223 >>> exc_msg = ''
224 >>> e = doctest.Example('raise X()', '', exc_msg)
225 >>> e.exc_msg
226 '\n'
Tim Peters8485b562004-08-04 18:46:34 +0000227"""
228
229def test_DocTest(): r"""
230Unit tests for the `DocTest` class.
231
232DocTest is a collection of examples, extracted from a docstring, along
233with information about where the docstring comes from (a name,
234filename, and line number). The docstring is parsed by the `DocTest`
235constructor:
236
237 >>> docstring = '''
238 ... >>> print 12
239 ... 12
240 ...
241 ... Non-example text.
242 ...
243 ... >>> print 'another\example'
244 ... another
245 ... example
246 ... '''
247 >>> globs = {} # globals to run the test in.
Edward Lopera1ef6112004-08-09 16:14:41 +0000248 >>> parser = doctest.DocTestParser()
249 >>> test = parser.get_doctest(docstring, globs, 'some_test',
250 ... 'some_file', 20)
Tim Peters8485b562004-08-04 18:46:34 +0000251 >>> print test
252 <DocTest some_test from some_file:20 (2 examples)>
253 >>> len(test.examples)
254 2
255 >>> e1, e2 = test.examples
256 >>> (e1.source, e1.want, e1.lineno)
Tim Petersbb431472004-08-09 03:51:46 +0000257 ('print 12\n', '12\n', 1)
Tim Peters8485b562004-08-04 18:46:34 +0000258 >>> (e2.source, e2.want, e2.lineno)
Tim Petersbb431472004-08-09 03:51:46 +0000259 ("print 'another\\example'\n", 'another\nexample\n', 6)
Tim Peters8485b562004-08-04 18:46:34 +0000260
261Source information (name, filename, and line number) is available as
262attributes on the doctest object:
263
264 >>> (test.name, test.filename, test.lineno)
265 ('some_test', 'some_file', 20)
266
267The line number of an example within its containing file is found by
268adding the line number of the example and the line number of its
269containing test:
270
271 >>> test.lineno + e1.lineno
272 21
273 >>> test.lineno + e2.lineno
274 26
275
276If the docstring contains inconsistant leading whitespace in the
277expected output of an example, then `DocTest` will raise a ValueError:
278
279 >>> docstring = r'''
280 ... >>> print 'bad\nindentation'
281 ... bad
282 ... indentation
283 ... '''
Edward Lopera1ef6112004-08-09 16:14:41 +0000284 >>> parser.get_doctest(docstring, globs, 'some_test', 'filename', 0)
Tim Peters8485b562004-08-04 18:46:34 +0000285 Traceback (most recent call last):
Edward Loper7c748462004-08-09 02:06:06 +0000286 ValueError: line 4 of the docstring for some_test has inconsistent leading whitespace: ' indentation'
Tim Peters8485b562004-08-04 18:46:34 +0000287
288If the docstring contains inconsistent leading whitespace on
289continuation lines, then `DocTest` will raise a ValueError:
290
291 >>> docstring = r'''
292 ... >>> print ('bad indentation',
293 ... ... 2)
294 ... ('bad', 'indentation')
295 ... '''
Edward Lopera1ef6112004-08-09 16:14:41 +0000296 >>> parser.get_doctest(docstring, globs, 'some_test', 'filename', 0)
Tim Peters8485b562004-08-04 18:46:34 +0000297 Traceback (most recent call last):
298 ValueError: line 2 of the docstring for some_test has inconsistent leading whitespace: ' ... 2)'
299
300If there's no blank space after a PS1 prompt ('>>>'), then `DocTest`
301will raise a ValueError:
302
303 >>> docstring = '>>>print 1\n1'
Edward Lopera1ef6112004-08-09 16:14:41 +0000304 >>> parser.get_doctest(docstring, globs, 'some_test', 'filename', 0)
Tim Peters8485b562004-08-04 18:46:34 +0000305 Traceback (most recent call last):
Edward Loper7c748462004-08-09 02:06:06 +0000306 ValueError: line 1 of the docstring for some_test lacks blank after >>>: '>>>print 1'
307
308If there's no blank space after a PS2 prompt ('...'), then `DocTest`
309will raise a ValueError:
310
311 >>> docstring = '>>> if 1:\n...print 1\n1'
Edward Lopera1ef6112004-08-09 16:14:41 +0000312 >>> parser.get_doctest(docstring, globs, 'some_test', 'filename', 0)
Edward Loper7c748462004-08-09 02:06:06 +0000313 Traceback (most recent call last):
314 ValueError: line 2 of the docstring for some_test lacks blank after ...: '...print 1'
315
Tim Peters8485b562004-08-04 18:46:34 +0000316"""
317
Tim Peters8485b562004-08-04 18:46:34 +0000318def test_DocTestFinder(): r"""
319Unit tests for the `DocTestFinder` class.
320
321DocTestFinder is used to extract DocTests from an object's docstring
322and the docstrings of its contained objects. It can be used with
323modules, functions, classes, methods, staticmethods, classmethods, and
324properties.
325
326Finding Tests in Functions
327~~~~~~~~~~~~~~~~~~~~~~~~~~
328For a function whose docstring contains examples, DocTestFinder.find()
329will return a single test (for that function's docstring):
330
Tim Peters8485b562004-08-04 18:46:34 +0000331 >>> finder = doctest.DocTestFinder()
Jim Fulton07a349c2004-08-22 14:10:00 +0000332
333We'll simulate a __file__ attr that ends in pyc:
334
335 >>> import test.test_doctest
336 >>> old = test.test_doctest.__file__
337 >>> test.test_doctest.__file__ = 'test_doctest.pyc'
338
Tim Peters8485b562004-08-04 18:46:34 +0000339 >>> tests = finder.find(sample_func)
Edward Loper8e4a34b2004-08-12 02:34:27 +0000340
Edward Loper74bca7a2004-08-12 02:27:44 +0000341 >>> print tests # doctest: +ELLIPSIS
Tim Petersa7def722004-08-23 22:13:22 +0000342 [<DocTest sample_func from ...:13 (1 example)>]
Edward Loper8e4a34b2004-08-12 02:34:27 +0000343
Tim Peters4de7c5c2004-08-23 22:38:05 +0000344The exact name depends on how test_doctest was invoked, so allow for
345leading path components.
346
347 >>> tests[0].filename # doctest: +ELLIPSIS
348 '...test_doctest.py'
Jim Fulton07a349c2004-08-22 14:10:00 +0000349
350 >>> test.test_doctest.__file__ = old
Tim Petersc6cbab02004-08-22 19:43:28 +0000351
Jim Fulton07a349c2004-08-22 14:10:00 +0000352
Tim Peters8485b562004-08-04 18:46:34 +0000353 >>> e = tests[0].examples[0]
Tim Petersbb431472004-08-09 03:51:46 +0000354 >>> (e.source, e.want, e.lineno)
355 ('print sample_func(22)\n', '44\n', 3)
Tim Peters8485b562004-08-04 18:46:34 +0000356
Tim Peters8485b562004-08-04 18:46:34 +0000357If an object has no docstring, then a test is not created for it:
358
359 >>> def no_docstring(v):
360 ... pass
361 >>> finder.find(no_docstring)
362 []
363
364If the function has a docstring with no examples, then a test with no
365examples is returned. (This lets `DocTestRunner` collect statistics
366about which functions have no tests -- but is that useful? And should
367an empty test also be created when there's no docstring?)
368
369 >>> def no_examples(v):
370 ... ''' no doctest examples '''
371 >>> finder.find(no_examples)
372 [<DocTest no_examples from None:1 (no examples)>]
373
374Finding Tests in Classes
375~~~~~~~~~~~~~~~~~~~~~~~~
376For a class, DocTestFinder will create a test for the class's
377docstring, and will recursively explore its contents, including
378methods, classmethods, staticmethods, properties, and nested classes.
379
380 >>> finder = doctest.DocTestFinder()
381 >>> tests = finder.find(SampleClass)
382 >>> tests.sort()
383 >>> for t in tests:
384 ... print '%2s %s' % (len(t.examples), t.name)
385 1 SampleClass
386 3 SampleClass.NestedClass
387 1 SampleClass.NestedClass.__init__
388 1 SampleClass.__init__
389 2 SampleClass.a_classmethod
390 1 SampleClass.a_property
391 1 SampleClass.a_staticmethod
392 1 SampleClass.double
393 1 SampleClass.get
394
395New-style classes are also supported:
396
397 >>> tests = finder.find(SampleNewStyleClass)
398 >>> tests.sort()
399 >>> for t in tests:
400 ... print '%2s %s' % (len(t.examples), t.name)
401 1 SampleNewStyleClass
402 1 SampleNewStyleClass.__init__
403 1 SampleNewStyleClass.double
404 1 SampleNewStyleClass.get
405
406Finding Tests in Modules
407~~~~~~~~~~~~~~~~~~~~~~~~
408For a module, DocTestFinder will create a test for the class's
409docstring, and will recursively explore its contents, including
410functions, classes, and the `__test__` dictionary, if it exists:
411
412 >>> # A module
413 >>> import new
414 >>> m = new.module('some_module')
415 >>> def triple(val):
416 ... '''
417 ... >>> print tripple(11)
418 ... 33
419 ... '''
420 ... return val*3
421 >>> m.__dict__.update({
422 ... 'sample_func': sample_func,
423 ... 'SampleClass': SampleClass,
424 ... '__doc__': '''
425 ... Module docstring.
426 ... >>> print 'module'
427 ... module
428 ... ''',
429 ... '__test__': {
430 ... 'd': '>>> print 6\n6\n>>> print 7\n7\n',
431 ... 'c': triple}})
432
433 >>> finder = doctest.DocTestFinder()
434 >>> # Use module=test.test_doctest, to prevent doctest from
435 >>> # ignoring the objects since they weren't defined in m.
436 >>> import test.test_doctest
437 >>> tests = finder.find(m, module=test.test_doctest)
438 >>> tests.sort()
439 >>> for t in tests:
440 ... print '%2s %s' % (len(t.examples), t.name)
441 1 some_module
442 1 some_module.SampleClass
443 3 some_module.SampleClass.NestedClass
444 1 some_module.SampleClass.NestedClass.__init__
445 1 some_module.SampleClass.__init__
446 2 some_module.SampleClass.a_classmethod
447 1 some_module.SampleClass.a_property
448 1 some_module.SampleClass.a_staticmethod
449 1 some_module.SampleClass.double
450 1 some_module.SampleClass.get
451 1 some_module.c
452 2 some_module.d
453 1 some_module.sample_func
454
455Duplicate Removal
456~~~~~~~~~~~~~~~~~
457If a single object is listed twice (under different names), then tests
458will only be generated for it once:
459
Tim Petersf3f57472004-08-08 06:11:48 +0000460 >>> from test import doctest_aliases
461 >>> tests = finder.find(doctest_aliases)
Tim Peters8485b562004-08-04 18:46:34 +0000462 >>> tests.sort()
463 >>> print len(tests)
464 2
465 >>> print tests[0].name
Tim Petersf3f57472004-08-08 06:11:48 +0000466 test.doctest_aliases.TwoNames
467
468 TwoNames.f and TwoNames.g are bound to the same object.
469 We can't guess which will be found in doctest's traversal of
470 TwoNames.__dict__ first, so we have to allow for either.
471
472 >>> tests[1].name.split('.')[-1] in ['f', 'g']
Tim Peters8485b562004-08-04 18:46:34 +0000473 True
474
475Filter Functions
476~~~~~~~~~~~~~~~~
Tim Petersf727c6c2004-08-08 01:48:59 +0000477A filter function can be used to restrict which objects get examined,
478but this is temporary, undocumented internal support for testmod's
479deprecated isprivate gimmick.
Tim Peters8485b562004-08-04 18:46:34 +0000480
481 >>> def namefilter(prefix, base):
482 ... return base.startswith('a_')
Tim Petersf727c6c2004-08-08 01:48:59 +0000483 >>> tests = doctest.DocTestFinder(_namefilter=namefilter).find(SampleClass)
Tim Peters8485b562004-08-04 18:46:34 +0000484 >>> tests.sort()
485 >>> for t in tests:
486 ... print '%2s %s' % (len(t.examples), t.name)
487 1 SampleClass
488 3 SampleClass.NestedClass
489 1 SampleClass.NestedClass.__init__
490 1 SampleClass.__init__
491 1 SampleClass.double
492 1 SampleClass.get
493
Tim Peters8485b562004-08-04 18:46:34 +0000494If a given object is filtered out, then none of the objects that it
495contains will be added either:
496
497 >>> def namefilter(prefix, base):
498 ... return base == 'NestedClass'
Tim Petersf727c6c2004-08-08 01:48:59 +0000499 >>> tests = doctest.DocTestFinder(_namefilter=namefilter).find(SampleClass)
Tim Peters8485b562004-08-04 18:46:34 +0000500 >>> tests.sort()
501 >>> for t in tests:
502 ... print '%2s %s' % (len(t.examples), t.name)
503 1 SampleClass
504 1 SampleClass.__init__
505 2 SampleClass.a_classmethod
506 1 SampleClass.a_property
507 1 SampleClass.a_staticmethod
508 1 SampleClass.double
509 1 SampleClass.get
510
Tim Petersf727c6c2004-08-08 01:48:59 +0000511The filter function apply to contained objects, and *not* to the
Tim Peters8485b562004-08-04 18:46:34 +0000512object explicitly passed to DocTestFinder:
513
514 >>> def namefilter(prefix, base):
515 ... return base == 'SampleClass'
Tim Petersf727c6c2004-08-08 01:48:59 +0000516 >>> tests = doctest.DocTestFinder(_namefilter=namefilter).find(SampleClass)
Tim Peters8485b562004-08-04 18:46:34 +0000517 >>> len(tests)
518 9
519
520Turning off Recursion
521~~~~~~~~~~~~~~~~~~~~~
522DocTestFinder can be told not to look for tests in contained objects
523using the `recurse` flag:
524
525 >>> tests = doctest.DocTestFinder(recurse=False).find(SampleClass)
526 >>> tests.sort()
527 >>> for t in tests:
528 ... print '%2s %s' % (len(t.examples), t.name)
529 1 SampleClass
Edward Loperb51b2342004-08-17 16:37:12 +0000530
531Line numbers
532~~~~~~~~~~~~
533DocTestFinder finds the line number of each example:
534
535 >>> def f(x):
536 ... '''
537 ... >>> x = 12
538 ...
539 ... some text
540 ...
541 ... >>> # examples are not created for comments & bare prompts.
542 ... >>>
543 ... ...
544 ...
545 ... >>> for x in range(10):
546 ... ... print x,
547 ... 0 1 2 3 4 5 6 7 8 9
548 ... >>> x/2
549 ... 6
550 ... '''
551 >>> test = doctest.DocTestFinder().find(f)[0]
552 >>> [e.lineno for e in test.examples]
553 [1, 9, 12]
Tim Peters8485b562004-08-04 18:46:34 +0000554"""
555
556class test_DocTestRunner:
557 def basics(): r"""
558Unit tests for the `DocTestRunner` class.
559
560DocTestRunner is used to run DocTest test cases, and to accumulate
561statistics. Here's a simple DocTest case we can use:
562
563 >>> def f(x):
564 ... '''
565 ... >>> x = 12
566 ... >>> print x
567 ... 12
568 ... >>> x/2
569 ... 6
570 ... '''
571 >>> test = doctest.DocTestFinder().find(f)[0]
572
573The main DocTestRunner interface is the `run` method, which runs a
574given DocTest case in a given namespace (globs). It returns a tuple
575`(f,t)`, where `f` is the number of failed tests and `t` is the number
576of tried tests.
577
578 >>> doctest.DocTestRunner(verbose=False).run(test)
579 (0, 3)
580
581If any example produces incorrect output, then the test runner reports
582the failure and proceeds to the next example:
583
584 >>> def f(x):
585 ... '''
586 ... >>> x = 12
587 ... >>> print x
588 ... 14
589 ... >>> x/2
590 ... 6
591 ... '''
592 >>> test = doctest.DocTestFinder().find(f)[0]
593 >>> doctest.DocTestRunner(verbose=True).run(test)
Edward Loperaacf0832004-08-26 01:19:50 +0000594 Trying:
595 x = 12
596 Expecting nothing
Tim Peters8485b562004-08-04 18:46:34 +0000597 ok
Edward Loperaacf0832004-08-26 01:19:50 +0000598 Trying:
599 print x
600 Expecting:
601 14
Tim Peters8485b562004-08-04 18:46:34 +0000602 **********************************************************************
Jim Fulton07a349c2004-08-22 14:10:00 +0000603 Line 3, in f
604 Failed example:
605 print x
606 Expected:
607 14
608 Got:
609 12
Edward Loperaacf0832004-08-26 01:19:50 +0000610 Trying:
611 x/2
612 Expecting:
613 6
Tim Peters8485b562004-08-04 18:46:34 +0000614 ok
615 (1, 3)
616"""
617 def verbose_flag(): r"""
618The `verbose` flag makes the test runner generate more detailed
619output:
620
621 >>> def f(x):
622 ... '''
623 ... >>> x = 12
624 ... >>> print x
625 ... 12
626 ... >>> x/2
627 ... 6
628 ... '''
629 >>> test = doctest.DocTestFinder().find(f)[0]
630
631 >>> doctest.DocTestRunner(verbose=True).run(test)
Edward Loperaacf0832004-08-26 01:19:50 +0000632 Trying:
633 x = 12
634 Expecting nothing
Tim Peters8485b562004-08-04 18:46:34 +0000635 ok
Edward Loperaacf0832004-08-26 01:19:50 +0000636 Trying:
637 print x
638 Expecting:
639 12
Tim Peters8485b562004-08-04 18:46:34 +0000640 ok
Edward Loperaacf0832004-08-26 01:19:50 +0000641 Trying:
642 x/2
643 Expecting:
644 6
Tim Peters8485b562004-08-04 18:46:34 +0000645 ok
646 (0, 3)
647
648If the `verbose` flag is unspecified, then the output will be verbose
649iff `-v` appears in sys.argv:
650
651 >>> # Save the real sys.argv list.
652 >>> old_argv = sys.argv
653
654 >>> # If -v does not appear in sys.argv, then output isn't verbose.
655 >>> sys.argv = ['test']
656 >>> doctest.DocTestRunner().run(test)
657 (0, 3)
658
659 >>> # If -v does appear in sys.argv, then output is verbose.
660 >>> sys.argv = ['test', '-v']
661 >>> doctest.DocTestRunner().run(test)
Edward Loperaacf0832004-08-26 01:19:50 +0000662 Trying:
663 x = 12
664 Expecting nothing
Tim Peters8485b562004-08-04 18:46:34 +0000665 ok
Edward Loperaacf0832004-08-26 01:19:50 +0000666 Trying:
667 print x
668 Expecting:
669 12
Tim Peters8485b562004-08-04 18:46:34 +0000670 ok
Edward Loperaacf0832004-08-26 01:19:50 +0000671 Trying:
672 x/2
673 Expecting:
674 6
Tim Peters8485b562004-08-04 18:46:34 +0000675 ok
676 (0, 3)
677
678 >>> # Restore sys.argv
679 >>> sys.argv = old_argv
680
681In the remaining examples, the test runner's verbosity will be
682explicitly set, to ensure that the test behavior is consistent.
683 """
684 def exceptions(): r"""
685Tests of `DocTestRunner`'s exception handling.
686
687An expected exception is specified with a traceback message. The
688lines between the first line and the type/value may be omitted or
689replaced with any other string:
690
691 >>> def f(x):
692 ... '''
693 ... >>> x = 12
694 ... >>> print x/0
695 ... Traceback (most recent call last):
696 ... ZeroDivisionError: integer division or modulo by zero
697 ... '''
698 >>> test = doctest.DocTestFinder().find(f)[0]
699 >>> doctest.DocTestRunner(verbose=False).run(test)
700 (0, 2)
701
Edward Loper19b19582004-08-25 23:07:03 +0000702An example may not generate output before it raises an exception; if
703it does, then the traceback message will not be recognized as
704signaling an expected exception, so the example will be reported as an
705unexpected exception:
Tim Peters8485b562004-08-04 18:46:34 +0000706
707 >>> def f(x):
708 ... '''
709 ... >>> x = 12
710 ... >>> print 'pre-exception output', x/0
711 ... pre-exception output
712 ... Traceback (most recent call last):
713 ... ZeroDivisionError: integer division or modulo by zero
714 ... '''
715 >>> test = doctest.DocTestFinder().find(f)[0]
716 >>> doctest.DocTestRunner(verbose=False).run(test)
Edward Loper19b19582004-08-25 23:07:03 +0000717 ... # doctest: +ELLIPSIS
718 **********************************************************************
719 Line 3, in f
720 Failed example:
721 print 'pre-exception output', x/0
722 Exception raised:
723 ...
724 ZeroDivisionError: integer division or modulo by zero
725 (1, 2)
Tim Peters8485b562004-08-04 18:46:34 +0000726
727Exception messages may contain newlines:
728
729 >>> def f(x):
730 ... r'''
731 ... >>> raise ValueError, 'multi\nline\nmessage'
732 ... Traceback (most recent call last):
733 ... ValueError: multi
734 ... line
735 ... message
736 ... '''
737 >>> test = doctest.DocTestFinder().find(f)[0]
738 >>> doctest.DocTestRunner(verbose=False).run(test)
739 (0, 1)
740
741If an exception is expected, but an exception with the wrong type or
742message is raised, then it is reported as a failure:
743
744 >>> def f(x):
745 ... r'''
746 ... >>> raise ValueError, 'message'
747 ... Traceback (most recent call last):
748 ... ValueError: wrong message
749 ... '''
750 >>> test = doctest.DocTestFinder().find(f)[0]
751 >>> doctest.DocTestRunner(verbose=False).run(test)
Edward Loper8e4a34b2004-08-12 02:34:27 +0000752 ... # doctest: +ELLIPSIS
Tim Peters8485b562004-08-04 18:46:34 +0000753 **********************************************************************
Jim Fulton07a349c2004-08-22 14:10:00 +0000754 Line 2, in f
755 Failed example:
756 raise ValueError, 'message'
Tim Peters8485b562004-08-04 18:46:34 +0000757 Expected:
758 Traceback (most recent call last):
759 ValueError: wrong message
760 Got:
761 Traceback (most recent call last):
Edward Loper8e4a34b2004-08-12 02:34:27 +0000762 ...
Tim Peters8485b562004-08-04 18:46:34 +0000763 ValueError: message
764 (1, 1)
765
766If an exception is raised but not expected, then it is reported as an
767unexpected exception:
768
Tim Peters8485b562004-08-04 18:46:34 +0000769 >>> def f(x):
770 ... r'''
771 ... >>> 1/0
772 ... 0
773 ... '''
774 >>> test = doctest.DocTestFinder().find(f)[0]
775 >>> doctest.DocTestRunner(verbose=False).run(test)
Edward Loper74bca7a2004-08-12 02:27:44 +0000776 ... # doctest: +ELLIPSIS
Tim Peters8485b562004-08-04 18:46:34 +0000777 **********************************************************************
Jim Fulton07a349c2004-08-22 14:10:00 +0000778 Line 2, in f
779 Failed example:
780 1/0
Tim Peters8485b562004-08-04 18:46:34 +0000781 Exception raised:
782 Traceback (most recent call last):
Jim Fulton07a349c2004-08-22 14:10:00 +0000783 ...
Tim Peters8485b562004-08-04 18:46:34 +0000784 ZeroDivisionError: integer division or modulo by zero
785 (1, 1)
Tim Peters8485b562004-08-04 18:46:34 +0000786"""
787 def optionflags(): r"""
788Tests of `DocTestRunner`'s option flag handling.
789
790Several option flags can be used to customize the behavior of the test
791runner. These are defined as module constants in doctest, and passed
792to the DocTestRunner constructor (multiple constants should be or-ed
793together).
794
795The DONT_ACCEPT_TRUE_FOR_1 flag disables matches between True/False
796and 1/0:
797
798 >>> def f(x):
799 ... '>>> True\n1\n'
800
801 >>> # Without the flag:
802 >>> test = doctest.DocTestFinder().find(f)[0]
803 >>> doctest.DocTestRunner(verbose=False).run(test)
804 (0, 1)
805
806 >>> # With the flag:
807 >>> test = doctest.DocTestFinder().find(f)[0]
808 >>> flags = doctest.DONT_ACCEPT_TRUE_FOR_1
809 >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test)
810 **********************************************************************
Jim Fulton07a349c2004-08-22 14:10:00 +0000811 Line 1, in f
812 Failed example:
813 True
814 Expected:
815 1
816 Got:
817 True
Tim Peters8485b562004-08-04 18:46:34 +0000818 (1, 1)
819
820The DONT_ACCEPT_BLANKLINE flag disables the match between blank lines
821and the '<BLANKLINE>' marker:
822
823 >>> def f(x):
824 ... '>>> print "a\\n\\nb"\na\n<BLANKLINE>\nb\n'
825
826 >>> # Without the flag:
827 >>> test = doctest.DocTestFinder().find(f)[0]
828 >>> doctest.DocTestRunner(verbose=False).run(test)
829 (0, 1)
830
831 >>> # With the flag:
832 >>> test = doctest.DocTestFinder().find(f)[0]
833 >>> flags = doctest.DONT_ACCEPT_BLANKLINE
834 >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test)
835 **********************************************************************
Jim Fulton07a349c2004-08-22 14:10:00 +0000836 Line 1, in f
837 Failed example:
838 print "a\n\nb"
Tim Peters8485b562004-08-04 18:46:34 +0000839 Expected:
840 a
841 <BLANKLINE>
842 b
843 Got:
844 a
845 <BLANKLINE>
846 b
847 (1, 1)
848
849The NORMALIZE_WHITESPACE flag causes all sequences of whitespace to be
850treated as equal:
851
852 >>> def f(x):
853 ... '>>> print 1, 2, 3\n 1 2\n 3'
854
855 >>> # Without the flag:
856 >>> test = doctest.DocTestFinder().find(f)[0]
857 >>> doctest.DocTestRunner(verbose=False).run(test)
858 **********************************************************************
Jim Fulton07a349c2004-08-22 14:10:00 +0000859 Line 1, in f
860 Failed example:
861 print 1, 2, 3
Tim Peters8485b562004-08-04 18:46:34 +0000862 Expected:
863 1 2
864 3
Jim Fulton07a349c2004-08-22 14:10:00 +0000865 Got:
866 1 2 3
Tim Peters8485b562004-08-04 18:46:34 +0000867 (1, 1)
868
869 >>> # With the flag:
870 >>> test = doctest.DocTestFinder().find(f)[0]
871 >>> flags = doctest.NORMALIZE_WHITESPACE
872 >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test)
873 (0, 1)
874
Tim Peters026f8dc2004-08-19 16:38:58 +0000875 An example from the docs:
876 >>> print range(20) #doctest: +NORMALIZE_WHITESPACE
877 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
878 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
879
Tim Peters8485b562004-08-04 18:46:34 +0000880The ELLIPSIS flag causes ellipsis marker ("...") in the expected
881output to match any substring in the actual output:
882
883 >>> def f(x):
884 ... '>>> print range(15)\n[0, 1, 2, ..., 14]\n'
885
886 >>> # Without the flag:
887 >>> test = doctest.DocTestFinder().find(f)[0]
888 >>> doctest.DocTestRunner(verbose=False).run(test)
889 **********************************************************************
Jim Fulton07a349c2004-08-22 14:10:00 +0000890 Line 1, in f
891 Failed example:
892 print range(15)
893 Expected:
894 [0, 1, 2, ..., 14]
895 Got:
896 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
Tim Peters8485b562004-08-04 18:46:34 +0000897 (1, 1)
898
899 >>> # With the flag:
900 >>> test = doctest.DocTestFinder().find(f)[0]
901 >>> flags = doctest.ELLIPSIS
902 >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test)
903 (0, 1)
904
Tim Peterse594bee2004-08-22 01:47:51 +0000905 ... also matches nothing:
Tim Peters1cf3aa62004-08-19 06:49:33 +0000906
907 >>> for i in range(100):
Tim Peterse594bee2004-08-22 01:47:51 +0000908 ... print i**2, #doctest: +ELLIPSIS
909 0 1...4...9 16 ... 36 49 64 ... 9801
Tim Peters1cf3aa62004-08-19 06:49:33 +0000910
Tim Peters026f8dc2004-08-19 16:38:58 +0000911 ... can be surprising; e.g., this test passes:
Tim Peters26b3ebb2004-08-19 08:10:08 +0000912
913 >>> for i in range(21): #doctest: +ELLIPSIS
Tim Peterse594bee2004-08-22 01:47:51 +0000914 ... print i,
915 0 1 2 ...1...2...0
Tim Peters26b3ebb2004-08-19 08:10:08 +0000916
Tim Peters026f8dc2004-08-19 16:38:58 +0000917 Examples from the docs:
918
919 >>> print range(20) # doctest:+ELLIPSIS
920 [0, 1, ..., 18, 19]
921
922 >>> print range(20) # doctest: +ELLIPSIS
923 ... # doctest: +NORMALIZE_WHITESPACE
924 [0, 1, ..., 18, 19]
925
Edward Loper71f55af2004-08-26 01:41:51 +0000926The REPORT_UDIFF flag causes failures that involve multi-line expected
Tim Peters8485b562004-08-04 18:46:34 +0000927and actual outputs to be displayed using a unified diff:
928
929 >>> def f(x):
930 ... r'''
931 ... >>> print '\n'.join('abcdefg')
932 ... a
933 ... B
934 ... c
935 ... d
936 ... f
937 ... g
938 ... h
939 ... '''
940
941 >>> # Without the flag:
942 >>> test = doctest.DocTestFinder().find(f)[0]
943 >>> doctest.DocTestRunner(verbose=False).run(test)
944 **********************************************************************
Jim Fulton07a349c2004-08-22 14:10:00 +0000945 Line 2, in f
946 Failed example:
947 print '\n'.join('abcdefg')
Tim Peters8485b562004-08-04 18:46:34 +0000948 Expected:
949 a
950 B
951 c
952 d
953 f
954 g
955 h
956 Got:
957 a
958 b
959 c
960 d
961 e
962 f
963 g
964 (1, 1)
965
966 >>> # With the flag:
967 >>> test = doctest.DocTestFinder().find(f)[0]
Edward Loper71f55af2004-08-26 01:41:51 +0000968 >>> flags = doctest.REPORT_UDIFF
Tim Peters8485b562004-08-04 18:46:34 +0000969 >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test)
970 **********************************************************************
Jim Fulton07a349c2004-08-22 14:10:00 +0000971 Line 2, in f
972 Failed example:
973 print '\n'.join('abcdefg')
Edward Loper56629292004-08-26 01:31:56 +0000974 Differences (unified diff with -expected +actual):
Tim Peters8485b562004-08-04 18:46:34 +0000975 @@ -1,8 +1,8 @@
976 a
977 -B
978 +b
979 c
980 d
981 +e
982 f
983 g
984 -h
985 <BLANKLINE>
986 (1, 1)
987
Edward Loper71f55af2004-08-26 01:41:51 +0000988The REPORT_CDIFF flag causes failures that involve multi-line expected
Tim Peters8485b562004-08-04 18:46:34 +0000989and actual outputs to be displayed using a context diff:
990
Edward Loper71f55af2004-08-26 01:41:51 +0000991 >>> # Reuse f() from the REPORT_UDIFF example, above.
Tim Peters8485b562004-08-04 18:46:34 +0000992 >>> test = doctest.DocTestFinder().find(f)[0]
Edward Loper71f55af2004-08-26 01:41:51 +0000993 >>> flags = doctest.REPORT_CDIFF
Tim Peters8485b562004-08-04 18:46:34 +0000994 >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test)
995 **********************************************************************
Jim Fulton07a349c2004-08-22 14:10:00 +0000996 Line 2, in f
997 Failed example:
998 print '\n'.join('abcdefg')
Edward Loper56629292004-08-26 01:31:56 +0000999 Differences (context diff with expected followed by actual):
Tim Peters8485b562004-08-04 18:46:34 +00001000 ***************
1001 *** 1,8 ****
1002 a
1003 ! B
1004 c
1005 d
1006 f
1007 g
1008 - h
1009 <BLANKLINE>
1010 --- 1,8 ----
1011 a
1012 ! b
1013 c
1014 d
1015 + e
1016 f
1017 g
1018 <BLANKLINE>
1019 (1, 1)
Tim Petersc6cbab02004-08-22 19:43:28 +00001020
1021
Edward Loper71f55af2004-08-26 01:41:51 +00001022The REPORT_NDIFF flag causes failures to use the difflib.Differ algorithm
Tim Petersc6cbab02004-08-22 19:43:28 +00001023used by the popular ndiff.py utility. This does intraline difference
1024marking, as well as interline differences.
1025
1026 >>> def f(x):
1027 ... r'''
1028 ... >>> print "a b c d e f g h i j k l m"
1029 ... a b c d e f g h i j k 1 m
1030 ... '''
1031 >>> test = doctest.DocTestFinder().find(f)[0]
Edward Loper71f55af2004-08-26 01:41:51 +00001032 >>> flags = doctest.REPORT_NDIFF
Tim Petersc6cbab02004-08-22 19:43:28 +00001033 >>> doctest.DocTestRunner(verbose=False, optionflags=flags).run(test)
1034 **********************************************************************
1035 Line 2, in f
1036 Failed example:
1037 print "a b c d e f g h i j k l m"
1038 Differences (ndiff with -expected +actual):
1039 - a b c d e f g h i j k 1 m
1040 ? ^
1041 + a b c d e f g h i j k l m
1042 ? + ++ ^
1043 <BLANKLINE>
1044 (1, 1)
1045 """
1046
Tim Peters8485b562004-08-04 18:46:34 +00001047 def option_directives(): r"""
1048Tests of `DocTestRunner`'s option directive mechanism.
1049
Edward Loper74bca7a2004-08-12 02:27:44 +00001050Option directives can be used to turn option flags on or off for a
1051single example. To turn an option on for an example, follow that
1052example with a comment of the form ``# doctest: +OPTION``:
Tim Peters8485b562004-08-04 18:46:34 +00001053
1054 >>> def f(x): r'''
Edward Loper74bca7a2004-08-12 02:27:44 +00001055 ... >>> print range(10) # should fail: no ellipsis
1056 ... [0, 1, ..., 9]
1057 ...
1058 ... >>> print range(10) # doctest: +ELLIPSIS
1059 ... [0, 1, ..., 9]
1060 ... '''
1061 >>> test = doctest.DocTestFinder().find(f)[0]
1062 >>> doctest.DocTestRunner(verbose=False).run(test)
1063 **********************************************************************
Jim Fulton07a349c2004-08-22 14:10:00 +00001064 Line 2, in f
1065 Failed example:
1066 print range(10) # should fail: no ellipsis
1067 Expected:
1068 [0, 1, ..., 9]
1069 Got:
1070 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Edward Loper74bca7a2004-08-12 02:27:44 +00001071 (1, 2)
1072
1073To turn an option off for an example, follow that example with a
1074comment of the form ``# doctest: -OPTION``:
1075
1076 >>> def f(x): r'''
1077 ... >>> print range(10)
1078 ... [0, 1, ..., 9]
1079 ...
1080 ... >>> # should fail: no ellipsis
1081 ... >>> print range(10) # doctest: -ELLIPSIS
1082 ... [0, 1, ..., 9]
1083 ... '''
1084 >>> test = doctest.DocTestFinder().find(f)[0]
1085 >>> doctest.DocTestRunner(verbose=False,
1086 ... optionflags=doctest.ELLIPSIS).run(test)
1087 **********************************************************************
Jim Fulton07a349c2004-08-22 14:10:00 +00001088 Line 6, in f
1089 Failed example:
1090 print range(10) # doctest: -ELLIPSIS
1091 Expected:
1092 [0, 1, ..., 9]
1093 Got:
1094 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Edward Loper74bca7a2004-08-12 02:27:44 +00001095 (1, 2)
1096
1097Option directives affect only the example that they appear with; they
1098do not change the options for surrounding examples:
Edward Loper8e4a34b2004-08-12 02:34:27 +00001099
Edward Loper74bca7a2004-08-12 02:27:44 +00001100 >>> def f(x): r'''
Tim Peters8485b562004-08-04 18:46:34 +00001101 ... >>> print range(10) # Should fail: no ellipsis
1102 ... [0, 1, ..., 9]
1103 ...
Edward Loper74bca7a2004-08-12 02:27:44 +00001104 ... >>> print range(10) # doctest: +ELLIPSIS
Tim Peters8485b562004-08-04 18:46:34 +00001105 ... [0, 1, ..., 9]
1106 ...
Tim Peters8485b562004-08-04 18:46:34 +00001107 ... >>> print range(10) # Should fail: no ellipsis
1108 ... [0, 1, ..., 9]
1109 ... '''
1110 >>> test = doctest.DocTestFinder().find(f)[0]
1111 >>> doctest.DocTestRunner(verbose=False).run(test)
1112 **********************************************************************
Jim Fulton07a349c2004-08-22 14:10:00 +00001113 Line 2, in f
1114 Failed example:
1115 print range(10) # Should fail: no ellipsis
1116 Expected:
1117 [0, 1, ..., 9]
1118 Got:
1119 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Tim Peters8485b562004-08-04 18:46:34 +00001120 **********************************************************************
Jim Fulton07a349c2004-08-22 14:10:00 +00001121 Line 8, in f
1122 Failed example:
1123 print range(10) # Should fail: no ellipsis
1124 Expected:
1125 [0, 1, ..., 9]
1126 Got:
1127 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Tim Peters8485b562004-08-04 18:46:34 +00001128 (2, 3)
1129
Edward Loper74bca7a2004-08-12 02:27:44 +00001130Multiple options may be modified by a single option directive. They
1131may be separated by whitespace, commas, or both:
Tim Peters8485b562004-08-04 18:46:34 +00001132
1133 >>> def f(x): r'''
1134 ... >>> print range(10) # Should fail
1135 ... [0, 1, ..., 9]
Tim Peters8485b562004-08-04 18:46:34 +00001136 ... >>> print range(10) # Should succeed
Edward Loper74bca7a2004-08-12 02:27:44 +00001137 ... ... # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
Tim Peters8485b562004-08-04 18:46:34 +00001138 ... [0, 1, ..., 9]
1139 ... '''
1140 >>> test = doctest.DocTestFinder().find(f)[0]
1141 >>> doctest.DocTestRunner(verbose=False).run(test)
1142 **********************************************************************
Jim Fulton07a349c2004-08-22 14:10:00 +00001143 Line 2, in f
1144 Failed example:
1145 print range(10) # Should fail
1146 Expected:
1147 [0, 1, ..., 9]
1148 Got:
1149 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Tim Peters8485b562004-08-04 18:46:34 +00001150 (1, 2)
Edward Loper74bca7a2004-08-12 02:27:44 +00001151
1152 >>> def f(x): r'''
1153 ... >>> print range(10) # Should fail
1154 ... [0, 1, ..., 9]
1155 ... >>> print range(10) # Should succeed
1156 ... ... # doctest: +ELLIPSIS,+NORMALIZE_WHITESPACE
1157 ... [0, 1, ..., 9]
1158 ... '''
1159 >>> test = doctest.DocTestFinder().find(f)[0]
1160 >>> doctest.DocTestRunner(verbose=False).run(test)
1161 **********************************************************************
Jim Fulton07a349c2004-08-22 14:10:00 +00001162 Line 2, in f
1163 Failed example:
1164 print range(10) # Should fail
1165 Expected:
1166 [0, 1, ..., 9]
1167 Got:
1168 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Edward Loper74bca7a2004-08-12 02:27:44 +00001169 (1, 2)
1170
1171 >>> def f(x): r'''
1172 ... >>> print range(10) # Should fail
1173 ... [0, 1, ..., 9]
1174 ... >>> print range(10) # Should succeed
1175 ... ... # doctest: +ELLIPSIS, +NORMALIZE_WHITESPACE
1176 ... [0, 1, ..., 9]
1177 ... '''
1178 >>> test = doctest.DocTestFinder().find(f)[0]
1179 >>> doctest.DocTestRunner(verbose=False).run(test)
1180 **********************************************************************
Jim Fulton07a349c2004-08-22 14:10:00 +00001181 Line 2, in f
1182 Failed example:
1183 print range(10) # Should fail
1184 Expected:
1185 [0, 1, ..., 9]
1186 Got:
1187 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Edward Loper74bca7a2004-08-12 02:27:44 +00001188 (1, 2)
1189
1190The option directive may be put on the line following the source, as
1191long as a continuation prompt is used:
1192
1193 >>> def f(x): r'''
1194 ... >>> print range(10)
1195 ... ... # doctest: +ELLIPSIS
1196 ... [0, 1, ..., 9]
1197 ... '''
1198 >>> test = doctest.DocTestFinder().find(f)[0]
1199 >>> doctest.DocTestRunner(verbose=False).run(test)
1200 (0, 1)
Edward Loper8e4a34b2004-08-12 02:34:27 +00001201
Edward Loper74bca7a2004-08-12 02:27:44 +00001202For examples with multi-line source, the option directive may appear
1203at the end of any line:
1204
1205 >>> def f(x): r'''
1206 ... >>> for x in range(10): # doctest: +ELLIPSIS
1207 ... ... print x,
1208 ... 0 1 2 ... 9
1209 ...
1210 ... >>> for x in range(10):
1211 ... ... print x, # doctest: +ELLIPSIS
1212 ... 0 1 2 ... 9
1213 ... '''
1214 >>> test = doctest.DocTestFinder().find(f)[0]
1215 >>> doctest.DocTestRunner(verbose=False).run(test)
1216 (0, 2)
1217
1218If more than one line of an example with multi-line source has an
1219option directive, then they are combined:
1220
1221 >>> def f(x): r'''
1222 ... Should fail (option directive not on the last line):
1223 ... >>> for x in range(10): # doctest: +ELLIPSIS
1224 ... ... print x, # doctest: +NORMALIZE_WHITESPACE
1225 ... 0 1 2...9
1226 ... '''
1227 >>> test = doctest.DocTestFinder().find(f)[0]
1228 >>> doctest.DocTestRunner(verbose=False).run(test)
1229 (0, 1)
1230
1231It is an error to have a comment of the form ``# doctest:`` that is
1232*not* followed by words of the form ``+OPTION`` or ``-OPTION``, where
1233``OPTION`` is an option that has been registered with
1234`register_option`:
1235
1236 >>> # Error: Option not registered
1237 >>> s = '>>> print 12 #doctest: +BADOPTION'
1238 >>> test = doctest.DocTestParser().get_doctest(s, {}, 's', 's.py', 0)
1239 Traceback (most recent call last):
1240 ValueError: line 1 of the doctest for s has an invalid option: '+BADOPTION'
1241
1242 >>> # Error: No + or - prefix
1243 >>> s = '>>> print 12 #doctest: ELLIPSIS'
1244 >>> test = doctest.DocTestParser().get_doctest(s, {}, 's', 's.py', 0)
1245 Traceback (most recent call last):
1246 ValueError: line 1 of the doctest for s has an invalid option: 'ELLIPSIS'
1247
1248It is an error to use an option directive on a line that contains no
1249source:
1250
1251 >>> s = '>>> # doctest: +ELLIPSIS'
1252 >>> test = doctest.DocTestParser().get_doctest(s, {}, 's', 's.py', 0)
1253 Traceback (most recent call last):
1254 ValueError: line 0 of the doctest for s has an option directive on a line with no example: '# doctest: +ELLIPSIS'
Tim Peters8485b562004-08-04 18:46:34 +00001255"""
1256
1257def test_testsource(): r"""
1258Unit tests for `testsource()`.
1259
1260The testsource() function takes a module and a name, finds the (first)
Tim Peters19397e52004-08-06 22:02:59 +00001261test with that name in that module, and converts it to a script. The
1262example code is converted to regular Python code. The surrounding
1263words and expected output are converted to comments:
Tim Peters8485b562004-08-04 18:46:34 +00001264
1265 >>> import test.test_doctest
1266 >>> name = 'test.test_doctest.sample_func'
1267 >>> print doctest.testsource(test.test_doctest, name)
Edward Lopera5db6002004-08-12 02:41:30 +00001268 # Blah blah
Tim Peters19397e52004-08-06 22:02:59 +00001269 #
Tim Peters8485b562004-08-04 18:46:34 +00001270 print sample_func(22)
1271 # Expected:
Edward Lopera5db6002004-08-12 02:41:30 +00001272 ## 44
Tim Peters19397e52004-08-06 22:02:59 +00001273 #
Edward Lopera5db6002004-08-12 02:41:30 +00001274 # Yee ha!
Tim Peters8485b562004-08-04 18:46:34 +00001275
1276 >>> name = 'test.test_doctest.SampleNewStyleClass'
1277 >>> print doctest.testsource(test.test_doctest, name)
1278 print '1\n2\n3'
1279 # Expected:
Edward Lopera5db6002004-08-12 02:41:30 +00001280 ## 1
1281 ## 2
1282 ## 3
Tim Peters8485b562004-08-04 18:46:34 +00001283
1284 >>> name = 'test.test_doctest.SampleClass.a_classmethod'
1285 >>> print doctest.testsource(test.test_doctest, name)
1286 print SampleClass.a_classmethod(10)
1287 # Expected:
Edward Lopera5db6002004-08-12 02:41:30 +00001288 ## 12
Tim Peters8485b562004-08-04 18:46:34 +00001289 print SampleClass(0).a_classmethod(10)
1290 # Expected:
Edward Lopera5db6002004-08-12 02:41:30 +00001291 ## 12
Tim Peters8485b562004-08-04 18:46:34 +00001292"""
1293
1294def test_debug(): r"""
1295
1296Create a docstring that we want to debug:
1297
1298 >>> s = '''
1299 ... >>> x = 12
1300 ... >>> print x
1301 ... 12
1302 ... '''
1303
1304Create some fake stdin input, to feed to the debugger:
1305
1306 >>> import tempfile
1307 >>> fake_stdin = tempfile.TemporaryFile(mode='w+')
1308 >>> fake_stdin.write('\n'.join(['next', 'print x', 'continue', '']))
1309 >>> fake_stdin.seek(0)
1310 >>> real_stdin = sys.stdin
1311 >>> sys.stdin = fake_stdin
1312
1313Run the debugger on the docstring, and then restore sys.stdin.
1314
Tim Peters8485b562004-08-04 18:46:34 +00001315 >>> try:
1316 ... doctest.debug_src(s)
1317 ... finally:
1318 ... sys.stdin = real_stdin
1319 ... fake_stdin.close()
Edward Loper74bca7a2004-08-12 02:27:44 +00001320 ... # doctest: +NORMALIZE_WHITESPACE
Tim Peters8485b562004-08-04 18:46:34 +00001321 > <string>(1)?()
1322 (Pdb) 12
1323 --Return--
1324 > <string>(1)?()->None
1325 (Pdb) 12
1326 (Pdb)
1327
1328"""
1329
Jim Fulton356fd192004-08-09 11:34:47 +00001330def test_pdb_set_trace():
1331 r"""Using pdb.set_trace from a doctest
1332
Tim Peters413ced62004-08-09 15:43:47 +00001333 You can use pdb.set_trace from a doctest. To do so, you must
Jim Fulton356fd192004-08-09 11:34:47 +00001334 retrieve the set_trace function from the pdb module at the time
Tim Peters413ced62004-08-09 15:43:47 +00001335 you use it. The doctest module changes sys.stdout so that it can
1336 capture program output. It also temporarily replaces pdb.set_trace
1337 with a version that restores stdout. This is necessary for you to
Jim Fulton356fd192004-08-09 11:34:47 +00001338 see debugger output.
1339
1340 >>> doc = '''
1341 ... >>> x = 42
1342 ... >>> import pdb; pdb.set_trace()
1343 ... '''
Edward Lopera1ef6112004-08-09 16:14:41 +00001344 >>> parser = doctest.DocTestParser()
1345 >>> test = parser.get_doctest(doc, {}, "foo", "foo.py", 0)
Jim Fulton356fd192004-08-09 11:34:47 +00001346 >>> runner = doctest.DocTestRunner(verbose=False)
1347
1348 To demonstrate this, we'll create a fake standard input that
1349 captures our debugger input:
1350
1351 >>> import tempfile
1352 >>> fake_stdin = tempfile.TemporaryFile(mode='w+')
1353 >>> fake_stdin.write('\n'.join([
1354 ... 'up', # up out of pdb.set_trace
1355 ... 'up', # up again to get out of our wrapper
1356 ... 'print x', # print data defined by the example
1357 ... 'continue', # stop debugging
1358 ... '']))
1359 >>> fake_stdin.seek(0)
1360 >>> real_stdin = sys.stdin
1361 >>> sys.stdin = fake_stdin
1362
Edward Loper74bca7a2004-08-12 02:27:44 +00001363 >>> runner.run(test) # doctest: +ELLIPSIS
Jim Fulton356fd192004-08-09 11:34:47 +00001364 --Return--
1365 > ...set_trace()->None
1366 -> Pdb().set_trace()
1367 (Pdb) > ...set_trace()
1368 -> real_pdb_set_trace()
1369 (Pdb) > <string>(1)?()
1370 (Pdb) 42
1371 (Pdb) (0, 2)
1372
1373 >>> sys.stdin = real_stdin
1374 >>> fake_stdin.close()
1375
1376 You can also put pdb.set_trace in a function called from a test:
1377
1378 >>> def calls_set_trace():
1379 ... y=2
1380 ... import pdb; pdb.set_trace()
1381
1382 >>> doc = '''
1383 ... >>> x=1
1384 ... >>> calls_set_trace()
1385 ... '''
Edward Lopera1ef6112004-08-09 16:14:41 +00001386 >>> test = parser.get_doctest(doc, globals(), "foo", "foo.py", 0)
Jim Fulton356fd192004-08-09 11:34:47 +00001387 >>> fake_stdin = tempfile.TemporaryFile(mode='w+')
1388 >>> fake_stdin.write('\n'.join([
1389 ... 'up', # up out of pdb.set_trace
1390 ... 'up', # up again to get out of our wrapper
1391 ... 'print y', # print data defined in the function
1392 ... 'up', # out of function
1393 ... 'print x', # print data defined by the example
1394 ... 'continue', # stop debugging
1395 ... '']))
1396 >>> fake_stdin.seek(0)
1397 >>> real_stdin = sys.stdin
1398 >>> sys.stdin = fake_stdin
1399
Edward Loper74bca7a2004-08-12 02:27:44 +00001400 >>> runner.run(test) # doctest: +ELLIPSIS
Jim Fulton356fd192004-08-09 11:34:47 +00001401 --Return--
1402 > ...set_trace()->None
1403 -> Pdb().set_trace()
1404 (Pdb) ...set_trace()
1405 -> real_pdb_set_trace()
1406 (Pdb) > <string>(3)calls_set_trace()
1407 (Pdb) 2
1408 (Pdb) > <string>(1)?()
1409 (Pdb) 1
1410 (Pdb) (0, 2)
Jim Fulton356fd192004-08-09 11:34:47 +00001411 """
1412
Tim Peters19397e52004-08-06 22:02:59 +00001413def test_DocTestSuite():
Tim Peters1e277ee2004-08-07 05:37:52 +00001414 """DocTestSuite creates a unittest test suite from a doctest.
Tim Peters19397e52004-08-06 22:02:59 +00001415
1416 We create a Suite by providing a module. A module can be provided
1417 by passing a module object:
1418
1419 >>> import unittest
1420 >>> import test.sample_doctest
1421 >>> suite = doctest.DocTestSuite(test.sample_doctest)
1422 >>> suite.run(unittest.TestResult())
Tim Peters1e277ee2004-08-07 05:37:52 +00001423 <unittest.TestResult run=9 errors=0 failures=4>
Tim Peters19397e52004-08-06 22:02:59 +00001424
1425 We can also supply the module by name:
1426
1427 >>> suite = doctest.DocTestSuite('test.sample_doctest')
1428 >>> suite.run(unittest.TestResult())
Tim Peters1e277ee2004-08-07 05:37:52 +00001429 <unittest.TestResult run=9 errors=0 failures=4>
Tim Peters19397e52004-08-06 22:02:59 +00001430
1431 We can use the current module:
1432
1433 >>> suite = test.sample_doctest.test_suite()
1434 >>> suite.run(unittest.TestResult())
Tim Peters1e277ee2004-08-07 05:37:52 +00001435 <unittest.TestResult run=9 errors=0 failures=4>
Tim Peters19397e52004-08-06 22:02:59 +00001436
1437 We can supply global variables. If we pass globs, they will be
1438 used instead of the module globals. Here we'll pass an empty
1439 globals, triggering an extra error:
1440
1441 >>> suite = doctest.DocTestSuite('test.sample_doctest', globs={})
1442 >>> suite.run(unittest.TestResult())
Tim Peters1e277ee2004-08-07 05:37:52 +00001443 <unittest.TestResult run=9 errors=0 failures=5>
Tim Peters19397e52004-08-06 22:02:59 +00001444
1445 Alternatively, we can provide extra globals. Here we'll make an
1446 error go away by providing an extra global variable:
1447
1448 >>> suite = doctest.DocTestSuite('test.sample_doctest',
1449 ... extraglobs={'y': 1})
1450 >>> suite.run(unittest.TestResult())
Tim Peters1e277ee2004-08-07 05:37:52 +00001451 <unittest.TestResult run=9 errors=0 failures=3>
Tim Peters19397e52004-08-06 22:02:59 +00001452
1453 You can pass option flags. Here we'll cause an extra error
1454 by disabling the blank-line feature:
1455
1456 >>> suite = doctest.DocTestSuite('test.sample_doctest',
Tim Peters1e277ee2004-08-07 05:37:52 +00001457 ... optionflags=doctest.DONT_ACCEPT_BLANKLINE)
Tim Peters19397e52004-08-06 22:02:59 +00001458 >>> suite.run(unittest.TestResult())
Tim Peters1e277ee2004-08-07 05:37:52 +00001459 <unittest.TestResult run=9 errors=0 failures=5>
Tim Peters19397e52004-08-06 22:02:59 +00001460
Tim Peters1e277ee2004-08-07 05:37:52 +00001461 You can supply setUp and tearDown functions:
Tim Peters19397e52004-08-06 22:02:59 +00001462
1463 >>> def setUp():
1464 ... import test.test_doctest
1465 ... test.test_doctest.sillySetup = True
1466
1467 >>> def tearDown():
1468 ... import test.test_doctest
1469 ... del test.test_doctest.sillySetup
1470
1471 Here, we installed a silly variable that the test expects:
1472
1473 >>> suite = doctest.DocTestSuite('test.sample_doctest',
1474 ... setUp=setUp, tearDown=tearDown)
1475 >>> suite.run(unittest.TestResult())
Tim Peters1e277ee2004-08-07 05:37:52 +00001476 <unittest.TestResult run=9 errors=0 failures=3>
Tim Peters19397e52004-08-06 22:02:59 +00001477
1478 But the tearDown restores sanity:
1479
1480 >>> import test.test_doctest
1481 >>> test.test_doctest.sillySetup
1482 Traceback (most recent call last):
1483 ...
1484 AttributeError: 'module' object has no attribute 'sillySetup'
1485
1486 Finally, you can provide an alternate test finder. Here we'll
Tim Peters1e277ee2004-08-07 05:37:52 +00001487 use a custom test_finder to to run just the test named bar.
1488 However, the test in the module docstring, and the two tests
1489 in the module __test__ dict, aren't filtered, so we actually
1490 run three tests besides bar's. The filtering mechanisms are
1491 poorly conceived, and will go away someday.
Tim Peters19397e52004-08-06 22:02:59 +00001492
1493 >>> finder = doctest.DocTestFinder(
Tim Petersf727c6c2004-08-08 01:48:59 +00001494 ... _namefilter=lambda prefix, base: base!='bar')
Tim Peters19397e52004-08-06 22:02:59 +00001495 >>> suite = doctest.DocTestSuite('test.sample_doctest',
1496 ... test_finder=finder)
1497 >>> suite.run(unittest.TestResult())
Tim Peters1e277ee2004-08-07 05:37:52 +00001498 <unittest.TestResult run=4 errors=0 failures=1>
Tim Peters19397e52004-08-06 22:02:59 +00001499 """
1500
1501def test_DocFileSuite():
1502 """We can test tests found in text files using a DocFileSuite.
1503
1504 We create a suite by providing the names of one or more text
1505 files that include examples:
1506
1507 >>> import unittest
1508 >>> suite = doctest.DocFileSuite('test_doctest.txt',
1509 ... 'test_doctest2.txt')
1510 >>> suite.run(unittest.TestResult())
1511 <unittest.TestResult run=2 errors=0 failures=2>
1512
1513 The test files are looked for in the directory containing the
1514 calling module. A package keyword argument can be provided to
1515 specify a different relative location.
1516
1517 >>> import unittest
1518 >>> suite = doctest.DocFileSuite('test_doctest.txt',
1519 ... 'test_doctest2.txt',
1520 ... package='test')
1521 >>> suite.run(unittest.TestResult())
1522 <unittest.TestResult run=2 errors=0 failures=2>
1523
1524 Note that '/' should be used as a path separator. It will be
1525 converted to a native separator at run time:
1526
1527
1528 >>> suite = doctest.DocFileSuite('../test/test_doctest.txt')
1529 >>> suite.run(unittest.TestResult())
1530 <unittest.TestResult run=1 errors=0 failures=1>
1531
1532 You can specify initial global variables:
1533
1534 >>> suite = doctest.DocFileSuite('test_doctest.txt',
1535 ... 'test_doctest2.txt',
1536 ... globs={'favorite_color': 'blue'})
1537 >>> suite.run(unittest.TestResult())
1538 <unittest.TestResult run=2 errors=0 failures=1>
1539
1540 In this case, we supplied a missing favorite color. You can
1541 provide doctest options:
1542
1543 >>> suite = doctest.DocFileSuite('test_doctest.txt',
1544 ... 'test_doctest2.txt',
1545 ... optionflags=doctest.DONT_ACCEPT_BLANKLINE,
1546 ... globs={'favorite_color': 'blue'})
1547 >>> suite.run(unittest.TestResult())
1548 <unittest.TestResult run=2 errors=0 failures=2>
1549
1550 And, you can provide setUp and tearDown functions:
1551
1552 You can supply setUp and teatDoen functions:
1553
1554 >>> def setUp():
1555 ... import test.test_doctest
1556 ... test.test_doctest.sillySetup = True
1557
1558 >>> def tearDown():
1559 ... import test.test_doctest
1560 ... del test.test_doctest.sillySetup
1561
1562 Here, we installed a silly variable that the test expects:
1563
1564 >>> suite = doctest.DocFileSuite('test_doctest.txt',
1565 ... 'test_doctest2.txt',
1566 ... setUp=setUp, tearDown=tearDown)
1567 >>> suite.run(unittest.TestResult())
1568 <unittest.TestResult run=2 errors=0 failures=1>
1569
1570 But the tearDown restores sanity:
1571
1572 >>> import test.test_doctest
1573 >>> test.test_doctest.sillySetup
1574 Traceback (most recent call last):
1575 ...
1576 AttributeError: 'module' object has no attribute 'sillySetup'
1577
1578 """
1579
Jim Fulton07a349c2004-08-22 14:10:00 +00001580def test_trailing_space_in_test():
1581 """
Tim Petersa7def722004-08-23 22:13:22 +00001582 Trailing spaces in expected output are significant:
Tim Petersc6cbab02004-08-22 19:43:28 +00001583
Jim Fulton07a349c2004-08-22 14:10:00 +00001584 >>> x, y = 'foo', ''
1585 >>> print x, y
1586 foo \n
1587 """
Tim Peters19397e52004-08-06 22:02:59 +00001588
Tim Petersa7def722004-08-23 22:13:22 +00001589# old_test1, ... used to live in doctest.py, but cluttered it. Note
1590# that these use the deprecated doctest.Tester, so should go away (or
1591# be rewritten) someday.
1592
1593# Ignore all warnings about the use of class Tester in this module.
1594# Note that the name of this module may differ depending on how it's
1595# imported, so the use of __name__ is important.
1596warnings.filterwarnings("ignore", "class Tester", DeprecationWarning,
1597 __name__, 0)
1598
1599def old_test1(): r"""
1600>>> from doctest import Tester
1601>>> t = Tester(globs={'x': 42}, verbose=0)
1602>>> t.runstring(r'''
1603... >>> x = x * 2
1604... >>> print x
1605... 42
1606... ''', 'XYZ')
1607**********************************************************************
1608Line 3, in XYZ
1609Failed example:
1610 print x
1611Expected:
1612 42
1613Got:
1614 84
1615(1, 2)
1616>>> t.runstring(">>> x = x * 2\n>>> print x\n84\n", 'example2')
1617(0, 2)
1618>>> t.summarize()
1619**********************************************************************
16201 items had failures:
1621 1 of 2 in XYZ
1622***Test Failed*** 1 failures.
1623(1, 4)
1624>>> t.summarize(verbose=1)
16251 items passed all tests:
1626 2 tests in example2
1627**********************************************************************
16281 items had failures:
1629 1 of 2 in XYZ
16304 tests in 2 items.
16313 passed and 1 failed.
1632***Test Failed*** 1 failures.
1633(1, 4)
1634"""
1635
1636def old_test2(): r"""
1637 >>> from doctest import Tester
1638 >>> t = Tester(globs={}, verbose=1)
1639 >>> test = r'''
1640 ... # just an example
1641 ... >>> x = 1 + 2
1642 ... >>> x
1643 ... 3
1644 ... '''
1645 >>> t.runstring(test, "Example")
1646 Running string Example
Edward Loperaacf0832004-08-26 01:19:50 +00001647 Trying:
1648 x = 1 + 2
1649 Expecting nothing
Tim Petersa7def722004-08-23 22:13:22 +00001650 ok
Edward Loperaacf0832004-08-26 01:19:50 +00001651 Trying:
1652 x
1653 Expecting:
1654 3
Tim Petersa7def722004-08-23 22:13:22 +00001655 ok
1656 0 of 2 examples failed in string Example
1657 (0, 2)
1658"""
1659
1660def old_test3(): r"""
1661 >>> from doctest import Tester
1662 >>> t = Tester(globs={}, verbose=0)
1663 >>> def _f():
1664 ... '''Trivial docstring example.
1665 ... >>> assert 2 == 2
1666 ... '''
1667 ... return 32
1668 ...
1669 >>> t.rundoc(_f) # expect 0 failures in 1 example
1670 (0, 1)
1671"""
1672
1673def old_test4(): """
1674 >>> import new
1675 >>> m1 = new.module('_m1')
1676 >>> m2 = new.module('_m2')
1677 >>> test_data = \"""
1678 ... def _f():
1679 ... '''>>> assert 1 == 1
1680 ... '''
1681 ... def g():
1682 ... '''>>> assert 2 != 1
1683 ... '''
1684 ... class H:
1685 ... '''>>> assert 2 > 1
1686 ... '''
1687 ... def bar(self):
1688 ... '''>>> assert 1 < 2
1689 ... '''
1690 ... \"""
1691 >>> exec test_data in m1.__dict__
1692 >>> exec test_data in m2.__dict__
1693 >>> m1.__dict__.update({"f2": m2._f, "g2": m2.g, "h2": m2.H})
1694
1695 Tests that objects outside m1 are excluded:
1696
1697 >>> from doctest import Tester
1698 >>> t = Tester(globs={}, verbose=0)
1699 >>> t.rundict(m1.__dict__, "rundict_test", m1) # f2 and g2 and h2 skipped
1700 (0, 4)
1701
1702 Once more, not excluding stuff outside m1:
1703
1704 >>> t = Tester(globs={}, verbose=0)
1705 >>> t.rundict(m1.__dict__, "rundict_test_pvt") # None are skipped.
1706 (0, 8)
1707
1708 The exclusion of objects from outside the designated module is
1709 meant to be invoked automagically by testmod.
1710
1711 >>> doctest.testmod(m1, verbose=False)
1712 (0, 4)
1713"""
1714
Tim Peters8485b562004-08-04 18:46:34 +00001715######################################################################
1716## Main
1717######################################################################
1718
1719def test_main():
1720 # Check the doctest cases in doctest itself:
1721 test_support.run_doctest(doctest, verbosity=True)
1722 # Check the doctest cases defined here:
1723 from test import test_doctest
1724 test_support.run_doctest(test_doctest, verbosity=True)
1725
1726import trace, sys, re, StringIO
1727def test_coverage(coverdir):
1728 tracer = trace.Trace(ignoredirs=[sys.prefix, sys.exec_prefix,],
1729 trace=0, count=1)
1730 tracer.run('reload(doctest); test_main()')
1731 r = tracer.results()
1732 print 'Writing coverage results...'
1733 r.write_results(show_missing=True, summary=True,
1734 coverdir=coverdir)
1735
1736if __name__ == '__main__':
1737 if '-c' in sys.argv:
1738 test_coverage('/tmp/doctest.cover')
1739 else:
1740 test_main()