You can do that using pytest-dependency plugin. Take the below code as an example
import random import pytest class TestExample: @pytest.mark.dependency() def test_abc(self): print("Hello") assert random.randint(0, 1) @pytest.mark.dependency(depends=["TestExample::test_abc"]) def test_xyz(self): print("Hi") assert random.randint(0, 1) @pytest.mark.dependency(depends=["TestExample::test_abc", "TestExample::test_xyz"]) def test_aaa(self): print("World") def test_sample(self): print("Python")
In the example test_xyz will execute only if test_abc is successful, and test_aaa will execute only if both test_abc and test_xyz have passed. test_sample will be always executed as it doesn't depend on anything.
More information about the plugin can be found here: https://pytest-dependency.readthedocs.io/en/stable/usage.html
test_abcruns beforetest_xyzin the first place.