2010-11-25 19:11:10 +08:00
|
|
|
.. _`skip and xfail`:
|
|
|
|
|
2012-10-09 20:35:17 +08:00
|
|
|
.. _skipping:
|
|
|
|
|
2011-09-06 17:43:42 +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
|
|
|
|
2013-05-08 00:40:26 +08:00
|
|
|
A *skip* means that you expect your test to pass unless the environment
|
2013-07-07 00:54:24 +08:00
|
|
|
(e.g. wrong Python interpreter, missing dependency) prevents it to run.
|
2013-05-08 00:40:26 +08:00
|
|
|
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
|
|
|
|
2014-01-18 19:31:33 +08:00
|
|
|
``pytest`` counts and lists *skip* and *xfail* tests separately. Detailed
|
2013-05-08 00:40:26 +08:00
|
|
|
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:
|
2013-05-08 00:40:26 +08:00
|
|
|
.. _`condition booleans`:
|
2009-10-15 22:18:57 +08:00
|
|
|
|
2011-03-05 20:08:43 +08:00
|
|
|
Marking a test function to be skipped
|
2009-10-15 22:18:57 +08:00
|
|
|
-------------------------------------------
|
|
|
|
|
2015-09-23 22:04:04 +08:00
|
|
|
.. versionadded:: 2.9
|
|
|
|
|
2015-09-21 22:33:48 +08:00
|
|
|
The simplest way to skip a test function is to mark it with the `skip` decorator
|
2015-09-23 22:04:04 +08:00
|
|
|
which may be passed an optional `reason`:
|
2015-09-21 22:29:07 +08:00
|
|
|
|
|
|
|
@pytest.mark.skip(reason="no way of currently testing this")
|
|
|
|
def test_the_unknown():
|
|
|
|
...
|
|
|
|
|
2015-09-21 22:29:50 +08:00
|
|
|
.. versionadded:: 2.0, 2.4
|
|
|
|
|
2015-09-21 22:29:07 +08:00
|
|
|
If you wish to skip something conditionally then you can use `skipif` instead.
|
2015-09-21 22:33:48 +08:00
|
|
|
Here is an example of marking a test function to be skipped
|
2013-05-08 00:40:26 +08:00
|
|
|
when run on a Python3.3 interpreter::
|
2009-10-15 22:18:57 +08:00
|
|
|
|
2011-03-04 06:22:55 +08:00
|
|
|
import sys
|
2014-04-04 04:26:10 +08:00
|
|
|
@pytest.mark.skipif(sys.version_info < (3,3),
|
2013-05-08 00:40:26 +08:00
|
|
|
reason="requires python3.3")
|
2009-10-15 22:18:57 +08:00
|
|
|
def test_function():
|
|
|
|
...
|
|
|
|
|
2013-05-08 00:40:26 +08:00
|
|
|
During test function setup the condition ("sys.version_info >= (3,3)") is
|
|
|
|
checked. If it evaluates to True, the test function will be skipped
|
2013-07-07 00:54:24 +08:00
|
|
|
with the specified reason. Note that pytest enforces specifying a reason
|
2013-05-08 00:40:26 +08:00
|
|
|
in order to report meaningful "skip reasons" (e.g. when using ``-rs``).
|
2013-07-07 00:54:24 +08:00
|
|
|
If the condition is a string, it will be evaluated as python expression.
|
2011-03-04 06:22:55 +08:00
|
|
|
|
2013-05-08 00:40:26 +08:00
|
|
|
You can share skipif markers between modules. Consider this test module::
|
2011-03-04 06:22:55 +08:00
|
|
|
|
2013-05-08 00:40:26 +08:00
|
|
|
# content of test_mymodule.py
|
|
|
|
|
|
|
|
import mymodule
|
2014-04-04 04:26:10 +08:00
|
|
|
minversion = pytest.mark.skipif(mymodule.__versioninfo__ < (1,1),
|
2013-05-08 00:40:26 +08:00
|
|
|
reason="at least mymodule-1.1 required")
|
|
|
|
@minversion
|
2011-03-04 06:22:55 +08:00
|
|
|
def test_function():
|
|
|
|
...
|
2009-10-15 22:18:57 +08:00
|
|
|
|
2013-05-08 00:40:26 +08:00
|
|
|
You can import it from another test module::
|
2009-11-06 00:46:14 +08:00
|
|
|
|
2013-05-08 00:40:26 +08:00
|
|
|
# test_myothermodule.py
|
|
|
|
from test_mymodule import minversion
|
2009-11-06 00:46:14 +08:00
|
|
|
|
2013-05-08 00:40:26 +08:00
|
|
|
@minversion
|
|
|
|
def test_anotherfunction():
|
2009-11-06 00:46:14 +08:00
|
|
|
...
|
|
|
|
|
2013-05-08 00:40:26 +08:00
|
|
|
For larger test suites it's usually a good idea to have one file
|
|
|
|
where you define the markers which you then consistently apply
|
|
|
|
throughout your test suite.
|
|
|
|
|
2013-10-15 19:45:55 +08:00
|
|
|
Alternatively, the pre pytest-2.4 way to specify :ref:`condition strings
|
|
|
|
<string conditions>` instead of booleans will remain fully supported in future
|
2013-05-08 00:40:26 +08:00
|
|
|
versions of pytest. It couldn't be easily used for importing markers
|
|
|
|
between test modules so it's no longer advertised as the primary method.
|
|
|
|
|
|
|
|
|
|
|
|
Skip all test functions of a class or module
|
|
|
|
---------------------------------------------
|
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
|
2013-07-07 00:54:24 +08:00
|
|
|
`whole class- or module level`_. If your code targets python2.6 or above you
|
2013-05-08 00:40:26 +08:00
|
|
|
use the skipif decorator (and any other marker) on classes::
|
2009-10-23 02:57:21 +08:00
|
|
|
|
2015-12-05 10:35:31 +08:00
|
|
|
@pytest.mark.skipif(sys.platform == 'win32',
|
|
|
|
reason="does not run on windows")
|
2009-10-23 02:57:21 +08:00
|
|
|
class TestPosixCalls:
|
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
|
|
|
|
2013-05-08 00:40:26 +08:00
|
|
|
If the condition is true, this marker will produce a skip result for
|
|
|
|
each of the test methods.
|
|
|
|
|
|
|
|
If your code targets python2.5 where class-decorators are not available,
|
|
|
|
you can set the ``pytestmark`` attribute of a class::
|
2010-05-22 00:11:47 +08:00
|
|
|
|
|
|
|
class TestPosixCalls:
|
2015-12-05 10:43:06 +08:00
|
|
|
pytestmark = pytest.mark.skipif(sys.platform == 'win32',
|
|
|
|
reason="does not run on windows")
|
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
|
|
|
|
2013-05-08 00:40:26 +08:00
|
|
|
As with the class-decorator, the ``pytestmark`` special name tells
|
2014-01-18 19:31:33 +08:00
|
|
|
``pytest`` to apply it to each test function in the class.
|
2013-05-08 00:40:26 +08:00
|
|
|
|
|
|
|
If you want to skip all test functions of a module, you must use
|
2015-07-10 08:50:38 +08:00
|
|
|
the ``pytestmark`` name on the global level:
|
2013-05-08 00:40:26 +08:00
|
|
|
|
2015-07-10 08:50:38 +08:00
|
|
|
.. code-block:: python
|
2013-05-08 00:40:26 +08:00
|
|
|
|
2015-07-10 08:50:38 +08:00
|
|
|
# test_module.py
|
2013-05-08 00:40:26 +08:00
|
|
|
pytestmark = pytest.mark.skipif(...)
|
|
|
|
|
|
|
|
If multiple "skipif" decorators are applied to a test function, it
|
|
|
|
will be skipped if any of the skip conditions is true.
|
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
|
|
|
|
2011-09-06 17:43:42 +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
|
|
|
|
2013-07-07 00:54:24 +08:00
|
|
|
@pytest.mark.xfail(sys.version_info >= (3,3),
|
2013-05-08 00:40:26 +08:00
|
|
|
reason="python3.3 api changes")
|
2009-10-15 22:18:57 +08:00
|
|
|
def test_function():
|
|
|
|
...
|
|
|
|
|
2014-07-27 00:10:32 +08:00
|
|
|
If you want to be more specific as to why the test is failing, you can specify
|
|
|
|
a single exception, or a list of exceptions, in the ``raises`` argument. Then
|
|
|
|
the test will be reported as a regular failure if it fails with an
|
|
|
|
exception not mentioned in ``raises``.
|
|
|
|
|
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
|
2015-06-07 05:30:49 +08:00
|
|
|
======= test session starts ========
|
2015-12-12 08:00:31 +08:00
|
|
|
platform linux -- Python 3.4.3, pytest-2.8.5, py-1.4.31, pluggy-0.3.1
|
2015-09-22 20:02:11 +08:00
|
|
|
rootdir: $REGENDOC_TMPDIR/example, inifile:
|
2014-08-08 03:41:51 +08:00
|
|
|
collected 7 items
|
2015-09-21 22:29:50 +08:00
|
|
|
|
2014-08-08 03:41:51 +08:00
|
|
|
xfail_demo.py xxxxxxx
|
2015-06-07 05:30:49 +08:00
|
|
|
======= short test summary info ========
|
2010-11-21 04:35:55 +08:00
|
|
|
XFAIL xfail_demo.py::test_hello
|
|
|
|
XFAIL xfail_demo.py::test_hello2
|
2015-09-21 22:29:50 +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
|
2014-08-08 03:41:51 +08:00
|
|
|
XFAIL xfail_demo.py::test_hello7
|
2015-09-21 22:29:50 +08:00
|
|
|
|
2015-06-07 05:30:49 +08:00
|
|
|
======= 7 xfailed in 0.12 seconds ========
|
2010-10-11 05:45:45 +08:00
|
|
|
|
2013-05-21 09:12:45 +08:00
|
|
|
.. _`skip/xfail with parametrize`:
|
|
|
|
|
|
|
|
Skip/xfail with parametrize
|
|
|
|
---------------------------
|
|
|
|
|
|
|
|
It is possible to apply markers like skip and xfail to individual
|
2013-10-02 07:54:34 +08:00
|
|
|
test instances when using parametrize::
|
2013-05-21 09:12:45 +08:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(("n", "expected"), [
|
|
|
|
(1, 2),
|
|
|
|
pytest.mark.xfail((1, 0)),
|
|
|
|
pytest.mark.xfail(reason="some bug")((1, 3)),
|
2013-10-02 07:54:34 +08:00
|
|
|
(2, 3),
|
2013-05-21 09:12:45 +08:00
|
|
|
(3, 4),
|
|
|
|
(4, 5),
|
|
|
|
pytest.mark.skipif("sys.version_info >= (3,0)")((10, 11)),
|
|
|
|
])
|
|
|
|
def test_increment(n, expected):
|
|
|
|
assert n + 1 == expected
|
|
|
|
|
2011-03-05 20:08:43 +08:00
|
|
|
|
2011-09-06 17:43:42 +08:00
|
|
|
Imperative xfail from within a test or setup function
|
2010-05-26 03:01:43 +08:00
|
|
|
------------------------------------------------------
|
|
|
|
|
2013-07-07 00:54:24 +08:00
|
|
|
If you cannot declare xfail- of skipif conditions at import
|
2013-05-08 00:40:26 +08:00
|
|
|
time you can also imperatively produce an according outcome
|
|
|
|
imperatively, in test or setup code::
|
2010-05-26 03:01:43 +08:00
|
|
|
|
|
|
|
def test_function():
|
|
|
|
if not valid_config():
|
2013-05-08 00:40:26 +08:00
|
|
|
pytest.xfail("failing configuration (but should work)")
|
|
|
|
# or
|
2013-12-20 06:41:07 +08:00
|
|
|
pytest.skip("unsupported configuration")
|
2010-05-26 03:01:43 +08:00
|
|
|
|
2009-10-15 22:18:57 +08:00
|
|
|
|
2011-09-06 17:43:42 +08:00
|
|
|
Skipping on a missing import dependency
|
2009-10-23 02:57:21 +08:00
|
|
|
--------------------------------------------------
|
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
|
|
|
|
2013-07-07 00:54:24 +08:00
|
|
|
The version will be read from the specified
|
2013-05-08 00:40:26 +08:00
|
|
|
module's ``__version__`` attribute.
|
2009-10-15 22:18:57 +08:00
|
|
|
|
|
|
|
|
2013-10-15 19:45:55 +08:00
|
|
|
.. _string conditions:
|
2013-05-08 00:40:26 +08:00
|
|
|
|
|
|
|
specifying conditions as strings versus booleans
|
|
|
|
----------------------------------------------------------
|
|
|
|
|
|
|
|
Prior to pytest-2.4 the only way to specify skipif/xfail conditions was
|
|
|
|
to use strings::
|
2009-10-15 22:18:57 +08:00
|
|
|
|
2013-05-08 00:40:26 +08:00
|
|
|
import sys
|
|
|
|
@pytest.mark.skipif("sys.version_info >= (3,3)")
|
2009-10-15 22:18:57 +08:00
|
|
|
def test_function():
|
2013-05-08 00:40:26 +08:00
|
|
|
...
|
|
|
|
|
2013-07-07 00:54:24 +08:00
|
|
|
During test function setup the skipif condition is evaluated by calling
|
|
|
|
``eval('sys.version_info >= (3,0)', namespace)``. The namespace contains
|
2013-05-08 00:40:26 +08:00
|
|
|
all the module globals, and ``os`` and ``sys`` as a minimum.
|
|
|
|
|
2013-07-07 00:54:24 +08:00
|
|
|
Since pytest-2.4 `condition booleans`_ are considered preferable
|
2013-05-08 00:40:26 +08:00
|
|
|
because markers can then be freely imported between test modules.
|
|
|
|
With strings you need to import not only the marker but all variables
|
|
|
|
everything used by the marker, which violates encapsulation.
|
|
|
|
|
2014-01-18 19:31:33 +08:00
|
|
|
The reason for specifying the condition as a string was that ``pytest`` can
|
2013-07-07 00:54:24 +08:00
|
|
|
report a summary of skip conditions based purely on the condition string.
|
|
|
|
With conditions as booleans you are required to specify a ``reason`` string.
|
2013-05-08 00:40:26 +08:00
|
|
|
|
|
|
|
Note that string conditions will remain fully supported and you are free
|
|
|
|
to use them if you have no need for cross-importing markers.
|
|
|
|
|
|
|
|
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.
|
2013-07-07 00:54:24 +08:00
|
|
|
|
2013-05-08 00:40:26 +08:00
|
|
|
* 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(...):
|
|
|
|
...
|
|
|
|
|
|
|
|
The equivalent with "boolean conditions" is::
|
|
|
|
|
|
|
|
@pytest.mark.skipif(not pytest.config.getvalue("db"),
|
|
|
|
reason="--db was not specified")
|
|
|
|
def test_function(...):
|
|
|
|
pass
|
|
|
|
|
2014-03-26 14:15:54 +08:00
|
|
|
.. note::
|
2009-10-15 22:18:57 +08:00
|
|
|
|
2014-03-26 14:15:54 +08:00
|
|
|
You cannot use ``pytest.config.getvalue()`` in code
|
|
|
|
imported before py.test's argument parsing takes place. For example,
|
|
|
|
``conftest.py`` files are imported before command line parsing and thus
|
|
|
|
``config.getvalue()`` will not execute correctly.
|