Login

Unit Tests That Write Fixtures

Author:
justquick
Posted:
February 4, 2009
Language:
Python
Version:
1.0
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. Template tag - list punctuation for a list of items by shapiromatron 2 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
  5. Help text hyperlinks by sa2812 11 months ago

Comments

Please login first before commenting.