views:

381

answers:

2

I'm working on a web project in Django, and I'm using the python unittest framework. For every app I have some fixtures. This means, that every app has some identical tables in fixtures. I would like to share fixtures between apps and testcases, because otherwise if I change a model, I will have to change all json fixtures where this concrete table is referenced.

Is it sensible to use global fixtures?

+2  A: 

ToM,

I would highly recommend looking into Django's Testing architecture. Check out TestCase.fixtures especially; this is far more advanced and Django-specific than unittest.

Vince
Thank you for answer, I use Django TestCase. My question wasnt about using Django testcase, but about reusing fixtures. If somebody have bad experience with it.
The fixtures property follows the same loading rules as loaddata: http://docs.djangoproject.com/en/dev/ref/django-admin/#loaddata-fixture-fixture so you can put your common fixture file in one app and load it from all test cases.
dar
A: 

I can't think of anything wrong with using global fixtures as long as you delete them in your tearDown method (or teardown_test_environment method - see below).

I am not sure if you are asking to find out how to do this. If so there are two ways that I can think of.

1) Use a common base class for all your tests. Something like this:

class TestBase(django.test.TestCase):
    fixtures = ['common_fixtures.xml']

class MyTestClass(TestBase):
    fixtures = TestBase.fixtures + ['fixtures_for_this_test.xml']

    def test_foo(self):
        # test stuff

2) Use a custom test runner. In your test runner load all the fixtures you need before running the tests and take them down after executing the tests. You should preferably do this by using your own setup_ and teardown_test_environment methods.

Manoj Govindan
What's a JSON fixture for this look like? I can't get it to find the app other than the local one
Thomas Schultz