Skip to content

Commit 5f52d5e

Browse files
authored
Merge pull request #4927 from tkf/skip-doctest
Make pytest.skip work in doctest
2 parents 5c57d92 + 9570156 commit 5f52d5e

File tree

5 files changed

+22
-1
lines changed

5 files changed

+22
-1
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ Steffen Allner
222222
Stephan Obermann
223223
Sven-Hendrik Haase
224224
Tadek Teleżyński
225+
Takafumi Arakaki
225226
Tarcisio Fischer
226227
Tareq Alayan
227228
Ted Xiao

changelog/4911.feature.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Doctests can be skipped now dynamically using ``pytest.skip()``.

src/_pytest/doctest.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from _pytest._code.code import TerminalRepr
1616
from _pytest.compat import safe_getattr
1717
from _pytest.fixtures import FixtureRequest
18+
from _pytest.outcomes import Skipped
1819

1920
DOCTEST_REPORT_CHOICE_NONE = "none"
2021
DOCTEST_REPORT_CHOICE_CDIFF = "cdiff"
@@ -153,6 +154,8 @@ def report_failure(self, out, test, example, got):
153154
raise failure
154155

155156
def report_unexpected_exception(self, out, test, example, exc_info):
157+
if isinstance(exc_info[1], Skipped):
158+
raise exc_info[1]
156159
failure = doctest.UnexpectedException(test, example, exc_info)
157160
if self.continue_on_failure:
158161
out.append(failure)

src/_pytest/outcomes.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ def skip(msg="", **kwargs):
8080
Skip an executing test with the given message.
8181
8282
This function should be called only during testing (setup, call or teardown) or
83-
during collection by using the ``allow_module_level`` flag.
83+
during collection by using the ``allow_module_level`` flag. This function can
84+
be called in doctests as well.
8485
8586
:kwarg bool allow_module_level: allows this function to be called at
8687
module level, skipping the rest of the module. Default to False.
@@ -89,6 +90,9 @@ def skip(msg="", **kwargs):
8990
It is better to use the :ref:`pytest.mark.skipif ref` marker when possible to declare a test to be
9091
skipped under certain conditions like mismatching platforms or
9192
dependencies.
93+
Similarly, use the ``# doctest: +SKIP`` directive (see `doctest.SKIP
94+
<https://docs.python.org/3/library/doctest.html#doctest.SKIP>`_)
95+
to skip a doctest statically.
9296
"""
9397
__tracebackhide__ = True
9498
allow_module_level = kwargs.pop("allow_module_level", False)

testing/test_doctest.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,18 @@ def test_doctest_unexpected_exception(self, testdir):
188188
]
189189
)
190190

191+
def test_doctest_skip(self, testdir):
192+
testdir.maketxtfile(
193+
"""
194+
>>> 1
195+
1
196+
>>> import pytest
197+
>>> pytest.skip("")
198+
"""
199+
)
200+
result = testdir.runpytest("--doctest-modules")
201+
result.stdout.fnmatch_lines(["*1 skipped*"])
202+
191203
def test_docstring_partial_context_around_error(self, testdir):
192204
"""Test that we show some context before the actual line of a failing
193205
doctest.

0 commit comments

Comments
 (0)