Skip to content
Tomas Divis edited this page Sep 27, 2011 · 3 revisions

Changes to upstream original version (Almad)

My changes are mainly focused on speed of tests, trying to minimize unnecessary database creation and database flushes, while preserving test isolation, but also possibility for a group of test (test case, module, package) to be designed as not isolated if author of tests wishes it.

Lazy database creation

Test database is created only if needed (i.e. if you run only UnitTestCase the database is not created at all) and when needed (means just before first test which needs database is run).

Flushing only after test

Philosophy is that each test should clean up after itself, so having tests with database_flush = True, they flush database only AFTER test is run (not before and after as in original version), that is possible because at start of each test, the database is always clean.

Possibility to override type of test within test case

Sometimes you have a group of test in test case and for example only one performs commit. That means you need to use DestructiveDatabaseTestCase which also means that all tests in test case will flush database causing several unnecessary database flushes.

That's why I added possibility to override test type within test case, example:


class MyTest(DestructiveDatabaseTestCase):
    def test_dbflush(self):
        assert True == True

    def test_nodb(self):
        assert True == True
    test_nodb.no_database_interaction = True

Here database flush is done only after test_dbflush and test_nodb don't use database at all. You can also use attr decorator from nosetests:


from nose.plugins.attrib import attr

    ...
    @attr('no_database_interaction')
    def test_nodb(self):
        assert True == True

Non-isolated tests

Sometimes, your tests are written in a way, that they write to database and even use commits, but they don't need to be isolated on database level (so data from the first test don't need to be deleted for the second test). In this case, you can avoid lots of unnecessary rollbacks or flushes by using these test cases: NonIsolatedDatabaseTestCase or NonIsolatedDestructiveDatabaseTestCase, which do only one cleanup operation after the test case is finished.

You can achieve similar thing for whole module or package simply by adding database_single_transaction = True or database_flush = True to the module/package. That means the cleanup operation is done only once after all tests in module/package are run (assuming you use only UnitTestCase for cases you don't need database and NoCleanupDatabaseTestCase for cases that use database (so the test database is created)).