4

I'm a new with Python 3.6 (was writing a code on Python 2.X a few years) and I'm trying to write some unit tests for my asynchronous code.

I have some asynchronous function like this:

class MyClass(object):
    async def my_func(self):
        # Doing something with 'await', etc.

Also, I'm writing my test like this:

class MyClassTest(unittest.TestCase):
    async def test_my_func(self):
        my_class_instance = MyClass()
        data = await my_class_instance.my_func()

I hope, this should work, but running this test like this:

notetests -s my_tests.py

Shows me an issue:

/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/case.py:605: RuntimeWarning: coroutine 'MyClassTest.my_func' was never awaited testMethod()

It looks like I have await and it should work fine, but my data is not containing any data.

What went wrong?

P.S. I'm not using tornado, that's why there is no sense to add this dependency here.

smart
  • 1,975
  • 5
  • 26
  • 46
  • Possible duplicate of [How to test Python 3.4 asyncio code?](https://stackoverflow.com/questions/23033939/how-to-test-python-3-4-asyncio-code) – Lex Scarisbrick Aug 27 '17 at 12:25
  • @LexScarisbrick, I was reading a few similar topics, but it looks like this requires to write some custom decorators, etc. It looks strange for me because I think that some default behavior should exist. – smart Aug 27 '17 at 12:51
  • 2
    Async code needs to run in an event loop; that applies to tests, as well. The custom decorators in the answer @LexScarisbrick mentioned sets that up for you. – Xophmeister Sep 19 '17 at 15:39
  • that is not a solution for `nosetests`, as it doesnt discover the tests. – WiseStrawberry Mar 06 '20 at 15:31

0 Answers0