需要写单元测试,初步打算使用unittest测试框架写测试用例。但是受具体环境限制,不能真正发送HTTP请求。
现在,退而求其次,写一个测试用例,不用发送请求并验证服务器返回的数据,只需要检查HTTP请求的头部和数据部分是否正确就好。 怎么写这种测试用例? 初步的想法是mock一个HTTP Server
OK, found unittest.mock.
unittest.mock was originally used to hook third-party modules (classes/functions) in unit tests (bypassing the original modules), but HTTP requests are also sent through a certain function and can also be used to simulate.
The following is an example: main.pyProgram to be tested
from urllib.request import urlopen
def func():
url_ip = 'http://ifconfig.me/ip'
ip = None
try:
ip = urlopen(url_ip)
except:
pass
return ip
if __name__ == '__main__':
print(func())
test.pyTest Case
import unittest
from unittest import mock
from main import func
class FuncTestCase(unittest.TestCase):
@mock.patch('main.urlopen')
def test_func(self, mock_open):
mock_open.return_value = '127.0.0.1'
#设置模拟函数的返回值
result = func()
#调用待测功能,但已经模拟了urlopen函数
mock_open.assert_called_with('http://ifconfig.me/ip')
#验证调用参数
self.assertEqual(result, '127.0.0.1')
#验证返回结果
if __name__ == '__main__':
unittest.main()
Defect: If urlopen is called multiple times in func, the above code cannot be tested normally. Update: You can replace the system function with your own function by setting the side_effect attribute of the mock object (but it is not very elegant).
OK, found
unittest.mock
.unittest.mock was originally used to hook third-party modules (classes/functions) in unit tests (bypassing the original modules), but HTTP requests are also sent through a certain function and can also be used to simulate.
The following is an example:
main.py
Program to be testedtest.py
Test CaseDefect: If urlopen is called multiple times in func, the above code cannot be tested normally.
Update: You can replace the system function with your own function by setting the
side_effect
attribute of the mock object (but it is not very elegant).You may need this.
http://docs.python-requests.org/en/latest/