2009-10-23 02:57:21 +08:00
|
|
|
|
|
|
|
generic mechanism for marking python functions.
|
2010-01-13 23:00:33 +08:00
|
|
|
===============================================
|
|
|
|
|
2009-10-23 02:57:21 +08:00
|
|
|
|
|
|
|
.. contents::
|
|
|
|
:local:
|
|
|
|
|
|
|
|
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::
|
|
|
|
|
|
|
|
@py.test.mark.webtest
|
|
|
|
def test_send_http():
|
|
|
|
...
|
|
|
|
|
|
|
|
This will set a "Marker" instance as a function attribute named "webtest".
|
|
|
|
You can also specify parametrized meta data like this::
|
|
|
|
|
|
|
|
@py.test.mark.webtest(firefox=30)
|
|
|
|
def test_receive():
|
|
|
|
...
|
|
|
|
|
|
|
|
The named marker can be accessed like this later::
|
|
|
|
|
|
|
|
test_receive.webtest.kwargs['firefox'] == 30
|
|
|
|
|
|
|
|
In addition to set key-value pairs you can also use positional arguments::
|
|
|
|
|
|
|
|
@py.test.mark.webtest("triangular")
|
|
|
|
def test_receive():
|
|
|
|
...
|
|
|
|
|
|
|
|
and later access it with ``test_receive.webtest.args[0] == 'triangular``.
|
|
|
|
|
|
|
|
.. _`scoped-marking`:
|
|
|
|
|
2010-05-22 00:11:47 +08:00
|
|
|
Marking whole classes or modules
|
2009-10-23 02:57:21 +08:00
|
|
|
----------------------------------------------------
|
|
|
|
|
2010-05-22 00:11:47 +08:00
|
|
|
If you are programming with Python2.6 you may use ``py.test.mark`` decorators
|
|
|
|
with classes to apply markers to all its test methods::
|
|
|
|
|
|
|
|
@py.test.mark.webtest
|
|
|
|
class TestClass:
|
|
|
|
def test_startup(self):
|
|
|
|
...
|
|
|
|
|
|
|
|
This is equivalent to directly applying the decorator to the
|
|
|
|
``test_startup`` function.
|
|
|
|
|
|
|
|
To remain compatible with Python2.5 you can instead set a
|
|
|
|
``pytestmark`` attribute on a TestClass like this::
|
2009-10-23 02:57:21 +08:00
|
|
|
|
|
|
|
import py
|
|
|
|
|
|
|
|
class TestClass:
|
|
|
|
pytestmark = py.test.mark.webtest
|
|
|
|
|
2010-05-22 00:11:47 +08:00
|
|
|
or if you need to use multiple markers::
|
|
|
|
|
|
|
|
import py
|
|
|
|
|
|
|
|
class TestClass:
|
|
|
|
pytestmark = [py.test.mark.webtest, pytest.mark.slowtest]
|
2009-10-23 02:57:21 +08:00
|
|
|
|
|
|
|
You can also set a module level marker::
|
|
|
|
|
|
|
|
import py
|
|
|
|
pytestmark = py.test.mark.webtest
|
|
|
|
|
2010-05-22 00:11:47 +08:00
|
|
|
in which case then it will be applied to all functions and
|
2009-10-23 02:57:21 +08:00
|
|
|
methods defined in the module.
|
|
|
|
|
2009-11-06 00:46:14 +08:00
|
|
|
Using "-k MARKNAME" to select tests
|
|
|
|
----------------------------------------------------
|
|
|
|
|
|
|
|
You can use the ``-k`` command line option to select
|
|
|
|
tests::
|
|
|
|
|
|
|
|
py.test -k webtest # will only run tests marked as webtest
|
2009-10-23 02:57:21 +08:00
|
|
|
|
|
|
|
Start improving this plugin in 30 seconds
|
|
|
|
=========================================
|
|
|
|
|
|
|
|
|
|
|
|
1. Download `pytest_mark.py`_ plugin source code
|
|
|
|
2. put it somewhere as ``pytest_mark.py`` into your import path
|
|
|
|
3. a subsequent ``py.test`` run will use your local version
|
|
|
|
|
|
|
|
Checkout customize_, other plugins_ or `get in contact`_.
|
|
|
|
|
|
|
|
.. include:: links.txt
|