Home  >  Q&A  >  body text

单元测试 - Python如何测试HTTP客户端程序?

打算写一个模拟HTTP客户端的程序(通过urlopen来发送和接收HTTP请求)。

需要写单元测试,初步打算使用unittest测试框架写测试用例。但是受具体环境限制,不能真正发送HTTP请求。
现在,退而求其次,写一个测试用例,不用发送请求并验证服务器返回的数据,只需要检查HTTP请求的头部和数据部分是否正确就好。
怎么写这种测试用例?
初步的想法是mock一个HTTP Server

阿神阿神2715 days ago456

reply all(2)I'll reply

  • 怪我咯

    怪我咯2017-04-17 12:03:53

    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 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.py Test 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).

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 12:03:53

    You may need this.
    http://docs.python-requests.org/en/latest/

    reply
    0
  • Cancelreply