[Python-Dev] setUpClass and setUpModule in unittest

Nick Coghlan ncoghlan at gmail.com
Thu Feb 11 13:30:59 CET 2010


Michael Foord wrote:
> I'm not sure what response I expect from this email, and neither option
> will be implemented without further discussion - possibly at the PyCon
> sprints - but I thought I would make it clear what the possible
> directions are.

I'll repeat what I said in the python-ideas thread [1]: with the advent
of PEP 343 and context managers, I see any further extension of the
JUnit inspired setUp/tearDown nomenclature as an undesirable direction
for Python to take.

Instead, I believe unittest should be adjusted to allow appropriate
definition of context managers that take effect at the level of the test
module, test class and each individual test.

For example, given the following method definitions in unittest.TestCase
for backwards compatibility:

  def __enter__(self):
    self.setUp()

  def __exit__(self, *args):
    self.tearDown()

The test framework might promise to do the following for each test:

  with get_module_cm(test_instance): # However identified
    with get_class_cm(test_instance): # However identified
      with test_instance: # **
        test_instance.test_method()

It would then be up to the design of the module and class context
manager instances to cache any desired common state. Further design work
would also be needed on the underlying API for identifying the module
and class context managers given only the test instance to work with.

The get_*_cm mechanisms would return a no-op CM if there was no specific
CM defined for the supplied TestCase.

Cheers,
Nick.

[1]
http://mail.python.org/pipermail/python-ideas/2010-January/006758.html


-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------


More information about the Python-Dev mailing list