~malept/ubuntu/lucid/python2.6/dev-dependency-fix

« back to all changes in this revision

Viewing changes to Lib/test/test_doctest.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2009-02-13 12:51:00 UTC
  • Revision ID: james.westby@ubuntu.com-20090213125100-uufgcb9yeqzujpqw
Tags: upstream-2.6.1
ImportĀ upstreamĀ versionĀ 2.6.1

Show diffs side-by-side

added added

removed removed

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