Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listing all tests associated with a given marker in Pytest

Tags:

python

pytest

In Pytest we have the pytest --markers command which will list all of the markers that are available for use.

However, I am not seeing a command to list tests associated with x marker. The documentation didn't appear to cover this so is this a possibility in Pytest?

like image 899
Wunderbread Avatar asked Aug 09 '17 19:08

Wunderbread


People also ask

How do you use pytest markers?

To use markers, we have to import pytest module in the test file. We can define our own marker names to the tests and run the tests having those marker names. -m <markername> represents the marker name of the tests to be executed.

How do I run multiple markers in pytest?

Multiple custom markers can be registered, by defining each one in its own line, as shown in above example. You can ask which markers exist for your test suite - the list includes our just defined webtest and slow markers: $ pytest --markers @pytest. mark.

What is Pytestmark?

Pytest allows to group tests using markers. pytest.mark decorator is used to mark a test function with custom metadata like @pytest.mark.smoke. This is very handy when we want to run only a subset of tests like "smoke tests" to quickly verify if the changes made by the developer not breaking any major functionalities.


2 Answers

Use --collect-only in conjuction with -m <marker>:

$ py.test --collect-only -m x
=========================== test session starts ===========================
platform linux -- Python 3.6.2, pytest-3.2.1, py-1.4.34, pluggy-0.4.0
rootdir: /home/they4kman/.virtualenvs/tmp-e1f1b42d6ff9bfa/src, inifile:
collected 3 items                                                          
<Module 'test_markers.py'>
  <Function 'test_x'>

====================== no tests ran in 0.00 seconds =======================
like image 118
theY4Kman Avatar answered Nov 14 '22 23:11

theY4Kman


A more cleaner output can be achieved by using:

(spark_pytest) ALIPL0958:Spark_pytest kapilmathur$ pytest -m integration --collect-only -qq


tests/Servers/test_server_rebuild.py::TestRebuildWithVolumes::test_rebuild_with_volume_attached
tests/Servers/test_server_rebuild.py::TestRebuildWithVolumes::test_post_rebuild_with_second_volume_attach
tests/Servers/test_server_resize.py::TestResizeWithVolumes::test_resize_with_volume_attached
tests/Servers/test_server_resize.py::TestResizeWithVolumes::test_post_resize_with_second_volume_attach

enter image description here

like image 31
nomoreabond2017 Avatar answered Nov 14 '22 22:11

nomoreabond2017