import unittest
import requests
from unittest import TestCase
from unittest.mock import patch
class User(object):
def get_user_info(uid):
resp = requests.get("http://api.server.com/user/{0}".format(uid))
return resp.json()
class TestUserInfo(TestCase):
@patch('__main__.User')
def test_user_name(self, MockUser):
user = MockUser()
user.get_user_info.return_value = {
"user_id": 1,
"name": "python",
"age": 20
}
resp = user.get_user_info(1)
self.assertEqual(resp.get("name"), "python")
self.assertEqual(resp.get("age"), 20)
if __name__ == '__main__':
unittest.main()
本系列文章和代码已经作为项目归档到github,仓库地址:jumper2014/PyCodeComplete。大家觉得有帮助就请在github上star一下,你的支持是我更新的动力。什么?你没有github账号?学习Python怎么可以没有github账号呢,快去注册一个啦!