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
|
|
|
|
2010-11-18 05:12:16 +08:00
|
|
|
By using the ``pytest.mark`` helper you can instantiate
|
2011-03-04 06:40:38 +08:00
|
|
|
decorators that will set named metadata on test functions.
|
2009-10-23 02:57:21 +08:00
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
Marking a single function
|
2009-10-23 02:57:21 +08:00
|
|
|
----------------------------------------------------
|
|
|
|
|
2011-03-04 06:40:38 +08:00
|
|
|
You can "mark" a test function with metadata like this::
|
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():
|
2010-07-27 03:15:15 +08:00
|
|
|
...
|
2009-10-23 02:57:21 +08:00
|
|
|
|
2010-11-18 05:12:16 +08:00
|
|
|
This will set the function attribute ``webtest`` to a :py:class:`MarkInfo`
|
2011-03-04 06:40:38 +08:00
|
|
|
instance. You can also specify parametrized metadata like this::
|
2010-10-13 18:26:14 +08:00
|
|
|
|
|
|
|
# content of test_mark.py
|
2009-10-23 02:57:21 +08:00
|
|
|
|
2010-11-18 05:12:16 +08:00
|
|
|
import pytest
|
|
|
|
@pytest.mark.webtest(firefox=30)
|
2009-10-23 02:57:21 +08:00
|
|
|
def test_receive():
|
2010-10-13 18:26:14 +08:00
|
|
|
pass
|
2009-10-23 02:57:21 +08:00
|
|
|
|
2010-11-18 05:12:16 +08:00
|
|
|
@pytest.mark.webtest("functional", firefox=30)
|
2010-10-13 18:26:14 +08:00
|
|
|
def test_run_and_look():
|
|
|
|
pass
|
2009-10-23 02:57:21 +08:00
|
|
|
|
2010-10-13 18:26:14 +08:00
|
|
|
and access it from other places like this::
|
2009-10-23 02:57:21 +08:00
|
|
|
|
2010-10-13 18:26:14 +08:00
|
|
|
test_receive.webtest.kwargs['firefox'] == 30
|
|
|
|
test_run_and_look.webtest.args[0] == "functional"
|
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
|
|
|
|
2010-07-27 03:15:15 +08:00
|
|
|
To remain compatible with Python2.5 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
|
|
|
----------------------------------------------------
|
|
|
|
|
2010-10-13 18:26:14 +08:00
|
|
|
You can use the ``-k`` command line option to select tests::
|
|
|
|
|
2010-10-14 01:30:00 +08:00
|
|
|
$ py.test -k webtest # running with the above defined examples yields
|
2010-10-13 18:26:14 +08:00
|
|
|
=========================== test session starts ============================
|
2011-10-19 02:07:45 +08:00
|
|
|
platform darwin -- Python 2.7.1 -- pytest-2.1.3
|
2010-11-26 20:26:56 +08:00
|
|
|
collecting ... collected 4 items
|
2010-10-13 18:26:14 +08:00
|
|
|
|
|
|
|
test_mark.py ..
|
|
|
|
test_mark_classlevel.py ..
|
|
|
|
|
2011-10-19 02:07:45 +08:00
|
|
|
========================= 4 passed in 0.03 seconds =========================
|
2010-10-13 18:26:14 +08:00
|
|
|
|
|
|
|
And you can also run all tests except the ones that match the keyword::
|
|
|
|
|
2010-10-14 01:30:00 +08:00
|
|
|
$ py.test -k-webtest
|
2010-10-13 18:26:14 +08:00
|
|
|
=========================== test session starts ============================
|
2011-10-19 02:07:45 +08:00
|
|
|
platform darwin -- Python 2.7.1 -- pytest-2.1.3
|
2010-11-26 20:26:56 +08:00
|
|
|
collecting ... collected 4 items
|
2010-10-13 18:26:14 +08:00
|
|
|
|
|
|
|
===================== 4 tests deselected by '-webtest' =====================
|
2011-10-19 02:07:45 +08:00
|
|
|
======================= 4 deselected in 0.02 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
|
|
|
|
=========================== test session starts ============================
|
2011-10-19 02:07:45 +08:00
|
|
|
platform darwin -- Python 2.7.1 -- pytest-2.1.3
|
2010-11-26 20:26:56 +08:00
|
|
|
collecting ... collected 4 items
|
2010-10-14 01:30:00 +08:00
|
|
|
|
|
|
|
test_mark_classlevel.py ..
|
|
|
|
|
|
|
|
==================== 2 tests deselected by 'TestClass' =====================
|
2011-10-19 02:07:45 +08:00
|
|
|
================== 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
|
|
|
|