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

Skip to content

Commit 1976d9b

Browse files
committed
#15916: if there are no docstrings, make empty suite, not an error.
This makes doctest work like unittest: if the test case is empty, that just means there are zero tests run, it's not an error. The existing behavior was broken, since it only gave an error if there were *no* docstrings, and zero tests run if there were docstrings but none of them contained tests. So this makes it self-consistent as well. Patch by Glenn Jones.
1 parent 865d23d commit 1976d9b

6 files changed

Lines changed: 29 additions & 33 deletions

File tree

Doc/library/doctest.rst

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,15 +1058,9 @@ from text files and modules with doctests:
10581058

10591059
This function uses the same search technique as :func:`testmod`.
10601060

1061-
.. note::
1062-
Unlike :func:`testmod` and :class:`DocTestFinder`, this function raises
1063-
a :exc:`ValueError` if *module* contains no docstrings. You can prevent
1064-
this error by passing a :class:`DocTestFinder` instance as the
1065-
*test_finder* argument with its *exclude_empty* keyword argument set
1066-
to ``False``::
1067-
1068-
>>> finder = doctest.DocTestFinder(exclude_empty=False)
1069-
>>> suite = doctest.DocTestSuite(test_finder=finder)
1061+
.. versionchanged:: 3.5
1062+
:func:`DocTestSuite` returns an empty :class:`unittest.TestSuite` if *module*
1063+
contains no docstrings instead of raising :exc:`ValueError`.
10701064

10711065

10721066
Under the covers, :func:`DocTestSuite` creates a :class:`unittest.TestSuite` out

Doc/whatsnew/3.5.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,10 @@ Improved Modules
154154
applications where startup time is paramount (contributed by Brett Cannon in
155155
:issue:`17621`).
156156

157+
* :func:`doctest.DocTestSuite` returns an empty :class:`unittest.TestSuite` if
158+
*module* contains no docstrings instead of raising :exc:`ValueError`
159+
(contributed by Glenn Jones in :issue:`15916`).
160+
157161

158162
Optimizations
159163
=============

Lib/doctest.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2376,15 +2376,6 @@ def DocTestSuite(module=None, globs=None, extraglobs=None, test_finder=None,
23762376
suite = _DocTestSuite()
23772377
suite.addTest(SkipDocTestCase(module))
23782378
return suite
2379-
elif not tests:
2380-
# Why do we want to do this? Because it reveals a bug that might
2381-
# otherwise be hidden.
2382-
# It is probably a bug that this exception is not also raised if the
2383-
# number of doctest examples in tests is zero (i.e. if no doctest
2384-
# examples were found). However, we should probably not be raising
2385-
# an exception at all here, though it is too late to make this change
2386-
# for a maintenance release. See also issue #14649.
2387-
raise ValueError(module, "has no docstrings")
23882379

23892380
tests.sort()
23902381
suite = _DocTestSuite()

Lib/test/test_doctest.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2096,22 +2096,9 @@ def test_DocTestSuite():
20962096
>>> suite.run(unittest.TestResult())
20972097
<unittest.result.TestResult run=0 errors=0 failures=0>
20982098
2099-
However, if DocTestSuite finds no docstrings, it raises an error:
2099+
The module need not contain any docstrings either:
21002100
2101-
>>> try:
2102-
... doctest.DocTestSuite('test.sample_doctest_no_docstrings')
2103-
... except ValueError as e:
2104-
... error = e
2105-
2106-
>>> print(error.args[1])
2107-
has no docstrings
2108-
2109-
You can prevent this error by passing a DocTestFinder instance with
2110-
the `exclude_empty` keyword argument set to False:
2111-
2112-
>>> finder = doctest.DocTestFinder(exclude_empty=False)
2113-
>>> suite = doctest.DocTestSuite('test.sample_doctest_no_docstrings',
2114-
... test_finder=finder)
2101+
>>> suite = doctest.DocTestSuite('test.sample_doctest_no_docstrings')
21152102
>>> suite.run(unittest.TestResult())
21162103
<unittest.result.TestResult run=0 errors=0 failures=0>
21172104
@@ -2121,6 +2108,22 @@ def test_DocTestSuite():
21212108
>>> suite.run(unittest.TestResult())
21222109
<unittest.result.TestResult run=9 errors=0 failures=4>
21232110
2111+
We can also provide a DocTestFinder:
2112+
2113+
>>> finder = doctest.DocTestFinder()
2114+
>>> suite = doctest.DocTestSuite('test.sample_doctest',
2115+
... test_finder=finder)
2116+
>>> suite.run(unittest.TestResult())
2117+
<unittest.result.TestResult run=9 errors=0 failures=4>
2118+
2119+
The DocTestFinder need not return any tests:
2120+
2121+
>>> finder = doctest.DocTestFinder()
2122+
>>> suite = doctest.DocTestSuite('test.sample_doctest_no_docstrings',
2123+
... test_finder=finder)
2124+
>>> suite.run(unittest.TestResult())
2125+
<unittest.result.TestResult run=0 errors=0 failures=0>
2126+
21242127
We can supply global variables. If we pass globs, they will be
21252128
used instead of the module globals. Here we'll pass an empty
21262129
globals, triggering an extra error:

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,7 @@ Thomas Jollans
630630
Nicolas Joly
631631
Brian K. Jones
632632
Evan Jones
633+
Glenn Jones
633634
Jeremy Jones
634635
Richard Jones
635636
Irmen de Jong

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ Library
203203

204204
- Issue #20334: inspect.Signature and inspect.Parameter are now hashable.
205205

206+
- Issue #15916: doctest.DocTestSuite returns an empty unittest.TestSuite instead
207+
of raising ValueError if it finds no tests
208+
206209
IDLE
207210
----
208211

0 commit comments

Comments
 (0)