4

When running tests, you can do:

./manage.py test --keepdb

To run your tests, and keep the test database. Is it possible to have the django shell actually connect to it, so we can interactively access the test database the same way the Django shell can normally work with the production database?

Note that the answer and its comments here imply that you can access it by doing something like:

from django import test
test.utils.setup_test_environment()
from django.db import connection
db = connection.creation.create_test_db(keepdb=True)

But when I do that, my database appears to be empty when I do queries.

John
  • 2,551
  • 3
  • 30
  • 55
  • Are you sure the test database has any rows in it? – Iain Shelvington Apr 06 '20 at 22:48
  • 1
    My setUpTestData() classmethod creates new instances of my models, saving them to the test database, which I later use in my test methods. Since I use the `--keepdb` arg, I would assume it would survive, but connecting to my MySQL database directly, my tables are empty in the `test_` database. Am I doing something wrong? – John Apr 07 '20 at 00:52

1 Answers1

0

I ran into this, at first I thought it was because the codebase I'm working on has a flush call in the teardown function, but my DB was still empty after removing those. Maybe there were more flushes somewhere I didn't catch.

I ended up getting around this by sleeping at the end of the test, so it doesn't exit and doesn't clean up.

rtaft
  • 2,139
  • 1
  • 16
  • 32
  • I have the opposite problem. I *want* the empty test database, then I want to run one of the test files' setup commands that loads data, but when I do that, it complains about conflicts with existing data... – hepcat72 May 03 '22 at 15:56