Python 模拟 : How to test the number of calls on a recursive function?

标签 python unit-testing recursion mocking

我在名为 test_module 的模块中有一个递归函数

import requests    

def send_msg(msg, retries=0):
    try:
        # send the message here, e.g. a http request
        response = requests.get("http://www.doesnotexist98734.com")
        # if url does not exist raise an exception
    except Exception as e:
        if retries == 0:
            raise e
        else:
            return send_msg(msg, retries=retries-1)

我的问题是如何编写一个单元测试来检查 send_msg 函数在我设置 retries = n 时被调用了 n 次。我正在玩模拟模块(我使用的是 python 2.7),我想我想要这样的东西,

import mock, unittest

class MyUnitTest(unittest.TestCase):

    @mock.patch('test_module.send_msg')
    def test_send_msg_tries_n_times(self, mock_send_msg):
        with self.assertRaises(Exception):
            mock_send_msg("hello", retries=3)
        self.assertEqual(mock_send_msg.call_count, 4) # initial call + 3 retries

然而,由于我已经模拟了该函数,它不会调用真正的函数,所以我不会得到异常,也不会递归地调用自己...

最佳答案

您不能模拟被测函数。您想要测试预期结果,而不是函数是否正确使用了递归。

模拟 request.get() 调用,让它总是产生异常。然后计算你的模拟被调用的频率。

@mock.patch('requests.get')
def test_send_msg_tries_n_times(self, req_get_mock):
    req_get_mock.side_effect = Exception
    with self.assertRaises(Exception):
        send_msg("hello", retries=3)
    self.assertEqual(req_get_mock.call_count, 4)  # 1 initial call + 3 retries

如果将来您想避免使用递归并希望使用迭代,您的测试仍然可以工作,因为它验证的是行为,而不是特定的实现。您可以安全地重构被测函数。

关于Python 模拟 : How to test the number of calls on a recursive function?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31993268/

相关文章:

c# - 如何在 NUnit 中测试此 View 模型的异步行为?

python - 为什么在连接两个字符串时 Python 比 C 快?

python - 如何使用 Gitlab CI 提高基于 python 的无服务器框架项目的部署速度

python - 将 faker 与 selenium 和 python 结合使用

django - 如何从所有应用程序加载 Django 装置?

c++ - 计算将 n 划分为正整数之和的方法数 c++

python - tensorflow 的线性回归非常慢

python - 如何单元测试命令行参数?

c++ - 减少 C++ 递归函数中堆栈的使用

java - 用递归方法解决 stackoverflowException