pytest-django icon indicating copy to clipboard operation
pytest-django copied to clipboard

How to persisit data inside the database after tests run

Open George3d6 opened this issue 3 years ago • 1 comments
trafficstars

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.

George3d6 avatar Sep 07 '22 15:09 George3d6

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.

mgax avatar Sep 07 '22 16:09 mgax