Python版本:2.7.13
我已经使用pip安装了模拟
我正在尝试为进行外部api调用并返回响应的函数编写单元测试。
源代码(appl.py)
def getdetails(self, jsonData):
try:
## server not successfully registered as a deployment client
if self.__getClients(jsonData):
response = requests.get(cfg['url'], verify=False, auth=(cfg['username'], cfg['password']))
测试代码:
import unittest
import mock
from mock import patch, MagicMock
import appl
@patch('appl.requests.get') # Mock 'requests' module 'get' method.
def test_request_response_with_decorator(self, mock_get):
data = { "a": 1, "b": 2 }
"""Mocking using a patcher"""
mock_get_patcher = patch('appl.requests.get')
错误:
import mock
ImportError: No module named mock