Fwd: Please consider adding context manager versions of setUp/tearDown to unittest.TestCase

Folks, this has come up before, but: please don't post through Google Groups, as it breaks everyone else's ability to easily reply to the entire mailing list. ---------- Forwarded message ---------- From: Nick Coghlan <ncoghlan@gmail.com> Date: 22 August 2017 at 15:32 Subject: Re: [Python-ideas] Please consider adding context manager versions of setUp/tearDown to unittest.TestCase To: Neil Girdhar <mistersheik@gmail.com> Cc: python-ideas <python-ideas@googlegroups.com> On 21 August 2017 at 11:32, Neil Girdhar <mistersheik@gmail.com> wrote:
Using context managers when you can't use a with statement is one of the main use cases for contextlib.ExitStack(): def setUp(self): self._resource_stack = stack = contextlib.ExitStack() self._resource = stack.enter_context(MyResource()) def tearDown(self): self._resource_stack.close() I posted that as an additional answer to the question: https://stackoverflow.com/questions/8416208/in-python-is-there-a-good-idiom-... Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia

On 22 August 2017 at 15:34, Nick Coghlan <ncoghlan@gmail.com> wrote:
Sjoerd pointed out off-list that this doesn't cover the case where you're acquiring multiple resources and one of the later acquisitions fails, so I added the ExitStack idiom that covers that case (using stack.pop_all() as the last operation in a with statement): def setUp(self): with contextlib.ExitStack() as stack: self._resource1 = stack.enter_context(GetResource()) self._resource2 = stack.enter_context(GetOtherResource()) # Failures before here -> immediate cleanup self.addCleanup(stack.pop_all().close) # Now cleanup won't happen until the cleanup functions run I also remember that using addCleanup lets you avoid defining tearDown entirely. Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia

** Caution: cranky curmudgeonly opinionated comment ahead: ** unitest is such an ugly Java-esque static mess of an API that there's really no point in trying to clean it up and make it more pythonic -- go off and use pytest and be happier. -CHB On Tue, Aug 22, 2017 at 5:42 AM, Nick Coghlan <ncoghlan@gmail.com> wrote:
-- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker@noaa.gov

On Tue, Aug 22, 2017 at 12:34 AM, Nick Coghlan <ncoghlan@gmail.com> wrote:
Mentioning this is probably going to do nothing, especially for new, future users. Can you block python-ideas@googlegroups.com (or if it's CC'd or whatever) from posting if you just want the Groups page to be a read-only thing?

On 22 August 2017 at 15:34, Nick Coghlan <ncoghlan@gmail.com> wrote:
Sjoerd pointed out off-list that this doesn't cover the case where you're acquiring multiple resources and one of the later acquisitions fails, so I added the ExitStack idiom that covers that case (using stack.pop_all() as the last operation in a with statement): def setUp(self): with contextlib.ExitStack() as stack: self._resource1 = stack.enter_context(GetResource()) self._resource2 = stack.enter_context(GetOtherResource()) # Failures before here -> immediate cleanup self.addCleanup(stack.pop_all().close) # Now cleanup won't happen until the cleanup functions run I also remember that using addCleanup lets you avoid defining tearDown entirely. Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia

** Caution: cranky curmudgeonly opinionated comment ahead: ** unitest is such an ugly Java-esque static mess of an API that there's really no point in trying to clean it up and make it more pythonic -- go off and use pytest and be happier. -CHB On Tue, Aug 22, 2017 at 5:42 AM, Nick Coghlan <ncoghlan@gmail.com> wrote:
-- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker@noaa.gov

On Tue, Aug 22, 2017 at 12:34 AM, Nick Coghlan <ncoghlan@gmail.com> wrote:
Mentioning this is probably going to do nothing, especially for new, future users. Can you block python-ideas@googlegroups.com (or if it's CC'd or whatever) from posting if you just want the Groups page to be a read-only thing?
participants (3)
-
Chris Barker
-
Nick Coghlan
-
Nick Timkovich