Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I keep test data after Django tests complete?

I am using Django 1.8 and the docs say to use --keepdb to save the test database.

I am doing that and the database is there but every time I see it, it is empty and has no data in it.

Is there any way that I can preserve that so that I can see what's in there?

like image 306
user3214546 Avatar asked May 28 '15 10:05

user3214546


People also ask

What order Django tests are run?

Order in which tests are executed All TestCase subclasses are run first. Then, all other Django-based tests (test cases based on SimpleTestCase , including TransactionTestCase ) are run with no particular ordering guaranteed nor enforced among them.

What should you test in Django?

As discussed above, we should test anything that is part of our design or that is defined by code that we have written, but not libraries/code that is already tested by Django or the Python development team. For example, consider the Author model below.


1 Answers

All of your code is running within database transactions, which get rolled back at the end of each test.

From the Django testing docs:

Here is an example which subclasses from django.test.TestCase, which is a subclass of unittest.TestCase that runs each test inside a transaction to provide isolation:

This "isolation" means that anything you do inside of the test will be rolled back before the next test starts.

Instead, you want to use Python's class unittest.TestCase.

Another quote from the Django docs:

Using unittest.TestCase avoids the cost of running each test in a transaction and flushing the database, but if your tests interact with the database their behavior will vary based on the order that the test runner executes them. This can lead to unit tests that pass when run in isolation but fail when run in a suite.

As long as you can guarantee that your tests won't clobber each other's data, you can safely use this class instead of Django's test case.

like image 114
Thane Brimhall Avatar answered Sep 29 '22 13:09

Thane Brimhall