Skip to main content

Temporary pytest plugin backport for async Django DB fixture handling.

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest_django_asyncio-0.1.5.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pytest_django_asyncio-0.1.5-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file pytest_django_asyncio-0.1.5.tar.gz.

File metadata

  • Download URL: pytest_django_asyncio-0.1.5.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.10 {"installer":{"name":"uv","version":"0.10.10","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}

File hashes

Hashes for pytest_django_asyncio-0.1.5.tar.gz
Algorithm Hash digest
SHA256 5b25fbf42697fcb3202999c02581c3192bf4eb18d329d0c75dd0db3c6a5f1ab2
MD5 8e0800243779163aaab785bc97112004
BLAKE2b-256 61490af4d41563b3eb33d868d0b759b7f431e3c264e5086f058200aeb3d4d0ed

See more details on using hashes here.

File details

Details for the file pytest_django_asyncio-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: pytest_django_asyncio-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.10.10 {"installer":{"name":"uv","version":"0.10.10","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}

File hashes

Hashes for pytest_django_asyncio-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 06a55620e95f1a288cf51bca080a0b443fbc893d9b5c909110c19474671f316c
MD5 ceaff533eb61f4259f58721551c5bab9
BLAKE2b-256 f4705d563f69c50afb2de9ee6969a67b8e8f01921d2c3ed60773b7a215394c6f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page