I am looking to execute tests cases in pytest using multiple markers (say m1
, m2
), such that all test cases marked with m1
will run first, and then test cases marked m2
will run after.
I am using the command: pytest -s -m "m1 or m2"
to run pytest here.
Example:
test_a
uses marker m1
,
test_b
uses marker m2
,
test_c
uses marker m1
,
test_d
uses marker m2
.
If I execute pytest -s -m "m1 or m2"
, the test execution order is: test_a
->test_b
->test_c
->test_d
because pytest executes them in an alphabetical order. But I want the execution to be test_a
->test_c
->test_b
->test_d
such that all tests marked m1
are executed first, and then the tests marked m2
are executed after.
import pytest
@pytest.mark.m1
def test_a():
pass
@pytest.mark.m2
def test_b():
pass
@pytest.mark.m1
def test_c():
pass
@pytest.mark.m2
def test_d():
pass
Getting a clue from @cdub's answer regarding
pytest_collection_modifyitems
, I could implement the following and got it to work. Reference to original implementaion from: https://stackoverflow.com/questions/70738211/run-pytest-classes-in-custom-order#:~:text=You%20can%20use%20the%20pytest_collection_modifyitems,allows%20to%20sort%20by%20class.