[Python-Dev] setUpClass and setUpModule in unittest

Michael Foord fuzzyman at voidspace.org.uk
Fri Feb 12 16:49:02 CET 2010

On 11/02/2010 18:11, Guido van Rossum wrote:
> On Tue, Feb 9, 2010 at 8:42 AM, Michael Foord<fuzzyman at voidspace.org.uk>  wrote:
>> The next 'big' change to unittest will (may?) be the introduction of class
>> and module level setUp and tearDown. This was discussed on Python-ideas and
>> Guido supported them. They can be useful but are also very easy to abuse
>> (too much shared state, monolithic test classes and modules).
> [snip...]
> The potential for abuse in and of itself should not be an argument
> against a feature; it must always be weighed against the advantages.

The advantage of setUpClass and setUpModule is that they allow you to 
have shared fixtures shared between tests, essential for certain kinds 
of testing. A practical difficulty with setUpClass and setUpModule is 
that because the scope of the shared fixtures is fixed it makes it much 
harder to later refactor your tests - either into several classes or 
into several modules - when the tests grow.

My *hope* is that we provide a general solution, possibly based on all 
or part of Test Resources, with an easy mechanism for the setUpClass and 
setUpModule but also solves the more general case of sharing fixtures 
between tests. If that doesn't turn out to be possible then we'll go for 
a straight implementation of setUpClass / setUpModule. I'm hoping I can 
get this together in time for the PyCon sprints...

Here's a current minimal example of using Test Resources. It could be 
simplified further with helper functions and by some of the 
functionality moving into unittest itself. OptimisingTestSuite here 
ensures that the resource is created before first use (MyTempDir.make is 
called) and disposed of when finished with (MyTempDir.clean is called).

import shutil
import tempfile
import testresources

def load_tests(loader, tests, pattern):
# this step could be built into the standard loader
return testresources.OptimisingTestSuite(tests)

class MyTempDir(testresources.TestResource):
def make(self, dependency_resources):
return tempfile.mkdtemp()

def clean(self, resource):

class MyTest(testresources.ResourcedTestCase):
resources = [('workdir', MyTempDir())]
def test_foo(self):
print self.workdir
def test_bar(self):
print self.workdir



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 Python-Dev mailing list