Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

PyTest: Force default order

If for some reason we would like to make sure the order remains the same in a given file even when using the --random-order flag, we can add the following two lines of code.

import pytest
pytestmark = pytest.mark.random_order(disabled=True)
import pytest
pytestmark = pytest.mark.random_order(disabled=True)

def test_one():
    assert True

def test_two():
    assert True

def test_three():
    assert True


$ pytest -v --random-order

test_order.py::test_three PASSED
test_order.py::test_one PASSED
test_order.py::test_two PASSED
test_default_order.py::test_one PASSED
test_default_order.py::test_two PASSED
test_default_order.py::test_three PASSED