Magda
Magda

Reputation: 1248

Python Django Unittests: How to tear down fixtures

I need to write some tests for my Django-App. For that I am using fixtures which I load like that:

    from django.test import TestCase    

    class PermissionTest(TestCase):
        fixtures = ['test_groups.json','test_users.json']

        def setUp(self):       
        ... some other stuff

Now I am wondering what to write in my tearDown-Method to delete the groups and users generated from my fixtures. Or are they deleted automatically?

        def tearDown(self):
            ... what has to go here?

Upvotes: 4

Views: 2850

Answers (1)

naiquevin
naiquevin

Reputation: 7796

Deletion of the loaded fixtures will be taken care of by django's testing framework before running each test. So you don't need to handle this.

From the docs:

Here’s specifically what will happen:

At the start of each test case, before setUp() is run, Django will flush the database, returning the database to the state it was in directly after migrate was called.

Then, all the named fixtures are installed. In this example, Django will install any JSON fixture named mammals, followed by any fixture named birds. See the loaddata documentation for more details on defining and installing fixtures.

This flush/load procedure is repeated for each test in the test case, so you can be certain that the outcome of a test will not be affected by another test, or by the order of test execution.

Upvotes: 3

Related Questions