Django Testing: Does --keepdb reset changes made during tests?
Asked Answered
M

1

12

According to the Django docs regarding tests, the --keepdb flag will preserve the the test database for future runs.

https://docs.djangoproject.com/en/1.8/ref/django-admin/#django-admin-option---keepdb

Just to be clear, will any changes made to the database by the tests (ie: object.save() ) be reset automatically? Or will these changes need to be reversed from within the tests?

Mixie answered 8/7, 2015 at 9:16 Comment(0)
A
18

If you're using Django's default TestCase, all tests are run in a transaction, which is rolled back when the tests finishes. If your database supports transactions, you won't have to clean up anything.

If you're using Django's LiveServerTestCase or TransactionTestCase, all tables are truncated after each test, and the initial data, which is serialized before the test, is reloaded into the test database. This will not preserve any data for migrated apps, only for unmigrated apps.

The --keepdb option will not do anything special with the database. It simply prevents that the test database is destroyed, and if a database is found at the start of the tests, it is used instead of creating a new one. So, any data that is somehow left in the database when the tests finish will be seen as initial data. This is mostly relevant if some error or a user interrupt prevents tests without transactions from cleaning up the database. In that case it may be a good idea to recreate the database.

Ardin answered 8/7, 2015 at 9:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.