Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2a5f9da

Browse files
committed
Issue24756: clarify usage of run_docstring_examples()
1 parent 0b0c3b6 commit 2a5f9da

1 file changed

Lines changed: 23 additions & 7 deletions

File tree

Doc/library/doctest.rst

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -914,15 +914,10 @@ and :ref:`doctest-simple-testfile`.
914914
above, except that *globs* defaults to ``m.__dict__``.
915915

916916

917-
There's also a function to run the doctests associated with a single object.
918-
This function is provided for backward compatibility. There are no plans to
919-
deprecate it, but it's rarely useful:
920-
921-
922917
.. function:: run_docstring_examples(f, globs, verbose=False, name="NoName", compileflags=None, optionflags=0)
923918

924-
Test examples associated with object *f*; for example, *f* may be a module,
925-
function, or class object.
919+
Test examples associated with object *f*; for example, *f* may be a string,
920+
a module, a function, or a class object.
926921

927922
A shallow copy of dictionary argument *globs* is used for the execution context.
928923

@@ -1821,6 +1816,27 @@ several options for organizing tests:
18211816
* Define a ``__test__`` dictionary mapping from regression test topics to
18221817
docstrings containing test cases.
18231818

1819+
When you have placed your tests in a module, the module can itself be the test
1820+
runner. When a test fails, you can arrange for your test runner to re-run only
1821+
the failing doctest while you debug the problem. Here is a minimal example of
1822+
such a test runner::
1823+
1824+
if __name__ == '__main__':
1825+
import doctest
1826+
flags = doctest.REPORT_NDIFF|doctest.FAIL_FAST
1827+
if len(sys.argv) > 1:
1828+
name = sys.argv[1]
1829+
if name in globals():
1830+
obj = globals()[name]
1831+
else:
1832+
obj = __test__[name]
1833+
doctest.run_docstring_examples(obj, globals(), name=name,
1834+
optionflags=flags)
1835+
else:
1836+
fail, total = doctest.testmod(optionflags=flags)
1837+
print("{} failures out of {} tests".format(fail, total))
1838+
1839+
18241840
.. rubric:: Footnotes
18251841

18261842
.. [#] Examples containing both expected output and an exception are not supported.

0 commit comments

Comments
 (0)