Skip to content

Instantly share code, notes, and snippets.

@mikeywaites
Last active December 18, 2015 10:59
Show Gist options
  • Save mikeywaites/5772578 to your computer and use it in GitHub Desktop.
Save mikeywaites/5772578 to your computer and use it in GitHub Desktop.
load django fixture sets on a test by test basis
"""
Decorate a method inside of a django TestCase test runner instance to allow you to load fixtures on a per test basis
Useage::
from tests.decorators import load_fixtures
MyTestCase(TestCase):
@load_fixtures(['fixtures.json'])
def test_load_fixtures(self):
self.assertTrue()
...
"""
from functools import wraps
from django.utils.decorators import available_attrs
def load_fixtures(fixtures):
def decorator(test_fn):
def _wrapped(test_case, *args, **kwargs):
test_case.fixtures = fixtures
test_case._fixture_setup()
test_case.fixtures = []
return test_fn(test_case, *args, **kwargs)
return wraps(test_fn, assigned=available_attrs(test_fn))(_wrapped)
return decorator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment