0

I am currently writing test cases for views, Which eventually uses database also. By default a test database is being created and removed after test are run.

As the database itself is development database, I don't want my test to create a separate db but use exciting only. Also I will like to bring to your notice, that in my environment, database are created and provided and django can't run migration or create database.

How can I create unittest which uses real database ?

user
  • 2,694
  • 6
  • 24
  • 25

1 Answers1

0

I think the primary reason for this question is because your django database user is not provided with the create/drop database permission.

Django needs to create and drop a test database for the purpose of unit testing. It cannot use an existing database for this purpose. Why we are not allowed to use an existing database in the unit test is because, the data can be modified by anyone who has the same database permission and django may not have control over the updates they make, This might end up in an unsuccessful unit test.

This is clearly explained in another question's answer

If your DB Admin can provide your Django user the required access for the Test module to work as expected, You can make use of the Fixtures. Fixtures are like data files, can be created from your development environment and then can be used in the Unit test Setup to import the data from Fixtures to the test database created by Django.

The ultimate purpose of any Unit test framework will be to test the functionality of the Back end code logic with a data which is not likely to change. As mentioned in the above links, The Functional testing and Regression Testing is there to cover the real database.

For more details on Fixtures visit Using Fixtures with Django Test Cases