pytest-order is a pytest plugin that allows you to run the tests in required order with respect to other tests.
pytest-order works with Python 2.7 and 3.5 - 3.10, with pytest versions >= 3.7.0
We need to install the pytest-order pytest plugin first.
pip install pytest-order
In the below example we have created four methods and marked the order randomly from 1 to 4. We will see that all the tests are executed as per given order.
test_PytestRunOrder.py
import pytest @pytest.mark.run(order=4) def test_methodA(): print("This is method A") @pytest.mark.run(order=1) def test_methodB(): print("This is method B") @pytest.mark.run(order=2) def test_methodC(): print("This is method C") @pytest.mark.run(order=3) def test_methodD(): print("This is method D")
Now let us execute the script using the below command.
pytest -v -s test_PytestRunOrder.py
OutPut data
admin@admin-MacBook runorderconcept % pytest -v -s ============================================================================ test session starts ============================================================================ platform darwin -- Python 3.8.3, pytest-5.4.3, py-1.8.1, pluggy-0.13.1 -- /Library/Frameworks/Python.framework/Versions/3.8/bin/python3.8 cachedir: .pytest_cache rootdir: /Users/admin/Documents/Skill2Lead/Tutorials/pytest/runorderconcept plugins: rerunfailures-9.0, allure-pytest-2.8.16, ordering-0.6 collected 4 items test_PytestRunOrder.py::test_methodB This is method B PASSED test_PytestRunOrder.py::test_methodC This is method C PASSED test_PytestRunOrder.py::test_methodD This is method D PASSED test_PytestRunOrder.py::test_methodA This is method A PASSED ============================================================================= 4 passed in 0.04s ============================================================================= admin@admin-MacBook runorderconcept %