[TIP] Current unittest2 plugins: including test generators

Michael Foord michael at voidspace.org.uk
Fri Aug 6 07:39:23 PDT 2010

Hello all,

I'm posting this in a new thread in the hopes that I can tempt a few 
more of you to try out the unittest2 plugins. For instructions on 
getting the unittest2 plugins branch code see:


The latest addition to the unittest2 plugins is support for test 
generators, either as functions or TestCase methods. Enable the 
"generators" plugin from the command line or by setting 'always-on' in 
the config file. You can then use test generators (the tests are loaded 
from the generator at test load time):

. from unittest2.plugins.moduleloading import testGenerator

. class TestFoo(unittest2.TestCase):
. def _foo(self, a, b):
. self.assertEqual(a, b)

. @testGenerator
. def test_a(self):
. yield self._foo, (1, 1)
. yield self._foo, (3, 3)
. yield self._foo, (4, 2)

Test functions may also be generators using the same testGenerator 
decorator. There are also setUp and tearDown decorators (that take a 
function) in the moduleloading plugin, for use by test functions.

Generated tests from TestCase methods will share a test instance, but 
setUp and tearDown will be correctly called around each generated test.

The other plugins provided by the unittest2 branch (currently) are:

* a pep8 and pyflakes checker
* a debugger plugin that drops you into pdb on test fail / error
* a doctest loader (looks for doctests in all text files in the project)
* use a regex for matching files in test discovery instead of a glob
* growl notifications on test run start and stop
* filter individual test methods using a regex
* load test functions from modules as well as TestCases
* test generators
* integration with the coverage module for coverage reporting
* display the time of individual tests in verbose reports
* display a progress indicator as tests are run ([39/430] format) in 
verbose reports
* allow arbitrary channels for messaging instead of just the three 
verbosity levels

unittest.cfg in the plugins branch shows you all the configuration 
options for these plugins and `unit2 --help` will show you all the 
command line options.

A junit-xml plugin is "in progress" and I'll also be looking at a simple 
distributed test runner.

All the best,

Michael Foord


READ CAREFULLY. By accepting and reading this email you agree, on behalf of your employer, to release me from all obligations and waivers arising from any and all NON-NEGOTIATED agreements, licenses, terms-of-service, shrinkwrap, clickwrap, browsewrap, confidentiality, non-disclosure, non-compete and acceptable use policies (”BOGUS AGREEMENTS”) that I have entered into with your employer, its partners, licensors, agents and assigns, in perpetuity, without prejudice to my ongoing rights and privileges. You further represent that you have the authority to release me from any BOGUS AGREEMENTS on behalf of your employer.

More information about the testing-in-python mailing list