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
pytest -m "custom_marker"runs all tests with that marker it does not change execution order.I recommend looking into the pytest-order plugin. This allows the use of a @pytest.marker.order(#) marker and can even be applied to the class level. So you could have all your m1 tests in one TestClass and all the m2 tests in another.
Alternatively you can try looking into the "pytest_collection_modifyitems" hook, but I am not very familiar with using it.