2010-11-25 19:11:10 +08:00
.. _`skip and xfail`:
2011-03-05 20:08:43 +08:00
skip and xfail: dealing with tests that can not succeed
2010-10-13 18:26:14 +08:00
=====================================================================
2010-01-13 23:00:33 +08:00
2011-03-05 20:08:43 +08:00
If you have test functions that cannot be run on certain platforms
or that you expect to fail you can mark them accordingly or you
may call helper functions during execution of setup or test functions.
2011-03-04 06:22:55 +08:00
2011-03-05 20:08:43 +08:00
A *skip* means that you expect your test to pass unless a certain
configuration or condition (e.g. wrong Python interpreter, missing
dependency) prevents it to run. And *xfail* means that your test
can run but you expect it to fail because there is an implementation problem.
2010-11-21 04:35:55 +08:00
2011-03-05 20:08:43 +08:00
py.test counts and lists *skip* and *xfail* tests separately. However,
detailed information about skipped/xfailed tests is not shown by default
to avoid cluttering the output. You can use the ``-r`` option to see
details corresponding to the "short" letters shown in the test
progress::
2010-11-21 04:35:55 +08:00
2011-03-04 06:40:38 +08:00
py.test -rxs # show extra info on skips and xfails
2009-10-15 22:18:57 +08:00
2010-11-25 19:11:10 +08:00
(See :ref:`how to change command line options defaults`)
2009-10-15 22:18:57 +08:00
.. _skipif:
2011-03-05 20:08:43 +08:00
Marking a test function to be skipped
2009-10-15 22:18:57 +08:00
-------------------------------------------
2011-03-04 06:40:38 +08:00
Here is an example of marking a test function to be skipped
2009-11-06 00:46:14 +08:00
when run on a Python3 interpreter::
2009-10-15 22:18:57 +08:00
2011-03-04 06:22:55 +08:00
import sys
2010-11-18 05:12:16 +08:00
@pytest.mark.skipif("sys.version_info >= (3,0)")
2009-10-15 22:18:57 +08:00
def test_function():
...
2010-07-27 03:15:15 +08:00
During test function setup the skipif condition is
2011-03-05 20:08:43 +08:00
evaluated by calling ``eval('sys.version_info >= (3,0)', namespace)``.
(*New in version 2.0.2*) The namespace contains all the module globals of the test function so that
you can for example check for versions of a module you are using::
2011-03-04 06:22:55 +08:00
import mymodule
@pytest.mark.skipif("mymodule.__version__ < '1.2'")
def test_function():
...
2011-03-05 20:08:43 +08:00
The test function will not be run ("skipped") if
``mymodule`` is below the specified version. The reason
2011-03-04 06:22:55 +08:00
for specifying the condition as a string is mainly that
2011-03-05 20:08:43 +08:00
py.test can report a summary of skip conditions.
For information on the construction of the ``namespace``
see `evaluation of skipif/xfail conditions`_.
2009-10-15 22:18:57 +08:00
2011-03-05 20:08:43 +08:00
You can of course create a shortcut for your conditional skip
decorator at module level like this::
2009-11-06 00:46:14 +08:00
2010-11-18 05:12:16 +08:00
win32only = pytest.mark.skipif("sys.platform != 'win32'")
2009-11-06 00:46:14 +08:00
@win32only
def test_function():
...
2011-03-04 06:22:55 +08:00
skip all test functions of a class
2009-10-23 02:57:21 +08:00
--------------------------------------
2011-03-05 20:08:43 +08:00
As with all function :ref:`marking <mark>` you can skip test functions at the
2010-07-27 03:15:15 +08:00
`whole class- or module level`_. Here is an example
2011-03-04 06:40:38 +08:00
for skipping all methods of a test class based on the platform::
2009-10-23 02:57:21 +08:00
class TestPosixCalls:
2010-11-18 05:12:16 +08:00
pytestmark = pytest.mark.skipif("sys.platform == 'win32'")
2010-07-27 03:15:15 +08:00
2009-10-23 02:57:21 +08:00
def test_function(self):
2010-11-21 04:35:55 +08:00
"will not be setup or run under 'win32' platform"
2009-10-23 02:57:21 +08:00
2011-03-05 20:08:43 +08:00
The ``pytestmark`` special name tells py.test to apply it to each test
function in the class. If your code targets python2.6 or above you can
more naturally use the skipif decorator (and any other marker) on
classes::
2010-05-22 00:11:47 +08:00
2010-11-18 05:12:16 +08:00
@pytest.mark.skipif("sys.platform == 'win32'")
2010-05-22 00:11:47 +08:00
class TestPosixCalls:
2010-07-27 03:15:15 +08:00
2010-05-22 00:11:47 +08:00
def test_function(self):
2010-11-21 04:35:55 +08:00
"will not be setup or run under 'win32' platform"
2010-05-22 00:11:47 +08:00
2011-03-04 06:40:38 +08:00
Using multiple "skipif" decorators on a single function is generally fine - it means that if any of the conditions apply the function execution will be skipped.
2009-10-23 02:57:21 +08:00
.. _`whole class- or module level`: mark.html#scoped-marking
2010-07-07 22:37:28 +08:00
.. _xfail:
2009-10-23 02:57:21 +08:00
2010-10-11 06:49:54 +08:00
mark a test function as expected to fail
2009-10-15 22:18:57 +08:00
-------------------------------------------------------
2009-10-23 02:57:21 +08:00
You can use the ``xfail`` marker to indicate that you
2010-07-27 03:15:15 +08:00
expect the test to fail::
2009-10-15 22:18:57 +08:00
2010-11-18 05:12:16 +08:00
@pytest.mark.xfail
2009-10-23 02:57:21 +08:00
def test_function():
2009-10-15 22:18:57 +08:00
...
2009-10-23 02:57:21 +08:00
This test will be run but no traceback will be reported
2009-10-15 22:18:57 +08:00
when it fails. Instead terminal reporting will list it in the
"expected to fail" or "unexpectedly passing" sections.
2009-10-23 02:57:21 +08:00
2010-11-21 04:35:55 +08:00
By specifying on the commandline::
pytest --runxfail
you can force the running and reporting of an ``xfail`` marked test
as if it weren't marked at all.
2011-03-04 06:40:38 +08:00
As with skipif_ you can also mark your expectation of a failure
on a particular platform::
2009-10-15 22:18:57 +08:00
2010-11-18 05:12:16 +08:00
@pytest.mark.xfail("sys.version_info >= (3,0)")
2009-10-15 22:18:57 +08:00
def test_function():
...
2011-03-04 06:40:38 +08:00
You can furthermore prevent the running of an "xfail" test or
2010-11-21 04:35:55 +08:00
specify a reason such as a bug ID or similar. Here is
2011-03-04 06:40:38 +08:00
a simple test file with the several usages:
2010-11-21 04:35:55 +08:00
.. literalinclude:: example/xfail_demo.py
Running it with the report-on-xfail option gives this output::
example $ py.test -rx xfail_demo.py
=========================== test session starts ============================
2011-05-01 18:38:56 +08:00
platform linux2 -- Python 2.6.6 -- pytest-2.0.3
2011-03-04 06:40:38 +08:00
collecting ... collected 6 items
2010-11-21 04:35:55 +08:00
2011-03-04 06:40:38 +08:00
xfail_demo.py xxxxxx
2010-11-21 04:35:55 +08:00
========================= short test summary info ==========================
XFAIL xfail_demo.py::test_hello
XFAIL xfail_demo.py::test_hello2
2011-03-09 17:58:36 +08:00
reason: [NOTRUN]
2010-11-21 04:35:55 +08:00
XFAIL xfail_demo.py::test_hello3
condition: hasattr(os, 'sep')
XFAIL xfail_demo.py::test_hello4
bug 110
XFAIL xfail_demo.py::test_hello5
2011-03-04 06:40:38 +08:00
condition: pytest.__version__[0] != "17"
XFAIL xfail_demo.py::test_hello6
2010-11-21 04:35:55 +08:00
reason: reason
2011-05-01 18:38:56 +08:00
======================== 6 xfailed in 0.05 seconds =========================
2010-10-11 05:45:45 +08:00
2011-03-05 20:08:43 +08:00
.. _`evaluation of skipif/xfail conditions`:
evaluation of skipif/xfail expressions
----------------------------------------------------
.. versionadded:: 2.0.2
The evaluation of a condition string in ``pytest.mark.skipif(conditionstring)``
or ``pytest.mark.xfail(conditionstring)`` takes place in a namespace
dictionary which is constructed as follows:
* the namespace is initialized by putting the ``sys`` and ``os`` modules
and the pytest ``config`` object into it.
* updated with the module globals of the test function for which the
expression is applied.
The pytest ``config`` object allows you to skip based on a test configuration value
which you might have added::
@pytest.mark.skipif("not config.getvalue('db')")
def test_function(...):
...
2010-05-26 03:01:43 +08:00
imperative xfail from within a test or setup function
------------------------------------------------------
If you cannot declare xfail-conditions at import time
2010-07-27 03:15:15 +08:00
you can also imperatively produce an XFail-outcome from
2010-05-26 03:01:43 +08:00
within test or setup code. Example::
def test_function():
if not valid_config():
2011-02-17 21:46:40 +08:00
pytest.xfail("unsupported configuration")
2010-05-26 03:01:43 +08:00
2009-10-15 22:18:57 +08:00
2009-10-23 02:57:21 +08:00
skipping on a missing import dependency
--------------------------------------------------
2009-10-15 22:18:57 +08:00
2010-07-27 03:15:15 +08:00
You can use the following import helper at module level
2009-11-06 00:46:14 +08:00
or within a test or test setup function::
2009-10-15 22:18:57 +08:00
2010-11-18 05:12:16 +08:00
docutils = pytest.importorskip("docutils")
2009-10-15 22:18:57 +08:00
2009-10-23 02:57:21 +08:00
If ``docutils`` cannot be imported here, this will lead to a
2011-03-04 06:40:38 +08:00
skip outcome of the test. You can also skip based on the
version number of a library::
2009-10-15 22:18:57 +08:00
2010-11-18 05:12:16 +08:00
docutils = pytest.importorskip("docutils", minversion="0.3")
2009-10-15 22:18:57 +08:00
The version will be read from the specified module's ``__version__`` attribute.
2009-10-23 02:57:21 +08:00
imperative skip from within a test or setup function
------------------------------------------------------
2009-10-15 22:18:57 +08:00
2009-10-23 02:57:21 +08:00
If for some reason you cannot declare skip-conditions
2011-03-04 06:40:38 +08:00
you can also imperatively produce a skip-outcome from
2009-10-23 02:57:21 +08:00
within test or setup code. Example::
2009-10-15 22:18:57 +08:00
def test_function():
if not valid_config():
2011-02-17 21:46:40 +08:00
pytest.skip("unsupported configuration")
2009-10-15 22:18:57 +08:00