Skip to main content

pytest-django-asyncio

pytest-django-asyncio is a small temporary compatibility plugin for projects that need async Django ORM access to work correctly with pytest-django database fixtures before the equivalent upstream support lands.

It backports the async database fixture behavior proposed in pytest-django PR #1223 and exposes it as an installed pytest plugin through pytest11, so downstream projects get the fix automatically after installation.

I'll deprecate it from pypi once the linked MR gets merged into pytest-django.

What does this fix/why do tests fail in async contexts?

pytest-django's normal db fixture wraps each test in a transaction and rolls it back afterwards. In async tests, Django's async ORM work runs via sync_to_async on a dedicated thread, so the transaction setup and teardown also need to happen on that thread or database state can escape the rollback boundary.

This package backports the fixture split and async helper approach from pytest-django PR #1223 so async tests marked with @pytest.mark.asyncio can use Django DB fixtures safely when this plugin is installed as a normal dependency.

Installation

pip install pytest-django-asyncio

You still need the usual pytest stack in the consuming project:

  • pytest
  • pytest-django
  • pytest-asyncio
  • Django

Usage

After installation, pytest auto-loads the plugin through its pytest11 entry point. That means it should be ready to go as long as you have the package installed in your python environment.

Typical usage:

import pytest


@pytest.mark.asyncio
@pytest.mark.django_db
async def test_async_orm_works():
    ...

The same backport also works when tests request db or transactional_db directly.

Caveats

  • If a project disables pytest plugin auto-loading, it must explicitly add pytest_django_asyncio.pytest_plugin to its plugin list.

How It Works

The package exposes a thin pytest_plugin.py entry module. During pytest_configure, it applies the small DjangoDbBlocker.unblock(async_only=...) backport needed by the async path and then registers the real fixture override module late with @pytest.hookimpl(trylast=True).

That late registration lets this package's _django_db_helper override win over pytest-django.

The sync path delegates back to pytest-django; the async path uses a separate helper modeled on pytest-django PR #1223: the transaction start/stop run in the same async worker thread that will execute the actual test, using async_to_sync.

Metadata

Release files for pytest-django-asyncio 0.2.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-asyncio 0.2.0
File Size Uploaded
pytest_django_asyncio-0.2.0.tar.gz 5.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pytest-django-asyncio 0.2.0
File Interpreter ABI Platform
pytest_django_asyncio-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 12.8 kB

Release files / pytest_django_asyncio-0.2.0.tar.gz

Download URL pytest_django_asyncio-0.2.0.tar.gz
Size 5.5 kB
Tags Source
SHA-256 checksum
How to use checksums
d8848706211ed7b036ce203d282355ea1cead4877a214f01a1f191eeb5bf23b3
BLAKE2b-256 checksum
How to use checksums
be9ce53e4dd11c980d8ac999955a252c0c90530d900bbd6eaac54ec5b533675c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.1 {"installer":{"name":"uv","version":"0.11.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / pytest_django_asyncio-0.2.0-py3-none-any.whl

Download URL pytest_django_asyncio-0.2.0-py3-none-any.whl
Size 7.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
35325b32a40809fd9b6e6ed243fa4c021b8cddee5ebf4e4a3cb7f7c699de4070
BLAKE2b-256 checksum
How to use checksums
5cd5852106f1ce3bd381c77ee2eb5f69d0096da075267a15d55e1df098b89793
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.1 {"installer":{"name":"uv","version":"0.11.1","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.5

2 release files

0.1.1

2 release files

0.1.0

2 release files

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