Jun 13, 2024 · Updated: Jun 11, 2025 · by Tim Kamanin
When working with Django applications, it's common to have a mix of fast unit tests and slower end-to-end (E2E) tests that use Pytest's live_server fixture and browser automation tools like Playwright or Selenium. To ensure my test suite runs efficiently, I want to execute the slower tests at the end. In this blog post, I will walk you through how to configure pytest to run these slow tests last.
Running slow tests last has several advantages:
First, let's ensure you have pytest, pytest-django, and Playwright installed:
pip install pytest pytest-django pytest-playwright
playwright install
Markers in pytest allow you to tag your tests, making it easier to manage and run specific subsets. We'll define a custom marker e2e to mark all tests using live_server automatically.
Create or update your pytest.ini file:
[pytest]
DJANGO_SETTINGS_MODULE = myproject.settings
markers =
e2e: marks tests that use Django's live server (deselect with '-m "not e2e"')
To control the order in which tests are run, we will write a custom pytest plugin. This plugin will automatically mark tests that use live_server fixtures with the e2e marker and ensure that marked tests run after all other tests.
Create or update conftest.py in your project root:
def pytest_collection_modifyitems(items):
live_server_tests = []
other_tests = []
for item in items:
if "live_server" in getattr(item, "fixturenames", ()):
item.add_marker("e2e")
live_server_tests.append(item)
else:
other_tests.append(item)
# Modify the items list to run live_server tests last
items[:] = other_tests + live_server_tests
Create a test.py file in your project and add the following:
import pytest
def test_e2e_test(live_server, page):
page.goto(live_server.url)
assert page.evaluate("document.title") == "Homepage | Sitename"
def test_simple_unit_test():
assert 1 + 1 == 2
Now, when you run pytest, the tests will be executed in the desired order: the unit test first and then the end-to-end test.
pytest
You should see your fast unit tests run first, followed by the slower tests that use live_server and Playwright/Selenium.
If you want to run only unit tests, excluding slow tests marked with the e2e tag, run pytest like this:
pytest -m "not e2e"
That’s it! I wish you happy and thorough testing!
Hey, if you've found this useful, please share the post to help other folks find it: