66
77# Provided as-is; use at your own risk; no warranty; no promises; enjoy!
88
9- # [XX] This docstring is out-of-date:
109r"""Module doctest -- a framework for running examples in docstrings.
1110
1211NORMAL USAGE
1312
14- In normal use, end each module M with:
13+ In simplest use, end each module M to be tested with:
1514
1615def _test():
17- import doctest, M # replace M with your module's name
18- return doctest.testmod(M) # ditto
16+ import doctest
17+ return doctest.testmod()
1918
2019if __name__ == "__main__":
2120 _test()
@@ -37,14 +36,20 @@ def _test():
3736and a detailed report of all examples tried is printed to stdout, along
3837with assorted summaries at the end.
3938
40- You can force verbose mode by passing "verbose=1 " to testmod, or prohibit
41- it by passing "verbose=0 ". In either of those cases, sys.argv is not
39+ You can force verbose mode by passing "verbose=True " to testmod, or prohibit
40+ it by passing "verbose=False ". In either of those cases, sys.argv is not
4241examined by testmod.
4342
4443In any case, testmod returns a 2-tuple of ints (f, t), where f is the
4544number of docstring examples that failed and t is the total number of
4645docstring examples attempted.
4746
47+ There are a variety of other ways to run doctests, including integration
48+ with the unittest framework, and support for running non-Python text
49+ files containing doctests. There are also many ways to override parts
50+ of doctest's default behaviors. See the Library Reference Manual for
51+ details.
52+
4853
4954WHICH DOCSTRINGS ARE EXAMINED?
5055
@@ -59,27 +64,13 @@ def _test():
5964+ If M.__test__ exists and "is true", it must be a dict, and
6065 each entry maps a (string) name to a function object, class object, or
6166 string. Function and class object docstrings found from M.__test__
62- are searched even if the name is private, and strings are searched
63- directly as if they were docstrings. In output, a key K in M.__test__
64- appears with name
67+ are searched, and strings are searched directly as if they were docstrings.
68+ In output, a key K in M.__test__ appears with name
6569 <name of M>.__test__.K
6670
6771Any classes found are recursively searched similarly, to test docstrings in
68- their contained methods and nested classes. All names reached from
69- M.__test__ are searched.
70-
71- Optionally, functions with private names can be skipped (unless listed in
72- M.__test__) by supplying a function to the "isprivate" argument that will
73- identify private functions. For convenience, one such function is
74- supplied. docttest.is_private considers a name to be private if it begins
75- with an underscore (like "_my_func") but doesn't both begin and end with
76- (at least) two underscores (like "__init__"). By supplying this function
77- or your own "isprivate" function to testmod, the behavior can be customized.
78-
79- If you want to test docstrings in objects with private names too, stuff
80- them into an M.__test__ dict, or see ADVANCED USAGE below (e.g., pass your
81- own isprivate function to Tester's constructor, or call the rundoc method
82- of a Tester instance).
72+ their contained methods and nested classes.
73+
8374
8475WHAT'S THE EXECUTION CONTEXT?
8576
@@ -96,48 +87,6 @@ def _test():
9687M.__dict__ merged with the globals from other imported modules.
9788
9889
99- WHAT IF I WANT TO TEST A WHOLE PACKAGE?
100-
101- Piece o' cake, provided the modules do their testing from docstrings.
102- Here's the test.py I use for the world's most elaborate Rational/
103- floating-base-conversion pkg (which I'll distribute some day):
104-
105- from Rational import Cvt
106- from Rational import Format
107- from Rational import machprec
108- from Rational import Rat
109- from Rational import Round
110- from Rational import utils
111-
112- modules = (Cvt,
113- Format,
114- machprec,
115- Rat,
116- Round,
117- utils)
118-
119- def _test():
120- import doctest
121- import sys
122- verbose = "-v" in sys.argv
123- for mod in modules:
124- doctest.testmod(mod, verbose=verbose, report=0)
125- doctest.master.summarize()
126-
127- if __name__ == "__main__":
128- _test()
129-
130- IOW, it just runs testmod on all the pkg modules. testmod remembers the
131- names and outcomes (# of failures, # of tries) for each item it's seen, and
132- passing "report=0" prevents it from printing a summary in verbose mode.
133- Instead, the summary is delayed until all modules have been tested, and
134- then "doctest.master.summarize()" forces the summary at the end.
135-
136- So this is very nice in practice: each module can be tested individually
137- with almost no work beyond writing up docstring examples, and collections
138- of modules can be tested too as a unit with no more work than the above.
139-
140-
14190WHAT ABOUT EXCEPTIONS?
14291
14392No problem, as long as the only output generated by the example is the
@@ -149,25 +98,10 @@ def _test():
14998 ValueError: list.remove(x): x not in list
15099 >>>
151100
152- Note that only the exception type and value are compared (specifically,
153- only the last line in the traceback).
154-
101+ Note that only the exception type and value are compared.
155102
156- ADVANCED USAGE
157103
158- doctest.testmod() captures the testing policy I find most useful most
159- often. You may want other policies.
160-
161- testmod() actually creates a local instance of class doctest.Tester, runs
162- appropriate methods of that class, and merges the results into global
163- Tester instance doctest.master.
164-
165- You can create your own instances of doctest.Tester, and so build your own
166- policies, or even run methods of doctest.master directly. See
167- doctest.Tester.__doc__ for details.
168-
169-
170- SO WHAT DOES A DOCSTRING EXAMPLE LOOK LIKE ALREADY!?
104+ SO WHAT DOES A DOCTEST EXAMPLE LOOK LIKE ALREADY!?
171105
172106Oh ya. It's easy! In most cases a copy-and-paste of an interactive
173107console session works fine -- just make sure the leading whitespace is
@@ -197,9 +131,6 @@ def _test():
197131
198132Bummers:
199133
200- + Expected output cannot contain an all-whitespace line, since such a line
201- is taken to signal the end of expected output.
202-
203134+ Output to stdout is captured, but not output to stderr (exception
204135 tracebacks are captured via a different means).
205136
@@ -234,57 +165,7 @@ def _test():
234165output as appeared in the initial ">>>" line that triggered it.
235166
236167If you execute this very file, the examples above will be found and
237- executed, leading to this output in verbose mode:
238-
239- Running doctest.__doc__
240- Trying: [1, 2, 3].remove(42)
241- Expecting:
242- Traceback (most recent call last):
243- File "<stdin>", line 1, in ?
244- ValueError: list.remove(x): x not in list
245- ok
246- Trying: x = 12
247- Expecting: nothing
248- ok
249- Trying: x
250- Expecting: 12
251- ok
252- Trying:
253- if x == 13:
254- print "yes"
255- else:
256- print "no"
257- print "NO"
258- print "NO!!!"
259- Expecting:
260- no
261- NO
262- NO!!!
263- ok
264- ... and a bunch more like that, with this summary at the end:
265-
266- 5 items had no tests:
267- doctest.Tester.__init__
268- doctest.Tester.run__test__
269- doctest.Tester.summarize
270- doctest.run_docstring_examples
271- doctest.testmod
272- 12 items passed all tests:
273- 8 tests in doctest
274- 6 tests in doctest.Tester
275- 10 tests in doctest.Tester.merge
276- 14 tests in doctest.Tester.rundict
277- 3 tests in doctest.Tester.rundoc
278- 3 tests in doctest.Tester.runstring
279- 2 tests in doctest.__test__._TestClass
280- 2 tests in doctest.__test__._TestClass.__init__
281- 2 tests in doctest.__test__._TestClass.get
282- 1 tests in doctest.__test__._TestClass.square
283- 2 tests in doctest.__test__.string
284- 7 tests in doctest.is_private
285- 60 tests in 17 items.
286- 60 passed and 0 failed.
287- Test passed.
168+ executed.
288169"""
289170
290171__all__ = [
0 commit comments