Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 641631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T21:05:03+00:00 2026-05-13T21:05:03+00:00

If you’ve got a database setup in Django, how can you have the TestRunner

  • 0

If you’ve got a database setup in Django, how can you have the TestRunner use the ‘live’ database (per the DATABASE_* settings in settings.py) instead of running them on the ephemeral test database.

For example, I’d like to run the following test on the live database that’s specified in settings.py:

import unittest

from example import models

class DBDriverTest(unittest.TestCase): 
    db testDriver(self):
       "Connect to the live database and drop in sample value."
       m = models.MyModel('hello')
       m.save() # ... save to the live database from settings.py

At present the above code will save only to the test database that’s been constructed. This is very limiting because my application has multiple parallel processes working on the database – and my unit tests will be incomplete (and incoherent) without the ability to pump things over to a ‘live’ database and see where they stand after a short sleep.

The two potential options I can think of are:

  1. Plug through the Django API to figure out how to ‘manually’ connect to the database in settings.py

  2. Make a low-level connection to the ‘live’ database and manually populate it

The prior is problematic because it’d be relying on things beneath Django’s public API. The latter is problematic because it foregoes the database-agnostic Django database API and is more manually intensive.

I’m grateful for your thoughts and input.

Brian

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T21:05:03+00:00Added an answer on May 13, 2026 at 9:05 pm

    You would have to define your own test_runner tearDown methods, because every test is run in isolation and database is cleared after every run. You can do, what you are asking for by simply building your own test_runner, we have done it once (though it wasn’t me on the team) with database that was accessed by web services and for some time we didn’t have any way to delete anything from there but to manually delete the whole database 😉 This was pretty fun.

    Answering your question: create your own test_runner and prepare to create your own tearDown methods, in which you will only delete objects, that you created. You must somehow store there primary keys, so you won’t delete anything from the live database.

    However, I don’t think this is a good way to do. Running tests on live database is asking yourself for a disaster. Sooner or later you’ll get into trouble with this. What you should do instead is to dump your live database, prepare fixtures from this data and use them in your tests. This is well documented and easily done. This way you will have live environment without risking your precious data and you don’t need to write your own test_runner. This is the best and safest way to go in my opinion.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.