Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I specify a database for Django Tests to use instead of having it build it every time?

I want to be able to use an existing test database to run my tests against and not have Django create and delete a database every time I want to run the tests. Is this possible?

like image 332
Ryan Detzel Avatar asked Jan 05 '11 16:01

Ryan Detzel


People also ask

Which class is a base class for testing in Django test package?

The best base class for most tests is django. test. TestCase. This test class creates a clean database before its tests are run, and runs every test function in its own transaction.

What is RequestFactory in Django?

The request factory The RequestFactory shares the same API as the test client. However, instead of behaving like a browser, the RequestFactory provides a way to generate a request instance that can be used as the first argument to any view.


1 Answers

It's possible, here is a way :

1) Define your own test runner look here to see how.

2) For your custom test runner look in the default test runner, you can just copy and past the code and just comment this line : connection.creation.destroy_test_db(old_name, verbosity) which is responsible for destroying the test database, and i think you should put the connection.creation.create_test_db(..) line in a try except something like this maybe:

try:
    # Create the database the first time.
    connection.creation.create_test_db(verbosity, autoclobber=not interactive) 
except ..: # Look at the error that this will raise when create a database that already exist
    # Test database already created.
    pass 

3) Bound TEST_RUNNER in setting.py to your test runner.

4) Now run your test like this: ./manage.py test

like image 63
mouad Avatar answered Oct 27 '22 06:10

mouad