Login

Unit Tests That Write Fixtures

Author:
justquick
Posted:
February 4, 2009
Language:
Python
Version:
1.0
Tags:
json unittest fixture
Score:
1 (after 1 ratings)

This is a skeleton framework of a unittest for an app which will write out a fixture of the test database once the test has been done. I run this once for all apps, but you can limit which apps get serialized by modifying the self.apps value from get_apps (all apps) to a list of only certain apps. This script by default assumes that you have a SVN_DIR setting which points to the current working subversion directory, with a subdirectory of fixtures where it places tests.json upon completion. You may change this location as well. After running python manage test you can run python manage loaddata fixtures/tests.json to load in to the real database all of the test database fixtures. Feel free to edit at will, let me know of any changes that are helpful, and dont forget to fill in the ...s

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import unittest
import os
from django.core import serializers
from django.db.models import get_apps
from django.conf import settings

from app.models import ...

class MyTestCase(unittest.TestCase):
    def setUp(self):
        self.apps = get_apps()
    
    def test...(self):
        ...
        
    def tearDown(self):
        objects = []
        for app in self.apps:
            for model in get_models(app):
                objects.extend(model._default_manager.all())

        out = open(os.path.join(settings.SVN_DIR,'fixtures','tests.json'), 'w')
        out.write(serializers.serialize('json',objects))
        out.close()

More like this

  1. Fail Faster: unsafe_test Management Command by majgis 2 years, 11 months ago
  2. UnitTesting without create/destroy database by crucialfelix 6 years, 5 months ago
  3. Unit Test Profiling by justquick 6 years, 5 months ago
  4. Unittest: Compare max_size with column size of database by guettli 6 years, 11 months ago
  5. change settings locally in an individual test by akaihola 4 years, 11 months ago

Comments

Please login first before commenting.