mirror of
https://github.com/python/cpython.git
synced 2025-12-31 04:23:37 +00:00
gh-117294: Report DocTestCase as skipped if all examples in the doctest are skipped (GH-117297)
This commit is contained in:
parent
efcc96844e
commit
29829b58a8
7 changed files with 87 additions and 7 deletions
|
|
@ -2281,12 +2281,13 @@ def runTest(self):
|
|||
|
||||
try:
|
||||
runner.DIVIDER = "-"*70
|
||||
failures, tries = runner.run(
|
||||
test, out=new.write, clear_globs=False)
|
||||
results = runner.run(test, out=new.write, clear_globs=False)
|
||||
if results.skipped == results.attempted:
|
||||
raise unittest.SkipTest("all examples were skipped")
|
||||
finally:
|
||||
sys.stdout = old
|
||||
|
||||
if failures:
|
||||
if results.failed:
|
||||
raise self.failureException(self.format_failure(new.getvalue()))
|
||||
|
||||
def format_failure(self, err):
|
||||
|
|
|
|||
49
Lib/test/test_doctest/sample_doctest_skip.py
Normal file
49
Lib/test/test_doctest/sample_doctest_skip.py
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
"""This is a sample module used for testing doctest.
|
||||
|
||||
This module includes various scenarios involving skips.
|
||||
"""
|
||||
|
||||
def no_skip_pass():
|
||||
"""
|
||||
>>> 2 + 2
|
||||
4
|
||||
"""
|
||||
|
||||
def no_skip_fail():
|
||||
"""
|
||||
>>> 2 + 2
|
||||
5
|
||||
"""
|
||||
|
||||
def single_skip():
|
||||
"""
|
||||
>>> 2 + 2 # doctest: +SKIP
|
||||
4
|
||||
"""
|
||||
|
||||
def double_skip():
|
||||
"""
|
||||
>>> 2 + 2 # doctest: +SKIP
|
||||
4
|
||||
>>> 3 + 3 # doctest: +SKIP
|
||||
6
|
||||
"""
|
||||
|
||||
def partial_skip_pass():
|
||||
"""
|
||||
>>> 2 + 2 # doctest: +SKIP
|
||||
4
|
||||
>>> 3 + 3
|
||||
6
|
||||
"""
|
||||
|
||||
def partial_skip_fail():
|
||||
"""
|
||||
>>> 2 + 2 # doctest: +SKIP
|
||||
4
|
||||
>>> 2 + 2
|
||||
5
|
||||
"""
|
||||
|
||||
def no_examples():
|
||||
"""A docstring with no examples should not be counted as run or skipped."""
|
||||
|
|
@ -2247,6 +2247,16 @@ def test_DocTestSuite():
|
|||
>>> suite.run(unittest.TestResult())
|
||||
<unittest.result.TestResult run=0 errors=0 failures=0>
|
||||
|
||||
If all examples in a docstring are skipped, unittest will report it as a
|
||||
skipped test:
|
||||
|
||||
>>> suite = doctest.DocTestSuite('test.test_doctest.sample_doctest_skip')
|
||||
>>> result = suite.run(unittest.TestResult())
|
||||
>>> result
|
||||
<unittest.result.TestResult run=6 errors=0 failures=2>
|
||||
>>> len(result.skipped)
|
||||
2
|
||||
|
||||
We can use the current module:
|
||||
|
||||
>>> suite = test.test_doctest.sample_doctest.test_suite()
|
||||
|
|
@ -2418,6 +2428,18 @@ def test_DocFileSuite():
|
|||
Traceback (most recent call last):
|
||||
ValueError: Package may only be specified for module-relative paths.
|
||||
|
||||
If all examples in a file are skipped, unittest will report it as a
|
||||
skipped test:
|
||||
|
||||
>>> suite = doctest.DocFileSuite('test_doctest.txt',
|
||||
... 'test_doctest4.txt',
|
||||
... 'test_doctest_skip.txt')
|
||||
>>> result = suite.run(unittest.TestResult())
|
||||
>>> result
|
||||
<unittest.result.TestResult run=3 errors=0 failures=1>
|
||||
>>> len(result.skipped)
|
||||
1
|
||||
|
||||
You can specify initial global variables:
|
||||
|
||||
>>> suite = doctest.DocFileSuite('test_doctest.txt',
|
||||
|
|
|
|||
4
Lib/test/test_doctest/test_doctest_skip.txt
Normal file
4
Lib/test/test_doctest/test_doctest_skip.txt
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
This is a sample doctest in a text file, in which all examples are skipped.
|
||||
|
||||
>>> 2 + 2 # doctest: +SKIP
|
||||
5
|
||||
|
|
@ -31,7 +31,7 @@
|
|||
# Retrieve some helpers from other test cases
|
||||
from test.test_doctest import (test_doctest,
|
||||
sample_doctest, sample_doctest_no_doctests,
|
||||
sample_doctest_no_docstrings)
|
||||
sample_doctest_no_docstrings, sample_doctest_skip)
|
||||
|
||||
|
||||
def _run_object_doctest(obj, module):
|
||||
|
|
@ -110,7 +110,7 @@ def test_doctest_issue4197(self):
|
|||
# The sample doctest files rewritten to include in the zipped version.
|
||||
sample_sources = {}
|
||||
for mod in [sample_doctest, sample_doctest_no_doctests,
|
||||
sample_doctest_no_docstrings]:
|
||||
sample_doctest_no_docstrings, sample_doctest_skip]:
|
||||
src = inspect.getsource(mod)
|
||||
src = src.replace("test.test_doctest.test_doctest", "test_zipped_doctest")
|
||||
# Rewrite the module name so that, for example,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue