Jay Taylor's notes

back to listing index

Doing what you aren’t supposed to do with Django tests | HeadSpin Blog

[web search]
Original source (blog.headspin.com)
Tags: django python testing blog.headspin.com
Clipped on: 2014-07-24

HeadSpin Blog

You know what the truth is? The truth is something my neighbor believes. If I want to make friends with him I ask him what he believes. He tells me and I say “Yeah, Yeah ain’t it the Truth?”

Doing what you aren’t supposed to do with Django tests

So I wanted to run my django test scripts against my development database. The django test system really wants to create a database from scratch but I wanted to see whether I could run against my local scratch copy since its already got a lot of data in it and I am too lazy to make fixtures etc.

Some googling produced alot of very helpful “You are doing it wrong!” comments which is fine as far as that goes but I am not good at taking advice.

Digging a little deeper I found you can override the DjangoTestSuiteRunner. Now we are getting somewhere.

put this in settings.py:


TEST_RUNNER = 'myapp.test_extras.NoTestDbDatabaseTestRunner'

And then define the NoTestDbDatabaseTestRunner:


from django.test.simple import DjangoTestSuiteRunner
class NoTestDbDatabaseTestRunner(DjangoTestSuiteRunner):
    def setup_databases(self, **kwargs):
        pass
    def teardown_databases(self, old_config, **kwargs):
        pass


I’m feeling pretty good about this. I try a simple “test” which just counts the number of items in a particular table.

Only what I get is a completely wiped out local db. Nice.

The trick is that my TestCase was using


from django.test import TestCase

which has its own ideas about what to do with the database and it was wiping it out. Switching to:


unittest.TestCase


or


from django.test import SimpleTestCase


if you are using django trunk. Looks like it fixes the problem, but it doesn’t make me feel any better.

Maybe I should just listen more.

This entry was posted in Uncategorized on September 7, 2011 by .

Leave a Reply

Your email address will not be published. Required fields are marked *

Name *

Email *

Website

Comment

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Notify me of follow-up comments by email.

Notify me of new posts by email.

Image (Asset 1/1) alt=