如何使用模拟框架模拟龙卷风协程功能进行单元测试?

Jim*_*rng 18 python unit-testing mocking tornado

标题简单描述了我的问题.我想用特定的返回值来模拟"_func_inner_1".谢谢你的任何建议:)

被测代码:

from tornado.gen import coroutine, Return
from tornado.testing import gen_test
from tornado.testing import AsyncTestCase

import mock

@coroutine
def _func_inner_1():
    raise Return(1)

@coroutine
def _func_under_test_1():
    temp = yield _func_inner_1()
    raise Return(temp + 1)
Run Code Online (Sandbox Code Playgroud)

但是,这种直观的解决方案无效

class Test123(AsyncTestCase):

    @gen_test
    @mock.patch(__name__ + '._func_inner_1')
    def test_1(self, mock_func_inner_1):
        mock_func_inner_1.side_effect = Return(9)
        result_1 = yield _func_inner_1()
        print 'result_1', result_1
        result = yield _func_under_test_1()
        self.assertEqual(10, result, result)
Run Code Online (Sandbox Code Playgroud)

如果出现以下错误,似乎_func_inner_1没有修补,因为它具有协同性质

AssertionError: 2
Run Code Online (Sandbox Code Playgroud)

如果我添加coroutine补丁返回模拟功能

@gen_test
@mock.patch(__name__ + '._func_inner_1')
def test_1(self, mock_func_inner_1):
    mock_func_inner_1.side_effect = Return(9)
    mock_func_inner_1 = coroutine(mock_func_inner_1)
    result_1 = yield _func_inner_1()
    print 'result_1', result_1
    result = yield _func_under_test_1()
    self.assertEqual(10, result, result)
Run Code Online (Sandbox Code Playgroud)

错误变成:

Traceback (most recent call last):
  File "tornado/testing.py", line 118, in __call__
    result = self.orig_method(*args, **kwargs)
  File "tornado/testing.py", line 494, in post_coroutine
    timeout=timeout)
  File "tornado/ioloop.py", line 418, in run_sync
    return future_cell[0].result()
  File "tornado/concurrent.py", line 109, in result
    raise_exc_info(self._exc_info)
  File "tornado/gen.py", line 175, in wrapper
    yielded = next(result)
  File "coroutine_unit_test.py", line 39, in test_1
    mock_func_inner_1 = coroutine(mock_func_inner_1)
  File "tornado/gen.py", line 140, in coroutine
    return _make_coroutine_wrapper(func, replace_callback=True)
  File "tornado/gen.py", line 150, in _make_coroutine_wrapper
    @functools.wraps(func)
  File "functools.py", line 33, in update_wrapper
    setattr(wrapper, attr, getattr(wrapped, attr))
  File "mock.py", line 660, in __getattr__
    raise AttributeError(name)
AttributeError: __name__
Run Code Online (Sandbox Code Playgroud)

这是我能找到的最接近的解决方案,但是在测试用例执行后不会重置模拟函数,这与补丁不同

@gen_test
def test_4(self):
    global _func_inner_1
    mock_func_inner_1 = mock.create_autospec(_func_inner_1)
    mock_func_inner_1.side_effect = Return(100)
    mock_func_inner_1 = coroutine(mock_func_inner_1)
    _func_inner_1 = mock_func_inner_1
    result = yield _func_under_test_1()
    self.assertEqual(101, result, result) 
Run Code Online (Sandbox Code Playgroud)

Ben*_*ell 29

这里有两个问题:

首先是之间的相互作用@mock.patch@gen_test.gen_test通过将生成器转换为"正常"函数来工作; mock.patch仅适用于普通函数(就装饰者所知,生成器一到达第一个就返回yield,因此mock.patch撤消其所有工作).为了避免这个问题,你可以重新排列装饰(始终把@mock.patch @gen_test,或使用with的形式mock.patch,而不是装饰形式.

其次,协同程序不应该提出异常.相反,它们返回一个Future包含结果或异常的内容.特殊Return例外由协程系统封装; 你永远不会从未来提出它.创建模拟时,必须创建适当的Future并将其设置为返回值,而不是使用side_effect来引发异常.

完整的解决方案是:

from tornado.concurrent import Future
from tornado.gen import coroutine, Return
from tornado.testing import gen_test
from tornado.testing import AsyncTestCase

import mock

@coroutine
def _func_inner_1():
    raise Return(1)

@coroutine
def _func_under_test_1():
    temp = yield _func_inner_1()
    raise Return(temp + 1)

class Test123(AsyncTestCase):

    @mock.patch(__name__ + '._func_inner_1')
    @gen_test
    def test_1(self, mock_func_inner_1):
        future_1 = Future()
        future_1.set_result(9)
        mock_func_inner_1.return_value = future_1
        result_1 = yield _func_inner_1()
        print 'result_1', result_1
        result = yield _func_under_test_1()
        self.assertEqual(10, result, result)

import unittest
unittest.main()
Run Code Online (Sandbox Code Playgroud)