Skip to content
Advertisement

Django test uses wrong database in some cases

I try to setup my Django tests, and I noticed that when I run all tests TestRunner uses correct test database (for all aliases):

docker-compose exec my_project python manage.py test --keepdb

JavaScript

But when I run tests for specific module, it uses the original database:

docker-compose exec my_project python manage.py test --keepdb apps.my_module

JavaScript

What is the reason of such behavior? Using the original database is not allowed when I run tests on pre-production stages!

Project configuration:

JavaScript

I would be grateful for any advice!

Advertisement

Answer

I think it is a kind of bug in Django test. When there is no ‘databases’ parameter in any TestCase, it does not use the test database, but uses the original one. To overpass this, I redefined setup_databases method in my TestRunner like this:

JavaScript
Advertisement