V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
爱意满满的作品展示区。
iyaozhen
V2EX  ›  分享创造

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

  •  2
     
  •   iyaozhen ·
    iyaozhen · 2017-10-16 14:30:48 +08:00 · 3838 次点击
    这是一个创建于 2356 天前的主题,其中的信息可能已经有所发展或是发生改变。

    py_http_api_test

    Support Python Version Build Status codecov

    一个基于 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 )解析结果,实践中主要有如下优点:

    • 统一规范,方便协作开发,更多精力放在业务上
    • 字段获取不用判空,代码更简洁和健壮
    • 对于复杂的 json 数据和逻辑,也能通过 jmespath 语法描述清楚,代码更简洁,同时也为之后扩展(如接口测试平台)打下基础(可存储)

    更多详见: https://github.com/iyaozhen/py-http-test-framework

    原谅我粗暴的先粘贴了一坨 READEME,主要是想先 show code 再说事情。想了解下各位大佬平常怎么做接口测试的,交流交流。感觉我这个有点井底之蛙闭门造轮子了。

    6 条回复    2017-10-18 11:35:01 +08:00
    lancegin
        1
    lancegin  
       2017-10-16 17:38:35 +08:00
    QA 大佬 (´▽`)
    eason622
        2
    eason622  
       2017-10-16 17:59:20 +08:00 via iPhone
    正好需要 回家看看 0.0 真大佬
    huip
        3
    huip  
       2017-10-18 08:30:53 +08:00
    可以试试 node.js 的 supertest
    WantMarryGakki
        4
    WantMarryGakki  
       2017-10-18 09:57:43 +08:00
    我写了个基于 request+unittest 的。
    iyaozhen
        5
    iyaozhen  
    OP
       2017-10-18 11:34:48 +08:00 via Android
    @huip js 这一块生态更好。但我们这边用 js 的少-_-||
    iyaozhen
        6
    iyaozhen  
    OP
       2017-10-18 11:35:01 +08:00 via Android
    @WantMarryGakki 哈哈,都差不多
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3791 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 10:21 · PVG 18:21 · LAX 03:21 · JFK 06:21
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.