Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I tell Django to save my test database?

Running Django unit tests is far too slow. Especially when I just want to run one test but the test runner wants to create the entire database and destroy the whole thing just for that one test.

In the case where I have not changed any of my models, I could save oodles of time if Django would not bother trying to create and destroy the entire database, and instead saved it for next time. Better yet, it would be great if the test runner was capable of being able to see which models have changed and only replacing those prior to running tests.

I'd prefer to not have to subclass the test runner myself, but that's what I'm going to have to do if I don't find a solution soon. is there anything like this already in existence?

like image 539
Conley Owens Avatar asked Jun 28 '10 02:06

Conley Owens


People also ask

What database does Django test use?

To help you get started, Django provides and uses a sample settings module that uses the SQLite database. See Using another settings module to learn how to use a different settings module to run the tests with a different database.

How do I run tests PY in Django?

Open /catalog/tests/test_models.py.from django. test import TestCase # Create your tests here. Often you will add a test class for each model/view/form you want to test, with individual methods for testing specific functionality.

What is RequestFactory in Django?

The request factory The API for the RequestFactory is a slightly restricted subset of the test client API: It only has access to the HTTP methods get() , post() , put() , delete() , head() , options() , and trace() . These methods accept all the same arguments except for follow .


2 Answers

In django1.8 added new parameter for manage.py test command --keepdb

./manage.py test --keepdb
like image 139
Patrick Z Avatar answered Sep 28 '22 22:09

Patrick Z


Have you tried using an in-memory SQLite database for tests? It's much faster than using a disk-based database.

like image 25
Ned Batchelder Avatar answered Sep 29 '22 00:09

Ned Batchelder