Pytest Order Plugin
pytest-order is a pytest plugin that allows running tests in a specific, user-defined order. It integrates with pytest using markers, providing various levels of control including custom order, dependency-based ordering, and relative ordering. The current version is 1.3.0, and it maintains an active release cadence with regular updates.
Warnings
- breaking Python 2.7 support was dropped in version 1.0.0. Users requiring Python 2.7 compatibility must stick to pytest-order v0.11.0 or older.
- gotcha Using pytest-order with `pytest-xdist` can lead to unpredictable test execution order, as `pytest-xdist` parallelizes tests across processes, potentially breaking assumed sequential order.
- gotcha By default, `@pytest.mark.order` applies within its declared scope (module, class). Cross-module or cross-class ordering often requires explicit `@pytest.mark.run(after='...')` markers or the `--hierarchical-ordering` option.
- gotcha Version 1.3.0 introduced the `--error-on-failed-ordering` option. Previously, tests whose order could not be resolved would simply run without an enforced order. With this flag, such tests will now fail, changing default behavior if the flag is enabled.
Install
-
pip install pytest-order
Imports
- pytest.mark.order
import pytest @pytest.mark.order(1)
Quickstart
import pytest
@pytest.mark.order(1)
def test_first():
assert True
@pytest.mark.order(2)
def test_second():
assert True
@pytest.mark.run(after='test_first')
def test_dependency_on_first():
assert True
@pytest.mark.run(before='test_second')
def test_dependency_before_second():
assert True
# To run these tests:
# 1. Save the code as a Python file (e.g., test_ordering.py)
# 2. Run from your terminal: pytest test_ordering.py