pytest-dev / pytest-django

A Django plugin for pytest.
https://pytest-django.readthedocs.io/
Other
1.39k stars 344 forks source link

How to persisit data inside the database after tests run #1025

Open George3d6 opened 2 years ago

George3d6 commented 2 years ago

Is there any way to persist data inside a (postgres) database after the tests run.

I've run through https://pytest-django.readthedocs.io/en/latest/database.html several times and none of the solutions provided seem to help.

Marking this test with @pytest.mark.django_db doesn't help, using the django_db_blocker fixture and calling .unblock() as the first line of the test doesn't work, the --reuse-db flag => in the database not being deleted but it remains empty once the tests run.

I see no object while the tests are running, so it seems to me like transactions aren't be committed, but I can't figure out how to change that.

mgax commented 2 years ago

I see no object while the tests are running, so it seems to me like transactions aren't be committed, but I can't figure out how to change that.

That's right, each test runs in a transaction, which gets rolled back when the test is done. From the docs:

By default pytest-django will set up the Django databases the first time a test needs them. Once setup, the database is cached to be used for all subsequent tests and rolls back transactions, to isolate tests from each other. This is the same way the standard Django TestCase uses the database.

If you really want to persist the data from a test, there's TransactionTestCase: https://pytest-django.readthedocs.io/en/latest/database.html#testing-transactions.