Python单元测试的九个技巧

开发 后端
requests是python知名的http爬虫库,同样简单易用,是python开源项目的TOP10。

 [[426078]]

本文转载自微信公众号「游戏不存在」,作者肖恩 。转载本文请联系游戏不存在公众号。

pytest是python的单元测试框架,简单易用,在很多知名项目中应用。requests是python知名的http爬虫库,同样简单易用,是python开源项目的TOP10。关于这2个项目,之前都有过介绍,本文主要介绍requests项目如何使用pytest进行单元测试,希望达到下面3个目标:

  1. 熟练pytest的使用
  2. 学习如何对项目进行单元测试
  3. 深入requests的一些实现细节

本文分如下几个部分:

  • requests项目单元测试状况
  • 简单工具类如何测试
  • request-api如何测试
  • 底层API测试

requests项目单元测试状况

requests的单元测试代码全部在 tests 目录,使用 pytest.ini 进行配置。测试除pytest外,还需要安装:

库名 描述
httpbin 一个使用flask实现的http服务,可以客户端定义http响应,主要用于测试http协议
pytest-httpbin pytest的插件,封装httpbin的实现
pytest-mock pytest的插件,提供mock
pytest-cov pytest的插件,提供覆盖率

上述依赖 master 版本在requirement-dev文件中定义;2.24.0版本会在pipenv中定义。

测试用例使用make命令,子命令在Makefile中定义, 使用make ci运行所有单元测试结果如下:

  1. $ make ci 
  2. pytest tests --junitxml=report.xml 
  3. ======================================================================================================= test session starts ======================================================================================================= 
  4. platform linux -- Python 3.6.8, pytest-3.10.1, py-1.10.0, pluggy-0.13.1 
  5. rootdir: /home/work6/project/requests, inifile: pytest.ini 
  6. plugins: mock-2.0.0, httpbin-1.0.0, cov-2.9.0 
  7. collected 552 items                                                                                                                                                                                                                
  8.  
  9. tests/test_help.py ...                                                                                                                                                                                                      [  0%] 
  10. tests/test_hooks.py ...                                                                                                                                                                                                     [  1%] 
  11. tests/test_lowlevel.py ...............                                                                                                                                                                                      [  3%] 
  12. tests/test_packages.py ...                                                                                                                                                                                                  [  4%] 
  13. tests/test_requests.py .................................................................................................................................................................................................... [ 39%] 
  14. 127.0.0.1 - - [10/Aug/2021 08:41:53] "GET /stream/4 HTTP/1.1" 200 756 
  15. .127.0.0.1 - - [10/Aug/2021 08:41:53] "GET /stream/4 HTTP/1.1" 500 59 
  16. ---------------------------------------- 
  17. Exception happened during processing of request from ('127.0.0.1', 46048) 
  18. Traceback (most recent call last): 
  19.   File "/usr/lib64/python3.6/wsgiref/handlers.py", line 138, in run 
  20.     self.finish_response() 
  21. x.........................................................................................                                                                                                                                 [ 56%] 
  22. tests/test_structures.py ....................                                                                                                                                                                               [ 59%] 
  23. tests/test_testserver.py ......s....                                                                                                                                                                                        [ 61%] 
  24. tests/test_utils.py ..s................................................................................................................................................................................................ssss [ 98%] 
  25. ssssss.....                                                                                                                                                                                                                 [100%] 
  26.  
  27. ----------------------------------------------------------------------------------- generated xml file: /home/work6/project/requests/report.xml ----------------------------------------------------------------------------------- 
  28. ======================================================================================= 539 passed, 12 skipped, 1 xfailed in 64.16 seconds ======================================================================================== 

可以看到requests在1分钟内,总共通过了539个测试用例,效果还是不错。使用 make coverage 查看单元测试覆盖率:

  1. $ make coverage 
  2. ----------- coverage: platform linux, python 3.6.8-final-0 ----------- 
  3. Name                          Stmts   Miss  Cover 
  4. ------------------------------------------------- 
  5. requests/__init__.py             71     71     0% 
  6. requests/__version__.py          10     10     0% 
  7. requests/_internal_utils.py      16      5    69% 
  8. requests/adapters.py            222     67    70% 
  9. requests/api.py                  20     13    35% 
  10. requests/auth.py                174     54    69% 
  11. requests/certs.py                 4      4     0% 
  12. requests/compat.py               47     47     0% 
  13. requests/cookies.py             238    115    52% 
  14. requests/exceptions.py           35     29    17% 
  15. requests/help.py                 63     19    70% 
  16. requests/hooks.py                15      4    73% 
  17. requests/models.py              455    119    74% 
  18. requests/packages.py             16     16     0% 
  19. requests/sessions.py            283     67    76% 
  20. requests/status_codes.py         15     15     0% 
  21. requests/structures.py           40     19    52% 
  22. requests/utils.py               465    170    63% 
  23. ------------------------------------------------- 
  24. TOTAL                          2189    844    61% 
  25. Coverage XML written to file coverage.xml 

结果显示requests项目总体覆盖率61%,每个模块的覆盖率也清晰可见。

单元测试覆盖率使用代码行数进行判断,Stmts显示模块的有效行数,Miss显示未执行到的行。如果生成html的报告,还可以定位到具体未覆盖到的行;pycharm的coverage也有类似功能。

tests下的文件及测试类如下表:

文件 描述
compat python2和python3兼容
conftest pytest配置
test_help,test_packages,test_hooks,test_structures 简单测试类
utils.py 工具函数
test_utils 测试工具函数
test_requests 测试requests
testserver\server 模拟服务
test_testserver 模拟服务测试
test_lowlevel 使用模拟服务测试模拟网络测试

简单工具类如何测试

test_help 实现分析

先从最简单的test_help上手,测试类和被测试对象命名是对应的。先看看被测试的模块help.py。这个模块主要是2个函数 info 和 _implementation:

  1. import idna 
  2.  
  3. def _implementation(): 
  4.     ... 
  5.      
  6. def info(): 
  7.     ... 
  8.     system_ssl = ssl.OPENSSL_VERSION_NUMBER 
  9.     system_ssl_info = { 
  10.         'version''%x' % system_ssl if system_ssl is not None else '' 
  11.     } 
  12.     idna_info = { 
  13.         'version': getattr(idna, '__version__'''), 
  14.     } 
  15.     ... 
  16.     return { 
  17.         'platform': platform_info, 
  18.         'implementation': implementation_info, 
  19.         'system_ssl': system_ssl_info, 
  20.         'using_pyopenssl': pyopenssl is not None, 
  21.         'pyOpenSSL': pyopenssl_info, 
  22.         'urllib3': urllib3_info, 
  23.         'chardet': chardet_info, 
  24.         'cryptography': cryptography_info, 
  25.         'idna': idna_info, 
  26.         'requests': { 
  27.             'version': requests_version, 
  28.         }, 
  29.     } 

info提供系统环境的信息,_implementation是其内部实现,以下划线*_*开头。再看测试类test_help:

  1. from requests.help import info 
  2.  
  3. def test_system_ssl(): 
  4.     """Verify we're actually setting system_ssl when it should be available.""" 
  5.     assert info()['system_ssl']['version'] != '' 
  6.  
  7. class VersionedPackage(object): 
  8.     def __init__(self, version): 
  9.         self.__version__ = version 
  10.  
  11. def test_idna_without_version_attribute(mocker): 
  12.     """Older versions of IDNA don't provide a __version__ attribute, verify 
  13.     that if we have such a package, we don't blow up. 
  14.     ""
  15.     mocker.patch('requests.help.idna', new=None) 
  16.     assert info()['idna'] == {'version'''
  17.  
  18. def test_idna_with_version_attribute(mocker): 
  19.     """Verify we're actually setting idna version when it should be available.""" 
  20.     mocker.patch('requests.help.idna', new=VersionedPackage('2.6')) 
  21.     assert info()['idna'] == {'version''2.6'

首先从头部的导入信息可以看到,仅仅对info函数进行测试,这个容易理解。info测试通过,自然覆盖到_implementation这个内部函数。这里可以得到单元测试的第1个技巧:

1.仅对public的接口进行测试

test_idna_without_version_attribute和test_idna_with_version_attribute均有一个mocker参数,这是pytest-mock提供的功能,会自动注入一个mock实现。使用这个mock对idna模块进行模拟

  1. # 模拟空实现 
  2. mocker.patch('requests.help.idna', new=None) 
  3. # 模拟版本2.6 
  4. mocker.patch('requests.help.idna', new=VersionedPackage('2.6')) 

可能大家会比较奇怪,这里patch模拟的是 requests.help.idna , 而我们在help中导入的是 inda 模块。这是因为在requests.packages中对inda进行了模块名重定向:

  1. for package in ('urllib3''idna''chardet'): 
  2.     locals()[package] = __import__(package) 
  3.     # This traversal is apparently necessary such that the identities are 
  4.     # preserved (requests.packages.urllib3.* is urllib3.*) 
  5.     for mod in list(sys.modules): 
  6.         if mod == package or mod.startswith(package + '.'): 
  7.             sys.modules['requests.packages.' + mod] = sys.modules[mod] 

使用mocker后,idna的__version__信息就可以进行控制,这样info中的idna结果也就可以预期。那么可以得到第2个技巧:

2.使用mock辅助单元测试

test_hooks 实现分析

我们继续查看hooks如何进行测试:

  1. from requests import hooks 
  2.  
  3. def hook(value): 
  4.     return value[1:] 
  5.  
  6. @pytest.mark.parametrize( 
  7.     'hooks_list, result', ( 
  8.         (hook, 'ata'), 
  9.         ([hook, lambda x: None, hook], 'ta'), 
  10.     ) 
  11. def test_hooks(hooks_list, result): 
  12.     assert hooks.dispatch_hook('response', {'response': hooks_list}, 'Data') == result 
  13.  
  14. def test_default_hooks(): 
  15.     assert hooks.default_hooks() == {'response': []} 

hooks模块的2个接口default_hooks和dispatch_hook都进行了测试。其中default_hooks是纯函数,无参数有返回值,这种函数最容易测试,仅仅检查返回值是否符合预期即可。dispatch_hook会复杂一些,还涉及对回调函数(hook函数)的调用:

  1. def dispatch_hook(key, hooks, hook_data, **kwargs): 
  2.     """Dispatches a hook dictionary on a given piece of data.""" 
  3.     hooks = hooks or {} 
  4.     hooks = hooks.get(key
  5.     if hooks: 
  6.         # 判断钩子函数 
  7.         if hasattr(hooks, '__call__'): 
  8.             hooks = [hooks] 
  9.         for hook in hooks: 
  10.             _hook_data = hook(hook_data, **kwargs) 
  11.             if _hook_data is not None: 
  12.                 hook_data = _hook_data 
  13.     return hook_data 

pytest.mark.parametrize提供了2组参数进行测试。第一组参数hook和ata很简单,hook是一个函数,会对参数裁剪,去掉首位,ata是期望的返回值。test_hooks的response的参数是Data,所以结果应该是ata。第二组参数中的第一个参数会复杂一些,变成了一个数组,首位还是hook函数,中间使用一个匿名函数,匿名函数没有返回值,这样覆盖到 if _hook_data is not None: 的旁路分支。执行过程如下:

  • hook函数裁剪Data首位,剩余ata
  • 匿名函数不对结果修改,剩余ata
  • hook函数继续裁剪ata首位,剩余ta

经过测试可以发现dispatch_hook的设计十分巧妙,使用pipeline模式,将所有的钩子串起来,这是和事件机制不一样的地方。细心的话,我们可以发现 if hooks: 并未进行旁路测试,这个不够严谨,有违我们的第3个技巧:

3.测试尽可能覆盖目标函数的所有分支

test_structures 实现分析

LookupDict的测试用例如下:

  1. class TestLookupDict: 
  2.  
  3.     @pytest.fixture(autouse=True
  4.     def setup(self): 
  5.         """LookupDict instance with "bad_gateway" attribute.""" 
  6.         self.lookup_dict = LookupDict('test'
  7.         self.lookup_dict.bad_gateway = 502 
  8.  
  9.     def test_repr(self): 
  10.         assert repr(self.lookup_dict) == "<lookup 'test'>" 
  11.  
  12.     get_item_parameters = pytest.mark.parametrize( 
  13.         'key, value', ( 
  14.             ('bad_gateway', 502), 
  15.             ('not_a_key', None) 
  16.         ) 
  17.     ) 
  18.  
  19.     @get_item_parameters 
  20.     def test_getitem(self, key, value): 
  21.         assert self.lookup_dict[key] == value 
  22.  
  23.     @get_item_parameters 
  24.     def test_get(self, key, value): 
  25.         assert self.lookup_dict.get(key) == value 

可以发现使用setup方法配合@pytest.fixture,给所有测试用例初始化了一个lookup_dict对象;同时pytest.mark.parametrize可以在不同的测试用例之间复用的,我们可以得到第4个技巧:

4.使用pytest.fixture复用被测试对象,使用pytest.mark.parametriz复用测试参数

通过TestLookupDict的test_getitem和test_get可以更直观的了解LookupDict的get和__getitem__方法的作用:

  1. class LookupDict(dict): 
  2.     ... 
  3.     def __getitem__(self, key): 
  4.         # We allow fall-through here, so values default to None 
  5.         return self.__dict__.get(key, None) 
  6.  
  7.     def get(self, keydefault=None): 
  8.         return self.__dict__.get(keydefault
  • get自定义字典,使其可以使用 get 方法获取值
  • __getitem__自定义字典,使其可以使用 [] 符合获取值

CaseInsensitiveDict的测试用例在test_structures和test_requests中都有测试,前者主要是基础测试,后者偏向业务使用层面,我们可以看到这两种差异:

  1. class TestCaseInsensitiveDict: 
  2.     # 类测试 
  3.     def test_repr(self): 
  4.         assert repr(self.case_insensitive_dict) == "{'Accept': 'application/json'}" 
  5.  
  6.     def test_copy(self): 
  7.         copy = self.case_insensitive_dict.copy() 
  8.         assert copy is not self.case_insensitive_dict 
  9.         assert copy == self.case_insensitive_dict 
  10.  
  11. class TestCaseInsensitiveDict: 
  12.     # 使用方法测试 
  13.     def test_delitem(self): 
  14.         cid = CaseInsensitiveDict() 
  15.         cid['Spam'] = 'someval' 
  16.         del cid['sPam'
  17.         assert 'spam' not in cid 
  18.         assert len(cid) == 0 
  19.  
  20.     def test_contains(self): 
  21.         cid = CaseInsensitiveDict() 
  22.         cid['Spam'] = 'someval' 
  23.         assert 'Spam' in cid 
  24.         assert 'spam' in cid 
  25.         assert 'SPAM' in cid 
  26.         assert 'sPam' in cid 
  27.         assert 'notspam' not in cid 

借鉴上面的测试方法,不难得出第5个技巧:

5.可以从不同的层面对同一个对象进行单元测试

后面的test_lowlevel和test_requests也应用了这种技巧

utils.py

utils中构建了一个可以写入env的生成器(由yield关键字提供),可以当上下文装饰器使用:

  1. import contextlib 
  2. import os 
  3.  
  4. @contextlib.contextmanager 
  5. def override_environ(**kwargs): 
  6.     save_env = dict(os.environ) 
  7.     for key, value in kwargs.items(): 
  8.         if value is None: 
  9.             del os.environ[key
  10.         else
  11.             os.environ[key] = value 
  12.     try: 
  13.         yield 
  14.     finally: 
  15.         os.environ.clear() 
  16.         os.environ.update(save_env) 

下面是使用方法示例:

  1. # test_requests.py 
  2.  
  3. kwargs = { 
  4.     var: proxy 
  5. # 模拟控制proxy环境变量 
  6. with override_environ(**kwargs): 
  7.     proxies = session.rebuild_proxies(prep, {}) 
  8.      
  9. def rebuild_proxies(self, prepared_request, proxies):   
  10.     bypass_proxy = should_bypass_proxies(url, no_proxy=no_proxy) 
  11.   
  12. def should_bypass_proxies(url, no_proxy): 
  13.     ... 
  14.     get_proxy = lambda k: os.environ.get(k) or os.environ.get(k.upper()) 
  15.     ... 

6.涉及环境变量的地方,可以使用上下文装饰器进行模拟多种环境变量

utils测试用例

utils的测试用例较多,我们选择部分进行分析。先看to_key_val_list函数:

  1. # 对象转列表 
  2. def to_key_val_list(value): 
  3.     if value is None: 
  4.         return None 
  5.  
  6.     if isinstance(value, (str, bytes, bool, int)): 
  7.         raise ValueError('cannot encode objects that are not 2-tuples'
  8.  
  9.     if isinstance(value, Mapping): 
  10.         value = value.items() 
  11.  
  12.     return list(value) 

对应的测试用例TestToKeyValList:

  1. class TestToKeyValList: 
  2.  
  3.     @pytest.mark.parametrize( 
  4.         'value, expected', ( 
  5.             ([('key''val')], [('key''val')]), 
  6.             ((('key''val'), ), [('key''val')]), 
  7.             ({'key''val'}, [('key''val')]), 
  8.             (None, None) 
  9.         )) 
  10.     def test_valid(self, value, expected): 
  11.         assert to_key_val_list(value) == expected 
  12.  
  13.     def test_invalid(self): 
  14.         with pytest.raises(ValueError): 
  15.             to_key_val_list('string'

重点是test_invalid中使用pytest.raise对异常的处理:

7.使用pytest.raises对异常进行捕获处理

TestSuperLen介绍了几种进行IO模拟测试的方法:

  1. class TestSuperLen: 
  2.  
  3.     @pytest.mark.parametrize( 
  4.         'stream, value', ( 
  5.             (StringIO.StringIO, 'Test'), 
  6.             (BytesIO, b'Test'), 
  7.             pytest.param(cStringIO, 'Test'
  8.                          marks=pytest.mark.skipif('cStringIO is None')), 
  9.         )) 
  10.     def test_io_streams(self, stream, value): 
  11.         """Ensures that we properly deal with different kinds of IO streams.""" 
  12.         assert super_len(stream()) == 0 
  13.         assert super_len(stream(value)) == 4 
  14.  
  15.     def test_super_len_correctly_calculates_len_of_partially_read_file(self): 
  16.         """Ensure that we handle partially consumed file like objects.""" 
  17.         s = StringIO.StringIO() 
  18.         s.write('foobarbogus'
  19.         assert super_len(s) == 0 
  20.      
  21.     @pytest.mark.parametrize( 
  22.         'mode, warnings_num', ( 
  23.             ('r', 1), 
  24.             ('rb', 0), 
  25.         )) 
  26.     def test_file(self, tmpdir, mode, warnings_num, recwarn): 
  27.         file_obj = tmpdir.join('test.txt'
  28.         file_obj.write('Test'
  29.         with file_obj.open(mode) as fd: 
  30.             assert super_len(fd) == 4 
  31.         assert len(recwarn) == warnings_num 
  32.  
  33.     def test_super_len_with_tell(self): 
  34.         foo = StringIO.StringIO('12345'
  35.         assert super_len(foo) == 5 
  36.         foo.read(2) 
  37.         assert super_len(foo) == 3 
  38.  
  39.     def test_super_len_with_fileno(self): 
  40.         with open(__file__, 'rb'as f: 
  41.             length = super_len(f) 
  42.             file_data = f.read() 
  43.         assert length == len(file_data) 
  • 使用StringIO来模拟IO操作,可以配置各种IO的测试。当然也可以使用BytesIO/cStringIO, 不过单元测试用例一般不关注性能,StringIO简单够用。
  • pytest提供tmpdir的fixture,可以进行文件读写操作测试
  • 可以使用__file__来进行文件的只读测试,__file__表示当前文件,不会产生副作用。

8.使用IO模拟配合进行单元测试

request-api如何测试

requests的测试需要httpbin和pytest-httpbin,前者会启动一个本地服务,后者会安装一个pytest插件,测试用例中可以得到httpbin的fixture,用来操作这个服务的URL。

功能
TestRequests requests业务测试
TestCaseInsensitiveDict 大小写不敏感的字典测试
TestMorselToCookieExpires cookie过期测试
TestMorselToCookieMaxAge cookie大小
TestTimeout 响应超时的测试
TestPreparingURLs URL预处理
... 一些零碎的测试用例

坦率的讲:这个测试用例内容庞大,达到2500行。看起来是针对各种业务的零散case,我并没有完全理顺其组织逻辑。我选择一些感兴趣的业务进行介绍, 先看TimeOut的测试:

  1. TARPIT = 'http://10.255.255.1' 
  2.  
  3. class TestTimeout: 
  4.  
  5.     def test_stream_timeout(self, httpbin): 
  6.         try: 
  7.             requests.get(httpbin('delay/10'), timeout=2.0) 
  8.         except requests.exceptions.Timeout as e: 
  9.             assert 'Read timed out' in e.args[0].args[0] 
  10.      
  11.     @pytest.mark.parametrize( 
  12.     'timeout', ( 
  13.         (0.1, None), 
  14.         Urllib3Timeout(connect=0.1, read=None) 
  15.     )) 
  16.     def test_connect_timeout(self, timeout): 
  17.         try: 
  18.             requests.get(TARPIT, timeout=timeout) 
  19.             pytest.fail('The connect() request should time out.'
  20.         except ConnectTimeout as e: 
  21.             assert isinstance(e, ConnectionError) 
  22.             assert isinstance(e, Timeout) 

test_stream_timeout利用httpbin创建了一个延迟10s响应的接口,然后请求本身设置成2s,这样可以收到一个本地timeout的错误。test_connect_timeout则是访问一个不存在的服务,捕获连接超时的错误。

TestRequests都是对requests的业务进程测试,可以看到至少是2种:

  1. class TestRequests: 
  2.      
  3.     def test_basic_building(self): 
  4.         req = requests.Request() 
  5.         req.url = 'http://kennethreitz.org/' 
  6.         req.data = {'life''42'
  7.      
  8.         pr = req.prepare() 
  9.         assert pr.url == req.url 
  10.         assert pr.body == 'life=42' 
  11.          
  12.     def test_path_is_not_double_encoded(self): 
  13.         request = requests.Request('GET'"http://0.0.0.0/get/test case").prepare() 
  14.      
  15.         assert request.path_url == '/get/test%20case 
  16.      
  17.     ... 
  18.      
  19.     def test_HTTP_200_OK_GET_ALTERNATIVE(self, httpbin): 
  20.         r = requests.Request('GET', httpbin('get')) 
  21.         s = requests.Session() 
  22.         s.proxies = getproxies() 
  23.  
  24.         r = s.send(r.prepare()) 
  25.  
  26.         assert r.status_code == 200 
  27.      
  28.     ef test_set_cookie_on_301(self, httpbin): 
  29.         s = requests.session() 
  30.         url = httpbin('cookies/set?foo=bar'
  31.         s.get(url) 
  32.         assert s.cookies['foo'] == 'bar' 
  • 对url进行校验,只需要对request进行prepare,这种情况下,请求并未发送,少了网络传输,测试用例会更迅速
  • 需要响应数据的情况,需要使用httbin构建真实的请求-响应数据

底层API测试

testserver构建一个简单的基于线程的tcp服务,这个tcp服务具有__enter__和__exit__方法,还可以当一个上下文环境使用。

  1. class TestTestServer: 
  2.  
  3.     def test_basic(self): 
  4.         """messages are sent and received properly""" 
  5.         question = b"success?" 
  6.         answer = b"yeah, success" 
  7.  
  8.         def handler(sock): 
  9.             text = sock.recv(1000) 
  10.             assert text == question 
  11.             sock.sendall(answer) 
  12.  
  13.         with Server(handler) as (host, port): 
  14.             sock = socket.socket() 
  15.             sock.connect((host, port)) 
  16.             sock.sendall(question) 
  17.             text = sock.recv(1000) 
  18.             assert text == answer 
  19.             sock.close() 
  20.      
  21.     def test_text_response(self): 
  22.         """the text_response_server sends the given text""" 
  23.         server = Server.text_response_server( 
  24.             "HTTP/1.1 200 OK\r\n" + 
  25.             "Content-Length: 6\r\n" + 
  26.             "\r\nroflol" 
  27.         ) 
  28.  
  29.         with server as (host, port): 
  30.             r = requests.get('http://{}:{}'.format(host, port)) 
  31.  
  32.             assert r.status_code == 200 
  33.             assert r.text == u'roflol' 
  34.             assert r.headers['Content-Length'] == '6' 

test_basic方法对Server进行基础校验,确保收发双方可以正确的发送和接收数据。先是客户端的sock发送question,然后服务端在handler中判断收到的数据是question,确认后返回answer,最后客户端再确认可以正确收到answer响应。test_text_response方法则不完整的测试了http协议。按照http协议的规范发送了http请求,Server.text_response_server会回显请求。下面是模拟浏览器的锚点定位不会经过网络传输的testcase:

  1. def test_fragment_not_sent_with_request(): 
  2.     """Verify that the fragment portion of a URI isn't sent to the server.""" 
  3.     def response_handler(sock): 
  4.         req = consume_socket_content(sock, timeout=0.5) 
  5.         sock.send( 
  6.             b'HTTP/1.1 200 OK\r\n' 
  7.             b'Content-Length: '+bytes(len(req))+b'\r\n' 
  8.             b'\r\n'+req 
  9.         ) 
  10.  
  11.     close_server = threading.Event() 
  12.     server = Server(response_handler, wait_to_close_event=close_server) 
  13.  
  14.     with server as (host, port): 
  15.         url = 'http://{}:{}/path/to/thing/#view=edit&token=hunter2'.format(host, port) 
  16.         r = requests.get(url) 
  17.         raw_request = r.content 
  18.  
  19.         assert r.status_code == 200 
  20.         headers, body = raw_request.split(b'\r\n\r\n', 1) 
  21.         status_line, headers = headers.split(b'\r\n', 1) 
  22.  
  23.         assert status_line == b'GET /path/to/thing/ HTTP/1.1' 
  24.         for frag in (b'view', b'edit', b'token', b'hunter2'): 
  25.             assert frag not in headers 
  26.             assert frag not in body 
  27.  
  28.         close_server.set() 

可以看到请求的path是 /path/to/thing/#view=edit&token=hunter2,其中 # 后面的部分是本地锚点,不应该进行网络传输。上面测试用例中,对接收到的响应进行判断,鉴别响应头和响应body中不包含这些关键字。

结合requests的两个层面的测试,我们可以得出第9个技巧:

9.构造模拟服务配合测试

小结

简单小结一下,从requests的单元测试实践中,可以得到下面9个技巧:

  1. 仅对public的接口进行测试
  2. 使用mock辅助单元测试
  3. 测试尽可能覆盖目标函数的所有分支
  4. 使用pytest.fixture复用被测试对象,使用pytest.mark.parametriz复用测试参数
  5. 可以从不同的层面对同一个对象进行单元测试
  6. 涉及环境变量的地方,可以使用上下文装饰器进行模拟多种环境变量
  7. 使用pytest.raises对异常进行捕获处理
  8. 使用IO模拟配合进行单元测试
  9. 构造模拟服务配合测试

参考链接

https://docs.python-requests.org/en/master/

https://httpbin.org

 

责任编辑:武晓燕 来源: 游戏不存在
相关推荐

2021-03-11 12:33:50

JavaPowerMock技巧

2017-01-14 23:42:49

单元测试框架软件测试

2016-12-13 10:06:25

编写Java单元测试技巧

2014-02-25 10:25:52

单元测试测试

2023-07-26 08:58:45

Golang单元测试

2022-12-08 08:01:02

Python测试单元

2011-05-16 16:52:09

单元测试彻底测试

2010-03-04 15:40:14

Python单元测试

2017-01-16 12:12:29

单元测试JUnit

2017-01-14 23:26:17

单元测试JUnit测试

2021-03-28 23:03:50

Python程序员编码

2022-05-12 09:37:03

测试JUnit开发

2011-06-14 15:56:42

单元测试

2020-08-18 08:10:02

单元测试Java

2011-07-04 18:16:42

单元测试

2020-05-07 17:30:49

开发iOS技术

2017-03-23 16:02:10

Mock技术单元测试

2010-08-27 09:11:27

Python单元测试

2021-05-05 11:38:40

TestNGPowerMock单元测试

2012-05-21 09:41:54

XcodeiOS单元测试
点赞
收藏

51CTO技术栈公众号