Repository navigation
Replies: 1 comment
|
Hi @ccurvey The key detail here is: This is pytest-django behavior rather than pytest core.
If you changed migrations or migration-created data, the existing reused test database can be stale. In that situation use: pytest --reuse-db --create-db
That explains why: pytest --create-dbmakes the tests pass. There is a second issue to consider, though: tests using Django's database support perform database isolation/cleanup. Data that a test modifies or creates should not be expected to remain in the reused database after the suite. So I would not rely on: as your test-data strategy. If those categories are required for a test, establish that precondition deterministically. Depending on what the data represents, that can be:
For example, for test-specific reference data: @pytest.fixture
def categories(db):
return [
Category.objects.create(name="A"),
Category.objects.create(name="B"),
]Then tests that require those categories request the fixture explicitly. For your immediate problem I would do two things:
pytest --reuse-db --create-db
So Please mark this answer as accepted if it helped, thank you. |
Uh oh!
There was an error while loading. Please reload this page.
I'm sure this is something I've done; I'm hoping the community can help me figure out where I went wrong.
I'm using pytest with Django. In my migrations, I create a Category class, and populate it with reference data. I have
--reuse-dbin mypytest.ini.A few days ago, some of my tests started failing because some of my Category reference data was missing. I can get my tests to pass if I specify
--create-dbon the command line. However, after a test run, the Category table exists in Postgres, but it's empty.Anyone seen something like this before? Any ideas on how I might be able to track down the issue?
All reactions