[Async-sig] no current event loop in thread 'MainThread'
Pradip Caulagi
caulagi at gmail.com
Fri Jul 7 11:22:32 EDT 2017
I am trying to write a test for a function that uses async. Can I mix
using unittest.TestCase and asyncio.test_utils.TestCase? When I do
that, the loop in test_utils.TestCase seems to affect the other code I
have. This is a simplified example -
$ cat foo.py
import asyncio
import unittest
from asyncio import test_utils
async def foo():
return True
class Foo1Test(test_utils.TestCase):
def setUp(self):
super().setUp()
self.loop = self.new_test_loop()
self.set_event_loop(self.loop)
def test_foo(self):
res = self.loop.run_until_complete(foo())
assert res is True
class Foo2Test(unittest.TestCase):
def test_foo(self):
loop = asyncio.get_event_loop()
res = loop.run_until_complete(foo())
assert res is True
$ python3
Python 3.6.1 (default, Apr 4 2017, 09:40:21)
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.38)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>
$ python3 -m unittest foo.py
.E
======================================================================
ERROR: test_foo (foo.Foo2Test)
----------------------------------------------------------------------
Traceback (most recent call last):
File "/private/tmp/foo.py", line 26, in test_foo
loop = asyncio.get_event_loop()
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/events.py",
line 678, in get_event_loop
return get_event_loop_policy().get_event_loop()
File "/usr/local/Cellar/python3/3.6.1/Frameworks/Python.framework/Versions/3.6/lib/python3.6/asyncio/events.py",
line 584, in get_event_loop
% threading.current_thread().name)
RuntimeError: There is no current event loop in thread 'MainThread'.
----------------------------------------------------------------------
Ran 2 tests in 0.001s
What am I missing?
--
Pradip Caulagi
http://caulagi.com
More information about the Async-sig
mailing list