Skip to main content

pytest-django-queries

Generate performance reports from your django database performance tests (inspired by coverage.py).

Coverage Status Documentation Status Version

Supported versions Supported implementations

Usage

Install pytest-django-queries, write your pytest tests and mark any test that should be counted or use the count_queries fixture.

Note: to use the latest development build, use pip install --pre pytest-django-queries

import pytest


@pytest.mark.count_queries
def test_query_performances():
    Model.objects.all()


# Or...
def test_another_query_performances(count_queries):
    Model.objects.all()

Each test file and/or package is considered as a category. Each test inside a "category" compose its data, see Visualising Results for more details.

You will find the full documentation here.

Recommendation when Using Fixtures

You might end up in the case where you want to add fixtures that are generating queries that you don't want to be counted in the results–or simply, you want to use the pytest-django plugin alongside of pytest-django-queries, which will generate unwanted queries in your results.

For that, you will want to put the count_queries fixture as the last fixture to execute.

But at the same time, you might want to use the the power of pytest markers, to separate the queries counting tests from other tests. In that case, you might want to do something like this to tell the marker to not automatically inject the count_queries fixture into your test:

import pytest


@pytest.mark.count_queries(autouse=False)
def test_retrieve_main_menu(fixture_making_queries, count_queries):
    pass

Notice the usage of the keyword argument autouse=False and the count_queries fixture being placed last.

Using pytest-django alongside of pytest-django-queries

We recommend you to do the following when using pytest-django:

import pytest


@pytest.mark.django_db
@pytest.mark.count_queries(autouse=False)
def test_retrieve_main_menu(any_fixture, other_fixture, count_queries):
    pass

Integrating with GitHub

TBA.

Testing Locally

Simply install pytest-django-queries through pip and run your tests using pytest. A report should have been generated in your current working directory in a file called with .pytest-queries.

Note: to override the save path, pass the --django-db-bench PATH option to pytest.

Visualising Results

You can generate a table from the tests results by using the show command:

django-queries show

You will get something like this to represent the results:

+---------+--------------------------------------+
| Module  |          Tests                       |
+---------+--------------------------------------+
| module1 | +-----------+---------+------------+ |
|         | | Test Name | Queries | Duplicated | |
|         | +-----------+---------+------------+ |
|         | |   test1   |    0    |     0      | |
|         | +-----------+---------+------------+ |
|         | |   test2   |    1    |     0      | |
|         | +-----------+---------+------------+ |
+---------+--------------------------------------+
| module2 | +-----------+---------+------------+ |
|         | | Test Name | Queries | Duplicated | |
|         | +-----------+---------+------------+ |
|         | |   test1   |   123   |     0      | |
|         | +-----------+---------+------------+ |
+---------+--------------------------------------+

Exporting the Results (HTML)

For a nicer presentation, use the html command, to export the results as HTML.

django-queries html

It will generate something like this.

Comparing Results

You can run django-queries backup (can take a path, optionally) after running your tests then rerun them. After that, you can run django-queries diff to generate results looking like this:

screenshot

Development

Refer to the contribution guidelines.

Metadata

Release files for pytest-django-queries 1.3.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pytest-django-queries 1.3.0
File Size Uploaded
pytest_django_queries-1.3.0.tar.gz 10.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-django-queries 1.3.0
File Interpreter ABI Platform
pytest_django_queries-1.3.0-py3-none-any.whl Python 3 none any Details

Total release size: 22.8 kB

Release files / pytest_django_queries-1.3.0.tar.gz

Download URL pytest_django_queries-1.3.0.tar.gz
Size 10.2 kB
Tags Source
SHA-256 checksum
How to use checksums
43e18574bb4cfacb65443f30b75ab085c5702afd9d2214f794451c4aa579edcd
BLAKE2b-256 checksum
How to use checksums
e13e757ebe8af2282c9a504a191cdcfb359ee47ddd905c18b431a14c4b3339a0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 1, 2026.

Transparency log

Release files / pytest_django_queries-1.3.0-py3-none-any.whl

Download URL pytest_django_queries-1.3.0-py3-none-any.whl
Size 12.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a6be143e758e52689170319151dd1f0a4c6f518601bb1c9490b2590e3c876a4d
BLAKE2b-256 checksum
How to use checksums
8aaf1712685a320fa2f32948e45cf1a82b2c91d3ef7166cfc1219b7c323c68c3
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Mar 1, 2026.

Transparency log
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page