Edgewall Software

Version 7 (modified by Jun Omae, 9 years ago) ( diff )

Note for setUpClass and tearDownClass

Writing unit tests

This wiki page shows how to write your own unit tests for Trac or Trac plugin.

Start writing TestCases

Any unit test should derive from class unittest.TestCase in order to be called from unittest.makeSuite. Thus you can make a set of test cases, which are called test suite.

Usually you need to setup your Environment in order to have any access to your Trac environment. This can be done using EnvironmentStub.

This test suite can be executed as a normal python code.

Here's a simple Test case (from trac plugin LogWatcherPlugin) to explain the above:

# -*- coding: utf-8 -*-

import os
import shutil
import tempfile
import unittest

from trac.test import EnvironmentStub

from logwatcher import api

class TestApi(unittest.TestCase):

    def setUp(self):
        self.env = EnvironmentStub(enable=[
            'trac.*', 'logwatcher.api'
        self.env.path = tempfile.mkdtemp()
        os.mkdir('%s/log' % self.env.path)

        self.env.config.set('logging', 'log_type', 'file')

    def tearDown(self):

    def test_get_logfile_name(self):
        test_api = api.LogViewerApi(self.env)
        logfile_name = test_api.get_logfile_name()
        self.assertNotEqual(None, logfile_name, "log file name is None")

def suite():
    print "Starting **API** test suite"
    suite = unittest.TestSuite()
    suite.addTest(unittest.makeSuite(TestApi, 'test'))
    return suite

if __name__ == '__main__':

Using several test methods

You can use several test methods within a TestCase class; they need to start with test. When you use method setUp it will be executed for each test method. If you only want to "set up" your test environment once of your test case, you need to use class method setUpClass.

Note that setUpClass and tearDownClass are available since Python 2.7.

For example:

# ... imports and such ...
class TestApi(unittest.TestCase):

    def setUpClass(self, port=None):
        super(TestApi, self).setUpClass()
        self.env = EnvironmentStub(enable=[
            'trac.*', 'logwatcher.api'
        # ... more lines ...

    def test_get_logfile_name(self):
        # ... more lines ...

    def test_get_log_settings_default(self):
        # ... more lines ...

    def tearDownClass(self):
        super(TestApi, self).tearDownClass()
        # you should also shutdown environment before deleting directory,
        # since it closes log files, shutdowns database connection and such
Note: See TracWiki for help on using the wiki.