2009-10-23 02:57:21 +08:00
2010-10-11 05:45:45 +08:00
.. _mark:
2011-09-06 17:43:42 +08:00
Marking test functions with attributes
2010-10-13 18:26:14 +08:00
=================================================================
2010-01-13 23:00:33 +08:00
2010-11-13 18:10:45 +08:00
.. currentmodule:: _pytest.mark
2010-10-14 01:30:00 +08:00
2011-11-12 06:56:06 +08:00
By using the ``pytest.mark`` helper you can easily set
metadata on your test functions. To begin with, there are
some builtin markers, for example:
2009-10-23 02:57:21 +08:00
2011-11-17 19:09:21 +08:00
* :ref:`skipif <skipif>` - skip a test function if a certain condition is met
* :ref:`xfail <xfail>` - produce an "expected failure" outcome if a certain
2011-11-12 06:56:06 +08:00
condition is met
2011-11-17 19:09:21 +08:00
* :ref:`parametrize <parametrizemark>` to perform multiple calls
to the same test function.
2011-11-12 06:56:06 +08:00
It's also easy to create custom markers or to apply markers
to whole test classes or modules.
marking test functions and selecting them for a run
2009-10-23 02:57:21 +08:00
----------------------------------------------------
2011-11-12 06:56:06 +08:00
You can "mark" a test function with custom metadata like this::
# content of test_server.py
2009-10-23 02:57:21 +08:00
2010-11-18 05:12:16 +08:00
import pytest
@pytest.mark.webtest
2009-10-23 02:57:21 +08:00
def test_send_http():
2011-11-12 06:56:06 +08:00
pass # perform some webtest test for your app
2011-11-12 07:02:06 +08:00
def test_something_quick():
pass
2009-10-23 02:57:21 +08:00
2011-11-12 06:56:06 +08:00
.. versionadded:: 2.2
2010-10-13 18:26:14 +08:00
2011-11-12 07:02:06 +08:00
You can then restrict a test run to only run tests marked with ``webtest``::
2009-10-23 02:57:21 +08:00
2011-11-12 07:02:06 +08:00
$ py.test -v -m webtest
============================= test session starts ==============================
platform darwin -- Python 2.7.1 -- pytest-2.2.0.dev6 -- /Users/hpk/venv/0/bin/python
collecting ... collected 2 items
test_server.py:3: test_send_http PASSED
===================== 1 tests deselected by "-m 'webtest'" =====================
==================== 1 passed, 1 deselected in 0.01 seconds ====================
2011-11-12 06:56:06 +08:00
Or the inverse, running all tests except the webtest ones::
2011-11-12 07:02:06 +08:00
$ py.test -v -m "not webtest"
============================= test session starts ==============================
platform darwin -- Python 2.7.1 -- pytest-2.2.0.dev6 -- /Users/hpk/venv/0/bin/python
collecting ... collected 2 items
test_server.py:6: test_something_quick PASSED
=================== 1 tests deselected by "-m 'not webtest'" ===================
==================== 1 passed, 1 deselected in 0.01 seconds ====================
2011-11-12 06:56:06 +08:00
Registering markers
-------------------------------------
.. versionadded:: 2.2
.. ini-syntax for custom markers:
Registering markers for your test suite is simple::
# content of pytest.ini
[pytest]
markers =
webtest: mark a test as a webtest.
2011-11-12 07:02:06 +08:00
You can ask which markers exist for your test suite - the list includes our just defined ``webtest`` markers::
2011-11-12 06:56:06 +08:00
$ py.test --markers
2011-11-12 07:02:06 +08:00
@pytest.mark.webtest: mark a test as a webtest.
@pytest.mark.skipif(*conditions): skip the given test function if evaluation of all conditions has a True value. Evaluation happens within the module global context. Example: skipif('sys.platform == "win32"') skips the test if we are on the win32 platform.
@pytest.mark.xfail(*conditions, reason=None, run=True): mark the the test function as an expected failure. Optionally specify a reason and run=False if you don't even want to execute the test function. Any positional condition strings will be evaluated (like with skipif) and if one is False the marker will not be applied.
@pytest.mark.tryfirst: mark a hook implementation function such that the plugin machinery will try to call it first/as early as possible.
@pytest.mark.trylast: mark a hook implementation function such that the plugin machinery will try to call it last/as late as possible.
2011-11-12 06:56:06 +08:00
For an example on how to add and work markers from a plugin, see
:ref:`adding a custom marker from a plugin`.
.. note::
It is recommended to explicitely register markers so that:
2009-10-23 02:57:21 +08:00
2011-11-12 06:56:06 +08:00
* there is one place in your test suite defining your markers
2009-10-23 02:57:21 +08:00
2011-11-12 06:56:06 +08:00
* asking for existing markers via ``py.test --markers`` gives good output
2009-10-23 02:57:21 +08:00
2011-11-12 06:56:11 +08:00
* typos in function markers are treated as an error if you use
the ``--strict`` option. Later versions of py.test are probably
going to treat non-registered markers as an error.
2009-10-23 02:57:21 +08:00
.. _`scoped-marking`:
2010-07-27 03:15:15 +08:00
Marking whole classes or modules
2009-10-23 02:57:21 +08:00
----------------------------------------------------
2010-11-18 05:12:16 +08:00
If you are programming with Python2.6 you may use ``pytest.mark`` decorators
2011-03-04 06:40:38 +08:00
with classes to apply markers to all of its test methods::
2010-05-22 00:11:47 +08:00
2010-10-13 18:26:14 +08:00
# content of test_mark_classlevel.py
2010-11-18 05:12:16 +08:00
import pytest
@pytest.mark.webtest
2010-05-22 00:11:47 +08:00
class TestClass:
def test_startup(self):
2010-10-13 18:26:14 +08:00
pass
2010-05-26 03:01:43 +08:00
def test_startup_and_more(self):
2010-10-13 18:26:14 +08:00
pass
2010-05-22 00:11:47 +08:00
This is equivalent to directly applying the decorator to the
2010-07-27 03:15:15 +08:00
two test functions.
2010-05-22 00:11:47 +08:00
2011-11-12 06:56:06 +08:00
To remain backward-compatible with Python2.4 you can also set a
2010-05-22 00:11:47 +08:00
``pytestmark`` attribute on a TestClass like this::
2009-10-23 02:57:21 +08:00
2010-11-18 05:12:16 +08:00
import pytest
2009-10-23 02:57:21 +08:00
class TestClass:
2010-11-18 05:12:16 +08:00
pytestmark = pytest.mark.webtest
2009-10-23 02:57:21 +08:00
2010-05-26 03:01:43 +08:00
or if you need to use multiple markers you can use a list::
2010-05-22 00:11:47 +08:00
2010-11-18 05:12:16 +08:00
import pytest
2010-05-22 00:11:47 +08:00
class TestClass:
2010-11-18 05:12:16 +08:00
pytestmark = [pytest.mark.webtest, pytest.mark.slowtest]
2009-10-23 02:57:21 +08:00
You can also set a module level marker::
2010-11-18 05:12:16 +08:00
import pytest
pytestmark = pytest.mark.webtest
2009-10-23 02:57:21 +08:00
2010-07-27 03:15:15 +08:00
in which case it will be applied to all functions and
methods defined in the module.
2009-10-23 02:57:21 +08:00
2010-10-14 01:30:00 +08:00
Using ``-k TEXT`` to select tests
2009-11-06 00:46:14 +08:00
----------------------------------------------------
2011-11-12 07:02:06 +08:00
You can use the ``-k`` command line option to only run tests with names that match the given argument::
2010-10-13 18:26:14 +08:00
2011-11-12 07:02:06 +08:00
$ py.test -k send_http # running with the above defined examples
============================= test session starts ==============================
platform darwin -- Python 2.7.1 -- pytest-2.2.0.dev6
2010-11-26 20:26:56 +08:00
collecting ... collected 4 items
2010-10-13 18:26:14 +08:00
2011-11-12 07:02:06 +08:00
test_server.py .
2010-10-13 18:26:14 +08:00
2011-11-12 07:02:06 +08:00
===================== 3 tests deselected by '-ksend_http' ======================
==================== 1 passed, 3 deselected in 0.02 seconds ====================
2010-10-13 18:26:14 +08:00
And you can also run all tests except the ones that match the keyword::
2011-11-12 07:02:06 +08:00
$ py.test -k-send_http
============================= test session starts ==============================
platform darwin -- Python 2.7.1 -- pytest-2.2.0.dev6
2010-11-26 20:26:56 +08:00
collecting ... collected 4 items
2010-10-13 18:26:14 +08:00
2011-11-12 07:02:06 +08:00
test_mark_classlevel.py ..
test_server.py .
===================== 1 tests deselected by '-k-send_http' =====================
==================== 3 passed, 1 deselected in 0.03 seconds ====================
2010-10-13 18:26:14 +08:00
2010-10-14 01:30:00 +08:00
Or to only select the class::
$ py.test -kTestClass
2011-11-12 07:02:06 +08:00
============================= test session starts ==============================
platform darwin -- Python 2.7.1 -- pytest-2.2.0.dev6
2010-11-26 20:26:56 +08:00
collecting ... collected 4 items
2010-10-14 01:30:00 +08:00
test_mark_classlevel.py ..
2011-11-12 07:02:06 +08:00
===================== 2 tests deselected by '-kTestClass' ======================
==================== 2 passed, 2 deselected in 0.02 seconds ====================
2010-10-14 01:30:00 +08:00
2010-10-13 18:26:14 +08:00
API reference for mark related objects
------------------------------------------------
2009-11-06 00:46:14 +08:00
2010-10-14 01:30:00 +08:00
.. autoclass:: MarkGenerator
:members:
.. autoclass:: MarkDecorator
:members:
.. autoclass:: MarkInfo
:members:
2009-10-23 02:57:21 +08:00