Login

Zope testing django layer

Author:
grahamcarlyle
Posted:
January 25, 2008
Language:
Python
Version:
.96
Score:
0 (after 0 ratings)

zope.testing is a test framework and test runner, similar to the django test runner and nose.

This snippet is a Layer class which you can assign as a layer attribute of your test suite to initialise and clean up the django test environment appropriately and to reset the test database between tests.

for example:

tests/suite.py

import unittest

from zope.testing import doctest

def test_suite():
    suite = doctest.DocFileSuite("models.txt")
    suite.layer = DjangoLayer
return suite

runtests.py

import os
from zope.testing import testrunner

os.environ['DJANGO_SETTINGS_MODULE'] = 'project.settings'
defaults = [
    '--path', 'tests',
    '--tests-pattern', '^suite$',
    '-c'
]
testrunner.run(defaults)
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
class DjangoLayer(object):

    @classmethod
    def setUp(self):
        from django.conf import settings
        self.pre_existing_db_name = settings.DATABASE_NAME
        from django.test import utils
        utils.setup_test_environment()
        utils.create_test_db(verbosity=0, autoclobber=True)

    @classmethod
    def tearDown(self):
        from django.test import utils
        utils.destroy_test_db(self.pre_existing_db_name, verbosity=0)
        utils.teardown_test_environment()
    
    @classmethod
    def testTearDown(self):
        from django.core import management
        management.call_command('flush', verbosity=0, interactive=False)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 3 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 3 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 10 months, 1 week ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 11 months ago
  5. Help text hyperlinks by sa2812 11 months, 4 weeks ago

Comments

Please login first before commenting.