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. Month / Year SelectDateWidget based on django SelectDateWidget by pierreben 3 months ago
  2. Python Django CRUD Example Tutorial by tuts_station 3 months, 2 weeks ago
  3. Browser-native date input field by kytta 5 months ago
  4. Generate and render HTML Table by LLyaudet 5 months, 1 week ago
  5. My firs Snippets by GutemaG 5 months, 1 week ago

Comments

Please login first before commenting.