mock
(Python 标准库) 一个用于伪造测试的库
使用
被测试的类
class Count():
def add(self):
pass
用mock测试
from unittest import mock
import unittest
from modular import Count
class TestCount(unittest.TestCase):
def test_add(self):
count = Count()
count.add = mock.Mock(return_value=13)
result = count.add(8,5)
self.assertEqual(result,13)
if __name__ == '__main__':
unittest.main()
HTTPretty
Python 的 HTTP 请求 mock 工具,不支持py3
安装
pip install HTTPretty
使用
import requests
import httpretty
def test_one():
httpretty.enable() # enable HTTPretty so that it will monkey patch the socket module
httpretty.register_uri(httpretty.GET, "http://yipit.com/",
body="Find the best daily deals")<