Skip to content

Conditional skip and xfail in doctests #310

@pytestbot

Description

@pytestbot

Originally reported by: Danilo Bellini (BitBucket: danilobellini, GitHub: danilobellini)


I'd like to skip or xfail some doctests, since they depend on something that doesn't exist in some possible system configuration. However, as I tried something like:

:::python
    def anything():
        """
        >>> import pytest
        >>> pytest.xfail()
        >>> raise Exception("Just anything...")
        """

It happens to fail.

============================= test session starts ==============================
platform linux2 -- Python 2.7.3 -- pytest-2.3.5
plugins: timeout, xdist, cov
collected 1 items

pytest_skip_xfail_docstring.py F

=================================== FAILURES ===================================
__________________________________ [doctest] ___________________________________
009     """
010     >>> import pytest
011     >>> pytest.xfail()
UNEXPECTED EXCEPTION: <XFailed instance>
Traceback (most recent call last):

  File "/usr/lib/python2.7/doctest.py", line 1289, in __run
    compileflags, 1) in test.globs

  File "<doctest pytest_skip_xfail_docstring.anything[1]>", line 1, in <module>

  File "/usr/local/lib/python2.7/dist-packages/_pytest/skipping.py", line 37, in xfail
    raise XFailed(reason)

XFailed: <XFailed instance>

/media/danilo/CODE/Spyder Workspace/pytest_skip_xfail_docstring.py:11: UnexpectedException
=========================== 1 failed in 0.33 seconds ===========================

I can use # doctest: +SKIP in doctests, and make a manually-written change into each __doc__ (besides class docstrings) to at least skip problematic tests, but that's far from a nice solution (and there couldn't be any xfail at all).

I've tried to change DoctestModule in _pytest/doctest.py by decorating it (monkeypatching) to call pytest.xfail() when exc_info[0] is a XFailed instance, however:

=================================== FAILURES ====================================
___________________________________ [doctest] ___________________________________
test/__init__.py:119: in new_meth
>         pytest.xfail(e.exc_info[1])
E         XFailed: This reason is just a string

It seems doctests can't xfail nor skip (besides doctest directive). This proposal is to allow [conditional] skips and xfails in doctests.


Metadata

Metadata

Assignees

No one assigned

    Labels

    plugin: doctestsrelated to the doctests builtin plugintype: proposalproposal for a new feature, often to gather opinions or design the API around the new feature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions