Skip to content

Commit

Permalink
doctest: handle any OutcomeException (#6669)
Browse files Browse the repository at this point in the history
Fixes using `pytest.xfail()` and `pytest.importorskip()` in doctests.

Ref: #310
  • Loading branch information
blueyed committed Feb 19, 2020
1 parent 442f7a7 commit f95c7f5
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
1 change: 1 addition & 0 deletions changelog/310.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add support for calling `pytest.xfail()` and `pytest.importorskip()` with doctests.
4 changes: 2 additions & 2 deletions src/_pytest/doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
from _pytest.compat import safe_getattr
from _pytest.compat import TYPE_CHECKING
from _pytest.fixtures import FixtureRequest
from _pytest.outcomes import Skipped
from _pytest.outcomes import OutcomeException
from _pytest.python_api import approx
from _pytest.warning_types import PytestWarning

Expand Down Expand Up @@ -178,7 +178,7 @@ def report_failure(self, out, test, example, got):
raise failure

def report_unexpected_exception(self, out, test, example, exc_info):
if isinstance(exc_info[1], Skipped):
if isinstance(exc_info[1], OutcomeException):
raise exc_info[1]
if isinstance(exc_info[1], bdb.BdbQuit):
outcomes.exit("Quitting debugger")
Expand Down
32 changes: 28 additions & 4 deletions testing/test_doctest.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,17 +186,41 @@ def test_doctest_unexpected_exception(self, testdir):
]
)

def test_doctest_skip(self, testdir):
def test_doctest_outcomes(self, testdir):
testdir.maketxtfile(
"""
test_skip="""
>>> 1
1
>>> import pytest
>>> pytest.skip("")
"""
>>> 2
3
""",
test_xfail="""
>>> import pytest
>>> pytest.xfail("xfail_reason")
>>> foo
bar
""",
test_importorskip="""
>>> import pytest
>>> pytest.importorskip("doesnotexist")
>>> foo
bar
""",
)
result = testdir.runpytest("--doctest-modules")
result.stdout.fnmatch_lines(["*1 skipped*"])
result.stdout.fnmatch_lines(
[
"collected 3 items",
"",
"test_importorskip.txt s *",
"test_skip.txt s *",
"test_xfail.txt x *",
"",
"*= 2 skipped, 1 xfailed in *",
]
)

def test_docstring_partial_context_around_error(self, testdir):
"""Test that we show some context before the actual line of a failing
Expand Down

0 comments on commit f95c7f5

Please sign in to comment.