Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Asyncio test case #13228

Closed
wants to merge 11 commits into from
Closed

Asyncio test case #13228

wants to merge 11 commits into from

Conversation

asvetlov
Copy link
Contributor

@asvetlov asvetlov commented May 9, 2019

A draft implementation for asyncio test case

@asvetlov asvetlov changed the title Async test case Asyncio test case May 16, 2019
@asvetlov asvetlov closed this May 16, 2019
@asvetlov asvetlov reopened this May 16, 2019
self.addCleanup(self.on_cleanup)

async def tearDown(self):
self.assertEqual(events, ['setUp', 'test'])
Copy link
Member

Choose a reason for hiding this comment

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

One of the problems I can see happening here is multiple inheritance of tests cases with both "async" and "sync" versions of "tearDown".

  • How are you supposed to call super().tearDown() in such case?
  • Is this a serious problem? I think it might be, essentially we're breaking the cooperative nature of super().
  • Should we prefix all "async" hooks, e.g. "async def asyncTearDown"? Or is that too ugly?

miss-islington pushed a commit that referenced this pull request May 29, 2019
Add explicit `asyncSetUp` and `asyncTearDown` methods.
The rest is the same as for #13228

`AsyncTestCase` create a loop instance for every test for the sake of test isolation.
Sometimes a loop shared between all tests can speed up tests execution time a lot but it requires control of closed resources after every test finish. Basically, it requires nested supervisors support that was discussed with @1st1 many times. Sorry, asyncio supervisors have no chance to land on Python 3.8.

The PR intentionally does not provide API for changing the used event loop or getting the test loop: use `asyncio.set_event_loop_policy()` and `asyncio.get_event_loop()` instead.

The PR adds four overridable methods to base `unittest.TestCase` class:
```
    def _callSetUp(self):
        self.setUp()

    def _callTestMethod(self, method):
        method()

    def _callTearDown(self):
        self.tearDown()

    def _callCleanup(self, function, /, *args, **kwargs):
        function(*args, **kwargs)
```
It allows using asyncio facilities with minimal influence on the unittest code.

The last but not least: the PR respects contextvars. The context variable installed by `asyncSetUp` is available on test, `tearDown` and a coroutine scheduled by `addCleanup`.


https://bugs.python.org/issue32972
@asvetlov asvetlov closed this May 31, 2019
@asvetlov asvetlov deleted the async-test-case branch September 12, 2019 12:42
DinoV pushed a commit to DinoV/cpython that referenced this pull request Jan 14, 2020
Add explicit `asyncSetUp` and `asyncTearDown` methods.
The rest is the same as for python#13228

`AsyncTestCase` create a loop instance for every test for the sake of test isolation.
Sometimes a loop shared between all tests can speed up tests execution time a lot but it requires control of closed resources after every test finish. Basically, it requires nested supervisors support that was discussed with @1st1 many times. Sorry, asyncio supervisors have no chance to land on Python 3.8.

The PR intentionally does not provide API for changing the used event loop or getting the test loop: use `asyncio.set_event_loop_policy()` and `asyncio.get_event_loop()` instead.

The PR adds four overridable methods to base `unittest.TestCase` class:
```
    def _callSetUp(self):
        self.setUp()

    def _callTestMethod(self, method):
        method()

    def _callTearDown(self):
        self.tearDown()

    def _callCleanup(self, function, /, *args, **kwargs):
        function(*args, **kwargs)
```
It allows using asyncio facilities with minimal influence on the unittest code.

The last but not least: the PR respects contextvars. The context variable installed by `asyncSetUp` is available on test, `tearDown` and a coroutine scheduled by `addCleanup`.


https://bugs.python.org/issue32972
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants