Skip to content

Conversation

@PetterS
Copy link
Contributor

@PetterS PetterS commented Mar 10, 2018

This allows a future subclass to customize how the test method is run. For
example, this is required in order to create an "AsyncTestCase" that runs
coroutines in an event loop.

This pull request only provides the neccessary change in unittest. Any future class like "AsyncTestCase" will probably live in asyncio.

https://bugs.python.org/issue32972

This allows a future subclass to customize how the test method is run. For example, this is required in order to create an "AsyncTestCase" that runs coroutines in an event loop.
msg = self._formatMessage(msg, standardMsg)
raise self.failureException(msg)

def _runTestMethod(self, method):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is documented as something that can be overloaded in subclasses, I think it should be a public API.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

Should it be publicly documented (in unittest.rst) as well?

@python python deleted a comment from smartdevoloper Mar 10, 2018
@asvetlov
Copy link
Contributor

Sure, the runTestMethod should be documented.
I quite unhappy with runTestMethod name. doTest maybe.
Moreover thinking on adding async support I see the patch is not complete: we need overridable methods for setUp()/tearDown() calls. Keep in mind that tear down is actually a bundle of self.tearDown() and running cleanups added by self.addCleanup(...).
That's why I would like to have three overridables: doTest(), doSetUp and doTearDown (or doCleanup.
Does it make sense?

Another question is FunctionTestCase. Should we support async tests for the class?
I think "no" is an easy and satisfactory answer but maybe somebody disagree.

P.S.
@PetterS thank you for moving our discussion into pull request, it's very hard to get the whole vision without looking on concrete code change proposals.

@asvetlov asvetlov changed the title bpo-32972: Unittest: Add a _runTestMethod to TestCase bpo-32972: Unittest: Add a _runTestMethod to TestCase (don't merge) Mar 11, 2018
@asvetlov
Copy link
Contributor

Added don't merge to title, I have a feeling that we should careful discuss my questions (and I expect a raising new ones during discussion).

@PetterS
Copy link
Contributor Author

PetterS commented Mar 11, 2018

I am not sure we need doSetUp and doTearDown . I got the impression from the discussion that setUp and tearDown should never need to be async. Thus, only a overridable test method runner would be needed.

As for the name, I have no opinion either way. doTest sounds good to me. Happy to change it unless anyone objects.

Copy link
Member

@1st1 1st1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR looks good, but there's still an open issue: setUp and tearDown shouldn't be async, but we still need a way to have their asyncTearDown and asyncSetUp equivalents.

@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@PetterS
Copy link
Contributor Author

PetterS commented Mar 12, 2018

Since setUp and tearDown shouldn't be async, I am nore sure that there is much left to do in TestCase at this point. I was thinking that asyncSetup and asyncTearDown can be implemented in the subclass using setUp and tearDown without any special support in TestCase. That way, we don't have to increase the public API of TestCase that much.

I believe that we can implement an AsyncTestCase with what we have in this PR.

@asvetlov
Copy link
Contributor

People assume that setUp() / tearDown() do nothing in default implementation.
It means overridden MyTest.setUp() doesn't call super().setUp() very often.
If AsyncioTestCase will override these methods to call asyncSetUp()/asyncDearDown() this changes game rules: MyTest.setUp() should call parent method.

That's why I propose an alternative: let's support doSetUp and doTearDown methods. By default doSetUp calls self.setUp(), doTearDown calls self.tearDown(). AsyncioTestCase can add custom calls to asyncSetUp() / asyncTearDown() without changing setUp()/tearDown() expectations.

@PetterS
Copy link
Contributor Author

PetterS commented Mar 12, 2018

Yes, that would work, but having both setUp and doSetUp defined and documented for TestCase is not a very nice interface.

Better to put as much of the new logic as possible in the new class. (IMO)

@asvetlov
Copy link
Contributor

In my mind all three do* methods should be documented as internals.
It means they should be used by new test case classes creators (e.g AsyncioTestCase) but not final tests (e.g. class TestMyFancyStuff(AsyncioTestCase):).

Otherwise we have an overload for running test itself only but setup/teardown is rigidly pinned.

See these lines from ./Lib/unittest/case.py:

 with outcome.testPartExecutor(self): self.setUp() if outcome.success: outcome.expecting_failure = expecting_failure with outcome.testPartExecutor(self, isTest=True): testMethod() outcome.expecting_failure = False with outcome.testPartExecutor(self): self.tearDown() self.doCleanups() 

The PR customizes testMethod() execution only. Why setup/teardown is not customized as well?

@PetterS
Copy link
Contributor Author

PetterS commented Mar 12, 2018

@1st1, you requested changes. Is that what you had in mind? Personally, I am not sure that there is much left to do in TestCase at this point.

@1st1
Copy link
Member

1st1 commented Mar 12, 2018

@asvetlov @PetterS Please let's try to keep the discussion about the API in the issue.

@asvetlov
Copy link
Contributor

That's what I'm talking about: we need three hooks: set-up, run-test and tear-down.

@PetterS PetterS changed the title bpo-32972: Unittest: Add a _runTestMethod to TestCase (don't merge) bpo-32972: Unittest: Add a runTestMethod to TestCase (don't merge) Oct 30, 2018
@PetterS PetterS closed this Apr 14, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

5 participants