The pytest-attrib plugin extends py.test with the ability to select tests based on a criteria rather than just the filename or pytest.mark. For example, you might want to run only tests that need internet connectivity, or tests that are slow.
The pytest.mark plugin already provides a feature to mark tests and run only the marked tests. This plugin also allows to select tests based on expressions of the attributes of the class or function, and does not require the pytest.mark decorator.
It offers features similar to the nose plugin
nose-attrib.
To maintain compatibility with nose-attrib, the --eval-attr
argument is
provided which provides eval-like attribute selection. Note that
pytest-attrib also provides the -a
argument as an alias to
--eval-attr
, hence -a
uses eval-like expressions too
(unlike nose-attr).
Install the plugin with:
pip install pytest-attrib
To use the plugin, the -a
(or --eval-attr
) CLI argument has been
provided. Consider a project with the test file:
import unittest
class MyTestCase(unittest.TestCase):
def test_function(self):
assert 1 == 1
class MySlowTestCase(unittest.TestCase):
slow = True
def test_slow_function(self):
import time
time.sleep(5)
assert 1 == 1
Using pytest-attrib, only the slow tests can be run using:
$ py.test -a slow
Or run only the fast tests using:
$ py.test -a "not slow"
The expression given in the -a
argument can be even more complex, for
example:
$ py.test -a "slow and requires_internet" $ py.test -a "slow and not requires_internet"
It can also do conditional arguments like:
$ py.test -a "speed=='slow' and requires_internet"
This code falls under the MIT License. Please note that some files or content may be copied from other places and have their own licenses. Dependencies that are being used to generate the databases also have their own licenses.