[Python-Dev] setUpClass and setUpModule in unittest

Antoine Pitrou solipsis at pitrou.net
Thu Feb 11 19:58:11 CET 2010


Le Thu, 11 Feb 2010 10:56:32 -0500, R. David Murray a écrit :
> 
> @unittest.case_context(foo_cm)
> @unittest.test_context(foo_test_cm)
> class TestFoo(unittest.TestCase):
> 
>     def test_bar:
>         foo = Foo(self.baz, testing=True)
>         self.assertTrue("Context managers are cool")
> 
> would be easier to write, be more maintainable, and be easier to
> understand when reading the code than the equivalent setUp and tearDown
> methods would be.

I don't think it would be seriously easier to write, more maintainable or 
easier to understand. There's nothing complicated or obscure in setUp and 
tearDown methods (the only annoying thing being PEP8 non-compliance).

As a matter of fact, nose has a "with_setup()" decorator which allows to 
avoid writing setUp/tearDown methods. But in my experience it's more 
annoying to use because:
- you have to add the decorator explicitly (setUp/tearDown is always
  invoked)
- you have to create your own recipient for local state (setUp/tearDown
  can simply use the TestCase instance), or use global variables which 
  is ugly.

Regards

Antoine.



More information about the Python-Dev mailing list