Django Unit Testing and South Migrations
In order to test a Django app I usually build objects for my models from the admin and then dump and load them as fixtures in my test classes, as explained in the docs about unit testing:
class MyAppTest(TestCase): fixtures = ['fixtures/test-myapp-fixtures.json', 'fixtures/test-anotherapp-fixtures.json'] def setUp(self): ... def test_something_in_my_app_test(self): ...
I recently added "blank=True, null=True" to one of my fields and created a south migration to apply the change. On my regular database, after running the migration, it worked without problems. But after recreating the test fixture and trying to run my test, I got an error that the field could not be null:
IntegrityError: Problem installing fixture 'fixtures/test-myapp-fixtures.json': Could not load myapp.Order(pk=179): myapp_order.country may not be NULL
After trying for a little bit to figure out what was going wrong with South, I found out that using syncdb for your tests, rather than the migrations, is really what I want most of the time. To do that, just add the following variable to your project settings.py:
SOUTH_TESTS_MIGRATE = False
There's more information on the boolean in the South docs: http://south.readthedocs.org/en/latest/settings.html#south-tests-migrate
Of course, you'll also have to remember to dump your test fixtures again any time you change your model structure/db schemas.