[Python-Dev] setUpClass and setUpModule in unittest

R. David Murray rdmurray at bitdance.com
Thu Feb 11 20:28:51 CET 2010


On Thu, 11 Feb 2010 16:08:54 +0000, Michael Foord <fuzzyman at voidspace.org.uk> wrote:
> On 11/02/2010 15:56, R. David Murray wrote:
> > On Thu, 11 Feb 2010 12:41:37 +0000, Michael Foord<fuzzyman at voidspace.org.uk>  wrote:
> >> On 11/02/2010 12:30, Nick Coghlan wrote:
> >>> 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()
> >>>        
> >
> > @contextlib.contextmanager
> > def foo_cm(testcase):
> >      testcase.bar = some_costly_setup_function()
> >      yield
> >      testcase.bar.close()
> >
> > @contextlib.contextmanager
> > def foo_test_cm(testcase):
> >      testcase.baz = Mock(testcase.bar)
> >      yield
> >
> >
> > @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")
> >
> >
> This is quite different to what Nick *specifically* suggested. It also 
> doesn't suggest a general approach that would easily allow for 
> setUpModule as well.

I'm not sure how it is different.  I thought I was indicating how to do
the context manager "discovery" that Nick punted on.  (Except for module
level, which I didn't have a good idea for).

> *However*, I am *hoping* to be able to incorporate some or all of Test 
> Resources as a general solution (with simple recipes for the setUpClass 
> and setUpModule cases) - at which point this particular discussion will 
> become moot.

Which pretty much makes my noddling above moot right now, because having
taken a quick look at testresources I think that's a much closer fit for
my use cases than class level setup/teardown.  So I'm +1 for going the
testresources route rather than the setup/teardown route.

--
R. David Murray                                      www.bitdance.com


More information about the Python-Dev mailing list