Hi everyone and good morning to some of you, Since asyncio and the async/await syntax are both part of Python, I think that we should extend TestCase to support it. The simplest solution that I can think of is to create unittest.AsyncTestCase sub-class with the following extensions: - create a new event loop and install it in AsyncTestCase.run - make it possible for setUp, tearDown, and test methods to be async by calling asyncio.iscoroutinefunction I wrote my own in a local test before noticing that Martin Richard had already written and published asynctest [1]. Since then I found the following projects as well: - https://github.com/kwarunek/aiounittest <https://github.com/kwarunek/aiounittest> - https://github.com/pytest-dev/pytest-asyncio <https://github.com/pytest-dev/pytest-asyncio> I think that the community as a whole would benefit from basic support in unittest for async/await test "user methods". I am personally fond of the approach of extending unittest.TestCase in asynctest [2] over some of the other approaches. Is this something that we want in our Standard Library? - dave -- [1]: https://github.com/Martiusweb/asynctest <https://github.com/Martiusweb/asynctest> [2]: https://github.com/Martiusweb/asynctest/blob/master/asynctest/case.py <https://github.com/Martiusweb/asynctest/blob/master/asynctest/case.py>
Thanks for proposing this. Yes, it makes sense to have unittest.AsyncTestCase in 3.8. AFAIK Lisa Roach (copied) was working on that (as well as on async Mock object), but I'm not sure what's the status of her work. I suggest to search for an open issue for this on bugs.python.org; if there's none, please create one. And let's make this happen. Yury On Wed, Oct 10, 2018 at 7:11 AM David Shawley <daveshawley@gmail.com> wrote:
Hi everyone and good morning to some of you,
Since asyncio and the async/await syntax are both part of Python, I think that we should extend TestCase to support it. The simplest solution that I can think of is to create unittest.AsyncTestCase sub-class with the following extensions:
- create a new event loop and install it in AsyncTestCase.run - make it possible for setUp, tearDown, and test methods to be async by calling asyncio.iscoroutinefunction
I wrote my own in a local test before noticing that Martin Richard had already written and published asynctest [1]. Since then I found the following projects as well:
- https://github.com/kwarunek/aiounittest - https://github.com/pytest-dev/pytest-asyncio
I think that the community as a whole would benefit from basic support in unittest for async/await test "user methods". I am personally fond of the approach of extending unittest.TestCase in asynctest [2] over some of the other approaches.
Is this something that we want in our Standard Library?
- dave -- [1]: https://github.com/Martiusweb/asynctest [2]: https://github.com/Martiusweb/asynctest/blob/master/asynctest/case.py _______________________________________________ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct: http://python.org/psf/codeofconduct/
-- Yury
10.10.18 20:19, Yury Selivanov пише:
Thanks for proposing this. Yes, it makes sense to have unittest.AsyncTestCase in 3.8. AFAIK Lisa Roach (copied) was working on that (as well as on async Mock object), but I'm not sure what's the status of her work. I suggest to search for an open issue for this on bugs.python.org; if there's none, please create one. And let's make this happen.
https://bugs.python.org/issue32972 https://bugs.python.org/issue26467
On Oct 10, 2018, at 1:34 PM, Serhiy Storchaka <storchaka@gmail.com> wrote:
10.10.18 20:19, Yury Selivanov пише:
Thanks for proposing this. Yes, it makes sense to have unittest.AsyncTestCase in 3.8. AFAIK Lisa Roach (copied) was working on that (as well as on async Mock object), but I'm not sure what's the status of her work. I suggest to search for an open issue for this on bugs.python.org; if there's none, please create one. And let's make this happen.
https://bugs.python.org/issue32972 https://bugs.python.org/issue26467
I have a working prototype of asynchronous testing in a branch on github [1] but I took a different approach than what was discussed in bpo-32972. Should I open a new bpo or continue the discussion on the existing bpo? I'm leaning in the direction of opening a new bpo so as to not muddy the waters with the discussion around the other branch. Here's a summary of the approach that I took: - create a new class unittest.AsyncioTestCase that implements a few new methods and attributes: - asyncSetUp / asyncTearDown: are awaitable and call the synchronous setUp and tearDown methods. These only exist on the new sub-class. - _runTest: a new method on unittest.TestCase that encapsulates running the test case. This is simply the portion of unittest.TestCase.run() that runs setUp()/testMethod()/tearDown(). - _terminateTest: a new method on unittest.TestCase that is *always* called from within run() immediately *before* calling stopTest() on the result. The AsyncioTestCase specialization of this method is responsible to cleaning up the event loop in the same manner that asyncio.run() does. In particular, it shuts down asynchronous generators. [a] - event_loop: an overridable property on the new sub-class that creates and installs a new event loop. This is referred to from within the AsyncioTestCase specialization of _runTest to create the event loop - event loops are created and destroyed for each test method [b] - the same event loop persists from before setUp until after the cleanups are called - test methods that asyncio.iscoroutinefunction thinks are co-routines are run on the event loop; other test methods are called directly [c] With that said, should I open a new bpo or start discussion on the existing one anew? Cheers, dave. -- [1]: https://github.com/dave-shawley/cpython/pull/1 [2]: https://github.com/python/cpython/pull/9296 [a]: I explicitly chose to not shutdown outstanding tasks in _terminateTest so that the event loop complains loudly about them. This is an area that I would like to discuss further. [b]: I chose to create and destroy an event loop with each test method to avoid having tasks and other event loop settings from leaking between test calls. [c]: This is problematic since mock.patch decorated co-routines won't be called correctly. I'm going to discuss this with @lisroach on her bpo-26467 patch [2]. I have a feeling that the "AwaitableMock" approach may work out and change this code. See the most recent comments on [2] for details. -- People think right angles produce harmony, but they don't. They produce sleep. -- James Krenov
Why not just use pytest? On Wednesday, October 10, 2018 at 7:12:02 AM UTC-4, David Shawley wrote:
Hi everyone and good morning to some of you,
Since asyncio and the async/await syntax are both part of Python, I think that we should extend TestCase to support it. The simplest solution that I can think of is to create unittest.AsyncTestCase sub-class with the following extensions:
- create a new event loop and install it in AsyncTestCase.run - make it possible for setUp, tearDown, and test methods to be async by calling asyncio.iscoroutinefunction
I wrote my own in a local test before noticing that Martin Richard had already written and published asynctest [1]. Since then I found the following projects as well:
- https://github.com/kwarunek/aiounittest - https://github.com/pytest-dev/pytest-asyncio
I think that the community as a whole would benefit from basic support in unittest for async/await test "user methods". I am personally fond of the approach of extending unittest.TestCase in asynctest [2] over some of the other approaches.
Is this something that we want in our Standard Library?
- dave -- [1]: https://github.com/Martiusweb/asynctest [2]: https://github.com/Martiusweb/asynctest/blob/master/asynctest/case.py
Sorry if this double posted but I got a bounce from python-ideas@googlegroups.com the first time that I sent it. I resent to python-ideas@python.org. On Oct 29, 2018, at 3:07 AM, Neil Girdhar <mistersheik@gmail.com> wrote:
Why not just use pytest?
I could use pytest or Martin Richard's asynctest[1]. I want the ability to test asynchronous code without pulling in a 3rd party library. I noticed that a number of the libraries in https://github.com/aio-libs/ have their own custom-rolled test support in addition to those that use pytest. Now that the async/await syntax has landed, the Standard Library should provide the ability to test event loop driven code. This will remove at least on barrier to entry for async/await based code -- you can simply test it using unittest. - cheers, dave. [1]: https://github.com/Martiusweb/asynctest -- "There are only two hard things in Computer Science: cache invalidation and naming things." -- Phil Karlton
participants (4)
-
David Shawley
-
Neil Girdhar
-
Serhiy Storchaka
-
Yury Selivanov