pytest-dev / pytest-dev/pytest-django

Pass through Django data `fixtures` to TransactionTestCase

Open
#844 1 comment 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.5k
Forks
367
PR merge metrics
No merged PRs in 30d

Description

I am writing some tests which need to disable Django's autocommit, as the code under test manages this itself. However, after the first test case, the database is flushed, and any useful state that was created in the migrations or data fixtures are lost which causes subsequent tests to fail. Specifically, it's the records in the Site table that must be available for the subsequent tests.

I've created a Django data fixture, sites.json. However, there doesn't seem to be an easy way in pytest-django to ensure this is loaded before each test. The TransactionTestCase has a fixtures attribute which can be used for this purpose.

Ideally, mark.django_db should pass through a list of fixtures to the test case like so:

pytestmark = pytest.mark.django_db(transaction=True, fixtures=["sites.json"])

Edit: I have tried overriding the django_db fixture as per the example in the docs. However, this also gets flushed after the first transactional test.

Is there another way to handle this issue apart from adding the loaddata code to each test?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the django_db marker and database setup for transaction=True, then compare its behavior with Django's TransactionTestCase fixture loading. Done means a fixtures list can be passed through the marker and the named data is available before each transactional test, including tests after a database flush.

Written by the indexing model from the issue text.

Assessment

Tech stack
django, python
Domain
testing
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.