I'm trying to load a json fixture into a Python 2.6/Django 1.4 unit test. I can do it at the test level, using Django's TestCase, but it takes 8-10 seconds to load (2M of json, not going to get much smaller). Running a dozen tests is therefore really slow, and I'd like to load the test database just once. It looks like it might be possible in Python 2.6, with the addition of unittest2
, but I haven't gotten it to work.
# Works, but takes eight seconds per test.
class BaseStuff(django.test.testcases.TestCase):
fixtures = ['test_data']
def setUp(self):
# stuff
def test_one(self):
# stuff
# Doesn't work - but runs *really* fast.
class BaseStuff(unittest2.TestCase):
@classmethod
def setUpClass(cls): # Added in unittest2
fixtures = ['test_data']
print "in setupClass()" # Does print, so function is called.
Trying setupModule() didn't work, either. The setup function is definitely being called, but doesn't seem to be fixture-aware.
So Django's TestCase will read fixtures, and unittest2's TestCase will do things at the class/module level. Is there any way to combine this operation, and read fixtures at the class/module level? I tried multiple inheritance, and couldn't get it to load the data.
Update: Based on @robjohncox's suggestion, this code, at the module level:
from django.core import management
management.call_command('loaddata', 'test_data.json', verbosity=1, noinput=True)
does seem to create a database. But then Django appears to create another database for each test (which is empty). Not sure how to tell the TestCase to use the first db (I tried both the Django and unittest2 TestCase).