一个基于 Python Nose 的轻量级 HTTP Api 测试框架

2017-10-16 14:30:48 +08:00
 iyaozhen

py_http_api_test

一个基于 Python Nose 的轻量级 HTTP Api 测试框架

设计思路

主要解决了 HTTP 接口测试中的三大问题:

  1. HTTP 接口多种多样,测试程序如何统一?
  2. 测试程序如何规范组织?
  3. 接口间有上下文依赖,如何解决?

详见:《使用 Python nose 组织 HTTP 接口测试》

使用方法

下载代码,将 py_http_api_test 文件夹(模块)复制到项目中。

然后和写普通单测 case 一样,测试类需要继承 HttpTest ( from py_http_api_test.http_test import HttpTest ),HttpTest 主要是初始化了一个 http_session ( Http 会话) 对象和注入了配置文件,一个测试方法完成一个接口测试。

示例( demo/contacts_api_test.py ):

#!/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 再说事情。想了解下各位大佬平常怎么做接口测试的,交流交流。感觉我这个有点井底之蛙闭门造轮子了。

3878 次点击
所在节点    分享创造
6 条回复
lancegin
2017-10-16 17:38:35 +08:00
QA 大佬 (´▽`)
eason622
2017-10-16 17:59:20 +08:00
正好需要 回家看看 0.0 真大佬
huip
2017-10-18 08:30:53 +08:00
可以试试 node.js 的 supertest
WantMarryGakki
2017-10-18 09:57:43 +08:00
我写了个基于 request+unittest 的。
iyaozhen
2017-10-18 11:34:48 +08:00
@huip js 这一块生态更好。但我们这边用 js 的少-_-||
iyaozhen
2017-10-18 11:35:01 +08:00
@WantMarryGakki 哈哈,都差不多

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/398010

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX