Unit testing Python 3.6:如何正确编写单元测试?

Unit testing Python 3.6:如何正确编写单元测试?,unit-testing,asynchronous,async-await,python-3.6,Unit Testing,Asynchronous,Async Await,Python 3.6,我是Python3.6的新手(几年前我在Python2.X上写了一段代码),我正在尝试为我的异步代码编写一些单元测试 我有一些异步函数,如下所示: class MyClass(object): async def my_func(self): # Doing something with 'await', etc. class MyClassTest(unittest.TestCase): async def test_my_func(self):

我是Python3.6的新手(几年前我在Python2.X上写了一段代码),我正在尝试为我的异步代码编写一些单元测试

我有一些异步函数,如下所示:

class MyClass(object):
    async def my_func(self):
        # Doing something with 'await', etc.
class MyClassTest(unittest.TestCase):
    async def test_my_func(self):
        my_class_instance = MyClass()
        data = await my_class_instance.my_func()
notetests -s my_tests.py
另外,我的测试是这样写的:

class MyClass(object):
    async def my_func(self):
        # Doing something with 'await', etc.
class MyClassTest(unittest.TestCase):
    async def test_my_func(self):
        my_class_instance = MyClass()
        data = await my_class_instance.my_func()
notetests -s my_tests.py
我希望,这应该会起作用,但运行此测试时如下所示:

class MyClass(object):
    async def my_func(self):
        # Doing something with 'await', etc.
class MyClassTest(unittest.TestCase):
    async def test_my_func(self):
        my_class_instance = MyClass()
        data = await my_class_instance.my_func()
notetests -s my_tests.py
向我展示了一个问题:

/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()
看起来我有
wait
,它应该可以正常工作,但是我的
数据
不包含任何数据

出了什么问题


另外,我没有使用tornado,这就是为什么在这里添加此依赖项毫无意义。

可能与@LexScarisbrick重复,我读了一些类似的主题,但看起来这需要编写一些自定义装饰程序,这对我来说很奇怪,因为我认为应该存在一些默认行为;这同样适用于测试。answer@LexScarisbrick中提到的自定义装饰器为您设置了此选项。这不是
nosetests
的解决方案,因为它没有发现测试。