Skip to content

Commit 29829b5

Browse files
authored
gh-117294: Report DocTestCase as skipped if all examples in the doctest are skipped (GH-117297)
1 parent efcc968 commit 29829b5

File tree

7 files changed

+87
-7
lines changed

7 files changed

+87
-7
lines changed

Doc/library/doctest.rst

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,7 +1021,8 @@ from text files and modules with doctests:
10211021
and runs the interactive examples in each file. If an example in any file
10221022
fails, then the synthesized unit test fails, and a :exc:`failureException`
10231023
exception is raised showing the name of the file containing the test and a
1024-
(sometimes approximate) line number.
1024+
(sometimes approximate) line number. If all the examples in a file are
1025+
skipped, then the synthesized unit test is also marked as skipped.
10251026

10261027
Pass one or more paths (as strings) to text files to be examined.
10271028

@@ -1087,7 +1088,8 @@ from text files and modules with doctests:
10871088
and runs each doctest in the module. If any of the doctests fail, then the
10881089
synthesized unit test fails, and a :exc:`failureException` exception is raised
10891090
showing the name of the file containing the test and a (sometimes approximate)
1090-
line number.
1091+
line number. If all the examples in a docstring are skipped, then the
1092+
synthesized unit test is also marked as skipped.
10911093

10921094
Optional argument *module* provides the module to be tested. It can be a module
10931095
object or a (possibly dotted) module name. If not specified, the module calling

Lib/doctest.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2281,12 +2281,13 @@ def runTest(self):
22812281

22822282
try:
22832283
runner.DIVIDER = "-"*70
2284-
failures, tries = runner.run(
2285-
test, out=new.write, clear_globs=False)
2284+
results = runner.run(test, out=new.write, clear_globs=False)
2285+
if results.skipped == results.attempted:
2286+
raise unittest.SkipTest("all examples were skipped")
22862287
finally:
22872288
sys.stdout = old
22882289

2289-
if failures:
2290+
if results.failed:
22902291
raise self.failureException(self.format_failure(new.getvalue()))
22912292

22922293
def format_failure(self, err):
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
"""This is a sample module used for testing doctest.
2+
3+
This module includes various scenarios involving skips.
4+
"""
5+
6+
def no_skip_pass():
7+
"""
8+
>>> 2 + 2
9+
4
10+
"""
11+
12+
def no_skip_fail():
13+
"""
14+
>>> 2 + 2
15+
5
16+
"""
17+
18+
def single_skip():
19+
"""
20+
>>> 2 + 2 # doctest: +SKIP
21+
4
22+
"""
23+
24+
def double_skip():
25+
"""
26+
>>> 2 + 2 # doctest: +SKIP
27+
4
28+
>>> 3 + 3 # doctest: +SKIP
29+
6
30+
"""
31+
32+
def partial_skip_pass():
33+
"""
34+
>>> 2 + 2 # doctest: +SKIP
35+
4
36+
>>> 3 + 3
37+
6
38+
"""
39+
40+
def partial_skip_fail():
41+
"""
42+
>>> 2 + 2 # doctest: +SKIP
43+
4
44+
>>> 2 + 2
45+
5
46+
"""
47+
48+
def no_examples():
49+
"""A docstring with no examples should not be counted as run or skipped."""

Lib/test/test_doctest/test_doctest.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2247,6 +2247,16 @@ def test_DocTestSuite():
22472247
>>> suite.run(unittest.TestResult())
22482248
<unittest.result.TestResult run=0 errors=0 failures=0>
22492249
2250+
If all examples in a docstring are skipped, unittest will report it as a
2251+
skipped test:
2252+
2253+
>>> suite = doctest.DocTestSuite('test.test_doctest.sample_doctest_skip')
2254+
>>> result = suite.run(unittest.TestResult())
2255+
>>> result
2256+
<unittest.result.TestResult run=6 errors=0 failures=2>
2257+
>>> len(result.skipped)
2258+
2
2259+
22502260
We can use the current module:
22512261
22522262
>>> suite = test.test_doctest.sample_doctest.test_suite()
@@ -2418,6 +2428,18 @@ def test_DocFileSuite():
24182428
Traceback (most recent call last):
24192429
ValueError: Package may only be specified for module-relative paths.
24202430
2431+
If all examples in a file are skipped, unittest will report it as a
2432+
skipped test:
2433+
2434+
>>> suite = doctest.DocFileSuite('test_doctest.txt',
2435+
... 'test_doctest4.txt',
2436+
... 'test_doctest_skip.txt')
2437+
>>> result = suite.run(unittest.TestResult())
2438+
>>> result
2439+
<unittest.result.TestResult run=3 errors=0 failures=1>
2440+
>>> len(result.skipped)
2441+
1
2442+
24212443
You can specify initial global variables:
24222444
24232445
>>> suite = doctest.DocFileSuite('test_doctest.txt',
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
This is a sample doctest in a text file, in which all examples are skipped.
2+
3+
>>> 2 + 2 # doctest: +SKIP
4+
5

Lib/test/test_zipimport_support.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
# Retrieve some helpers from other test cases
3232
from test.test_doctest import (test_doctest,
3333
sample_doctest, sample_doctest_no_doctests,
34-
sample_doctest_no_docstrings)
34+
sample_doctest_no_docstrings, sample_doctest_skip)
3535

3636

3737
def _run_object_doctest(obj, module):
@@ -110,7 +110,7 @@ def test_doctest_issue4197(self):
110110
# The sample doctest files rewritten to include in the zipped version.
111111
sample_sources = {}
112112
for mod in [sample_doctest, sample_doctest_no_doctests,
113-
sample_doctest_no_docstrings]:
113+
sample_doctest_no_docstrings, sample_doctest_skip]:
114114
src = inspect.getsource(mod)
115115
src = src.replace("test.test_doctest.test_doctest", "test_zipped_doctest")
116116
# Rewrite the module name so that, for example,
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
A ``DocTestCase`` now reports as skipped if all examples in the doctest are
2+
skipped.

0 commit comments

Comments
 (0)