0

如何使以下测试起作用?

我的模块.py

import requests

http_methods = {
    "GET": requests.get,
    "POST": requests.post,
    "PUT": requests.put,
    "DELETE": requests.delete
}

def foo(method):
    r = http_methods[method]("http://some.thing")
    return r.status_code

测试.py

import unittest
from mock import patch
import mymodule

class MyTestCase(unittest.TestCase):
    ...
    @patch("requests.post")
    def test_foo(self, post):
        post.return_value = 200
        self.assertEquals(mymodule.foo("POST"), 200)

当我执行测试时出现ConnectionError异常,因为foo正在执行真正的requests.post.

4

1 回答 1

1

试试看@patch("mymodule.requests.post")哪里打补丁

于 2013-05-13T19:32:06.063 回答