This error was the result of several problems. I'll summarize them here to help others who may have stumbled across this.
Ensure your settings.DATABASES is set correctly. Django's docs mention using
TEST_NAME
, but for clarity, I find it easier to check for thetest
command and override everything. e.g. at the bottom of my settings.py, I have:if 'test' in sys.argv: DATABASES = { 'default':{ 'ENGINE': 'django.db.backends.sqlite3', 'NAME': ':memory:', }, }
Unless you have a good reason, always use
:memory:
to ensure it runs in memory and doesn't create a physical file that will be bogged down on disk. For some odd reason, a lot of other answers on SO recommend specifying a literal path to a test.db file for testing. This is horrible advice.Unless you want to test South and/or your South migrations, disable South, because it'll only complicate things:
SOUTH_TESTS_MIGRATE = False SKIP_SOUTH_TESTS = True
Don't be dumb like me and try to access your models before they're created. This mostly means don't directly refer to models from the fields of other models or forms. e.g.
class MyForm(forms.Form): somefield = forms.ChoiceField( required=True, choices=[(_.id, _.name) for _ in OtherModel.objects.filter(criteria=blah)], )
This might work in code where your database already exists, but it'll break Django's unittest framework when it tries to load your tests, which load your models.py and forms.py, causing it to read a table that doesn't exist. Instead, set the choices value in the form's
__init__()
.