Skip to content

Instantly share code, notes, and snippets.

@magopian
Created November 16, 2012 11:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save magopian/4086449 to your computer and use it in GitHub Desktop.
Save magopian/4086449 to your computer and use it in GitHub Desktop.
Unit test to validate fixtures
class FixturesTest(TestCase):
def test_num_fixtures(self):
"""All fixtures on the project are tested"""
apps = [app.replace('.', '/')
for app in settings.INSTALLED_APPS if app.startswith('rh2')]
fixtures = []
for app in apps:
fixture_dir = path.join(path.dirname(settings.PROJECT_ROOT),
app,
'fixtures')
fixtures += glob.glob(fixture_dir + '/*.json')
# If this test fails, it's because a fixture was added but the number
# of tests here wasn't changed.
# While you're at it, please add a test for the new fixture.
self.assertEqual(len(fixtures), 7, "Did you add or remove a fixture "
"without testing it and modifying "
"this test?")
def _load_fixture(self, app_name, fixture_name):
"""Load a fixture and return the error message if any"""
stderr = StringIO()
fixture = path.join('apps', app_name, 'fixtures', fixture_name)
call_command('loaddata',
fixture,
verbosity=0,
interactive=False,
stderr=stderr)
return stderr.getvalue()
def test_load_fixtures(self):
self.assertEqual(self._load_fixture('some_app', 'somefixture.json'), '')
self.assertEqual(self._load_fixture('other_app', 'otherfixture.json'), '')
# add a test for each fixture
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment