How can I make the following test to work?
mymodule.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
tests.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)
When I execute the test I got a ConnectionError
exception because foo
is executing the real requests.post
.