
一个基于 Python Nose 的轻量级 HTTP Api 测试框架
主要解决了 HTTP 接口测试中的三大问题:
详见:《使用 Python nose 组织 HTTP 接口测试》
下载代码,将 py_http_api_test 文件夹(模块)复制到项目中。
然后和写普通单测 case 一样,测试类需要继承 HttpTest ( from py_http_api_test.http_test import HttpTest ),HttpTest 主要是初始化了一个 http_session ( Http 会话) 对象和注入了配置文件,一个测试方法完成一个接口测试。
#!/usr/bin/env python # -*- coding: utf-8 -*- from nose.tools import assert_greater_equal from nose.tools import assert_is_not_none from nose.tools import eq_ from nosedep import depends from py_http_api_test.http_test import HttpTest from py_http_api_test.jmespath_custom import search as jq_ class ContactsApiTest(HttpTest): """ 通讯录接口测试 https://open-doc.dingtalk.com/docs/doc.htm """ access_token = None def test_gettoken(self): """ 获取 access_token :return: """ params = { 'corpid': self.__class__.config['corpid'], 'corpsecret': self.__class__.config['corpsecret'] } respOnse= self.__class__.http_session.request( 'GET', self.__class__.config['dingtalk_oapi'] + '/gettoken', params=params ).json() self.__class__.access_token = jq_('access_token', response) eq_(jq_('errcode', response), 0) assert_is_not_none(self.__class__.access_token) @depends(after='test_gettoken') def test_department_list(self): """ 获取部门列表 :return: """ params = { 'access_token': self.__class__.access_token, 'id': self.__class__.config['department_parentid'] } respOnse= self.__class__.http_session.request( 'GET', self.__class__.config['dingtalk_oapi'] + '/department/list', params=params ).json() eq_(jq_('errcode', response), 0) assert_greater_equal(len(jq_('department', response)), 1) 因为框架基于 nose,所以还可以直接使用需要的 nose 插件,比如 demo 中的 nosedep 插件。
为了运行方便,建议使用 nose.cfg 来简化运行时的参数。
[nosetests] verbosity=2 nocapture=1 with-nosedep=1 no-byte-compile=1 [others] env=demo/online.yaml 这样就能使用 nosetests -c demo/nose.cfg demo 运行测试 case 了,运行结果:
$ nosetests -c demo/nose.cfg demo 获取 access_token ... ok 获取部门列表 ... ok ---------------------------------------------------------------------- Ran 2 tests in 0.275s OK 对于接口返回的 json 参数建议使用 jmespath.py( json 中的 xpath )解析结果,实践中主要有如下优点:
更多详见: https://github.com/iyaozhen/py-http-test-framework
原谅我粗暴的先粘贴了一坨 READEME,主要是想先 show code 再说事情。想了解下各位大佬平常怎么做接口测试的,交流交流。感觉我这个有点井底之蛙闭门造轮子了。
1 lancegin 2017 年 10 月 16 日 QA 大佬 (`) |
2 eason622 2017 年 10 月 16 日 via iPhone 正好需要 回家看看 0.0 真大佬 |