1 Star 0 Fork 5

zhangchi / AutoUAPI

forked from guanfuchang / AutoUAPI 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
MIT

AutoUAPI

介绍

auto-api 将用户端API接口HAR流量包转化成pytest脚本用例,进行流量回放。

项目背景

当我们需要对一个前端项目进行接口自动化回归测试时,无论是写接口脚本还是使用postman,jmeter或者是使用一些自动化平台,都需要做大量的接口配置工作。

为了减少测试人员代码编写或接口配置工作,基于pytest脚本模板,编写了本项目,目的的实现:

  1. 直接通过抓包文件HAR生成可执行的pytest用例文件。
  2. 可充分利用pytest的特性,可灵活编写代码,类似插件一样插入到pytest用例代码中。

安装教程

  1. git clone https://gitee.com/guanfuchang/auto-uapi.git
  2. pip install -r requirements.txt
  3. python run_all.py

使用说明

项目目录结构
C:.                                                                               
│  conftest.py                                                                     
│  requirements.txt                                 
│  run_all.py                           # 运行主程序                      
│  settings.py                                      

├─case_datas                                                                     
│  │                                                
│  ├─case_har                           # 将Fiddler中导出的xx.har文件放到这里                      
│  │      fht.har                                   
│  │                                                
│  └─case_py                            # 运行generate_case_confLists.py 脚本将xx.har解析成用例配置列表xx.py                                       

├─test_suits                            # 运行 generate_case_pytest.py 脚本将xx.py 解析生成pytest用例文件test_xx.py                                                               
└─utils                                             
    │  case_handler.py                  # 请求,断言处理          
    │  generate_case_confLists.py       # 将HAR文件解析成用例配置列表          
    │  generate_case_pytest.py          # 将用例配置列表解析成pytest运行文件                
                                                    
自动化过程分解
  1. step1:通过Fiddler等工具,导出接口HAR文件xxx.har
  • 备注:建议使用Fiddler,可设置过滤器,方便过滤和删除不必要的接口
  1. step2:通过脚本解析HAR文件,生成测试用例配置列表
  • 备注:举例如将index.har文件存到项目case_datas/case_har/目录下,然后修改generate_case_confLists.py中的har文件路径. 运行后会生成用例配置文件case_datas/case_py/xxx.py,内容类似如下:
case_list = [
    {
        "name": "mch_web_v1_login",
        "method": "POST",
        "api": "/mch_web/v1/login",
        "playload": {
            "uname": "fht20210621",
            "passwd": "fht20210621"
        },
        "assert_list": [
            [
                "resp.status_code==200",
                "接口响应状态码不等于200"
            ]
        ]
    },
    ...
]
  • 备注:通过修改配置,可实现python代码参数化,变量自定义等功能
  1. step3:通过脚本解析步骤2中生成的测试用例列表,生成可执行的pytest用例脚本
  • 备注:修改generate_case_pytest.py中的case_list和py_test_file,运行后会生成pytest用例文件如test_suits/test_xxx.py 内容类似如下:
from utils.case_handler import *


class TestIndex(object):

    def test_mch_web_v1_login(self, login_session):
        api = "/mch_web/v1/login"
        playload = {'uname': 'fht20210621', 'passwd': 'fht20210621'}
        method = "POST"
        resp = req(api=api, session=login_session, data=playload, method=method)
        validate(resp, [['resp.status_code==200', '接口响应状态码不等于200']])

    ...
  1. step4:运行pytest用例脚本,生成测试报告
DEMO 实例过程

参与贡献

  1. Fork 本仓库
  2. 新建 Feat_xxx 分支
  3. 提交代码
  4. 新建 Pull Request
MIT License Copyright (c) 2021 guanfuchang Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

简介

auto-api 将用户端API接口HAR流量包转化成pytest脚本用例,进行流量回放。 展开 收起
Python
MIT
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Python
1
https://gitee.com/zczc8/auto-uapi.git
git@gitee.com:zczc8/auto-uapi.git
zczc8
auto-uapi
AutoUAPI
master

搜索帮助

53164aa7 5694891 3bd8fe86 5694891