[Python-Dev] setUpClass and setUpModule in unittest

Tres Seaver tseaver at palladion.com
Thu Feb 11 18:12:37 CET 2010


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

exarkun at twistedmatrix.com wrote:
> On 04:18 pm, tseaver at palladion.com wrote:
>> Just as a point of reference:  zope.testing[1] has a "layer" feature
>> which is used to support this usecase:  a layer is a class namedd as an
>> attribute of a testcase, e.g.:
>>
>>  class FunctionalLayer:
>>     @classmethod
>>     def setUp(klass):
>>         """ Do some expesnive shared setup.
>>         """
>>     @classmethod
>>     def tearDown(klass):
>>         """ Undo the expensive setup.
>>         """
>>
>>  class MyTest(unittest.TestCase):
>>      layer = FunctionalLayer
>>
>> The zope.testing testrunner groups testcase classes together by layer:
>> each layer's setUp is called, then the testcases for that layer are 
>> run,
>> then the layer's tearDown is called.
>>
>> Other features:
>>
>> - - Layer classes can define per-testcase-method 'testSetUp' and
>> 'testTearDown' methods.
>>
>> - - Layers can be composed via inheritance, and don't need to call base
>>  layers' methods directly:  the testrunner does that for them.
>>
>> These features has been in heavy use for about 3 1/2 years with a lot 
>> of
>> success.
>>
>>
>> [1] http://pypi.python.org/pypi/zope.testing/
> 
> On the other hand:
> 
>   http://code.mumak.net/2009/09/layers-are-terrible.html
> 
> I've never used layers myself, so I won't personally weigh in for or 
> against.

I don't know the author of that post as a core Zope developer:  the fact
is that using inheritance to manage the layers works just fine for
Zope's thousands of functional tests.

As for his objections:  if you don't want the superclass methods called,
then don't make your layer inherit from it (why else would you?).
Sharing setup across test methods is the whole point of layers, or of
the other mechanisms being discussed here:  while I agree that such
tests aren't "unit tests" in the classic sense, they do have their place.



Tres.
- --
===================================================================
Tres Seaver          +1 540-429-0999          tseaver at palladion.com
Palladion Software   "Excellence by Design"    http://palladion.com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkt0OoAACgkQ+gerLs4ltQ5YSACeLzR+LfkafGB3GLWMgMPvdiPc
8nEAoKuudwJMznZiyrmJD1SHcOkYw3cr
=6VG8
-----END PGP SIGNATURE-----



More information about the Python-Dev mailing list