I have a full-on-slow django-test-db kit and a crazy test run against production test suite created from a common test module. I use the production package to test the health of my changes during development and as a step of checking commit on my development machine. The django suite module is as follows:
import django.test import my_test_module ... class MyTests(django.test.TestCase): def test_XXX(self): my_test_module.XXX(self)
The test suite module uses the bare unittest and looks like this:
import unittest import my_test_module class MyTests(unittest.TestCase): def test_XXX(self): my_test_module.XXX(self) suite = unittest.TestLoader().loadTestsFromTestCase(MyTests) unittest.TextTestRunner(verbosity=2).run(suite)
The testing module is as follows:
def XXX(testcase): testcase.assertEquals('foo', 'bar')
I run a bare version of unittest similar to this one, so my tests anyway have ORM django available to them:
% python manage.py shell < run_unit_tests
where run_unit_tests consists of:
import path.to.production_module
The production module needs slightly different setUp () and tearDown () settings from the django version, and you can put any necessary table cleanup there. I also use the django client test in a common test module, simulating a test client class:
class FakeDict(dict): """ class that wraps dict and provides a getlist member used by the django view request unpacking code, used when passing in a FakeRequest (see below), only needed for those api entrypoints that have list parameters """ def getlist(self, name): return [x for x in self.get(name)] class FakeRequest(object): """ an object mimicing the django request object passed in to views so we can test the api entrypoints from the developer unit test framework """ user = get_test_user() GET={} POST={}
Here is an example of a test module function that the client checks:
def XXX(testcase): if getattr(testcase, 'client', None) is None: req_dict = FakeDict() else: req_dict = {} req_dict['param'] = 'value' if getattr(testcase, 'client', None) is None: fake_req = FakeRequest() fake_req.POST = req_dict resp = view_function_to_test(fake_req) else: resp = testcase.client.post('/path/to/function_to_test/', req_dict) ...
I found that this structure works very well, and the super-fast production version of the package is an important time saver.