I have a couple methods I'd like to unit test which use the Python requests
library. Essentially, they're doing something like this:
def my_method_under_test(self):
r = requests.get("https://ec2.amazonaws.com/", params={'Action': 'GetConsoleOutput',
'InstanceId': 'i-123456'})
# do other stuffs
I'd essentially like to be able to test that
The problem is that I'd like to be able to test this without actually making the request as it'll take too long and some operations are potentially destructive.
How can I mock and test this quickly and easily?
How about a simple mock:
from mock import patch
from mymodule import my_method_under_test
class MyTest(TestCase):
def test_request_get(self):
with patch('requests.get') as patched_get:
my_method_under_test()
# Ensure patched get was called, called only once and with exactly these params.
patched_get.assert_called_once_with("https://ec2.amazonaws.com/", params={'Action': 'GetConsoleOutput', 'InstanceId': 'i-123456'})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With