test_ok1/doc/mark.txt

133 lines
3.6 KiB
Plaintext
Raw Normal View History

.. _mark:
mark test functions with attributes
=================================================================
.. currentmodule:: _pytest.mark
By using the ``py.test.mark`` helper you can instantiate
decorators that will set named meta data on test functions.
Marking a single function
----------------------------------------------------
You can "mark" a test function with meta data like this::
import py
@py.test.mark.webtest
def test_send_http():
...
This will set the function attribute ``webtest`` to a :py:class:`MarkInfo`
instance. You can also specify parametrized meta data like this::
# content of test_mark.py
import py
@py.test.mark.webtest(firefox=30)
def test_receive():
pass
@py.test.mark.webtest("functional", firefox=30)
def test_run_and_look():
pass
and access it from other places like this::
test_receive.webtest.kwargs['firefox'] == 30
test_run_and_look.webtest.args[0] == "functional"
.. _`scoped-marking`:
Marking whole classes or modules
----------------------------------------------------
If you are programming with Python2.6 you may use ``py.test.mark`` decorators
with classes to apply markers to all its test methods::
# content of test_mark_classlevel.py
import py
@py.test.mark.webtest
class TestClass:
def test_startup(self):
pass
def test_startup_and_more(self):
pass
This is equivalent to directly applying the decorator to the
two test functions.
To remain compatible with Python2.5 you can also set a
``pytestmark`` attribute on a TestClass like this::
import py
class TestClass:
pytestmark = py.test.mark.webtest
or if you need to use multiple markers you can use a list::
import py
class TestClass:
pytestmark = [py.test.mark.webtest, pytest.mark.slowtest]
You can also set a module level marker::
import py
pytestmark = py.test.mark.webtest
in which case it will be applied to all functions and
methods defined in the module.
Using ``-k TEXT`` to select tests
----------------------------------------------------
You can use the ``-k`` command line option to select tests::
$ py.test -k webtest # running with the above defined examples yields
=========================== test session starts ============================
platform linux2 -- Python 2.6.5 -- pytest-2.0.0.dev22
test path 1: /tmp/doc-exec-527
test_mark.py ..
test_mark_classlevel.py ..
========================= 4 passed in 0.02 seconds =========================
And you can also run all tests except the ones that match the keyword::
$ py.test -k-webtest
=========================== test session starts ============================
platform linux2 -- Python 2.6.5 -- pytest-2.0.0.dev22
test path 1: /tmp/doc-exec-527
===================== 4 tests deselected by '-webtest' =====================
======================= 4 deselected in 0.01 seconds =======================
Or to only select the class::
$ py.test -kTestClass
=========================== test session starts ============================
platform linux2 -- Python 2.6.5 -- pytest-2.0.0.dev22
test path 1: /tmp/doc-exec-527
test_mark_classlevel.py ..
==================== 2 tests deselected by 'TestClass' =====================
================== 2 passed, 2 deselected in 0.01 seconds ==================
API reference for mark related objects
------------------------------------------------
.. autoclass:: MarkGenerator
:members:
.. autoclass:: MarkDecorator
:members:
.. autoclass:: MarkInfo
:members: