5 Star 62 Fork 17

默金 / Aip接口自动化测试pytest+allure+request+jsonpath+excle

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

Aip接口自动化测试pytest+allure+request+jsonpath+excle/yaml

服务端项目(测试项目)

https://gitee.com/y_project/RuoYi-Vue 这是非常火爆的SpringBoot+Vue3前后端分离项目,作为接口自动化测试的服务端
部署方法:
官方部署文档:http://doc.ruoyi.vip/ruoyi-vue
部署视频:https://www.bilibili.com/video/BV1vX4y1Z7k8

Aip接口自动化测试框架介绍

Aip接口自动化测试python+pytest+allure+request+jsonpath+excle/yaml,一个支持仅编辑Excle/yaml就可实现的接口自动化测试框架。
1.编辑Excle做接口数据驱动的数据源,新增yaml添加用例,yaml,excle混和都支持
2.接口上下游数据依赖处理
3.用例中字符串中写入函数名,获取随机值做新增接口数据
4.数据库查询断言 ,数据库提取参数
5.数据库初始化备份恢复
5.sql查询数据做查询断言和请求数据
6.使用pytest+allure框架
7.测试完成后打包allure报告为zip包通过邮件发送
8.一键切换运行环境,dev/test/prod

img.png

img1.png

软件架构

软件架构说明

项目结构:   
├─common    #公共模块类      
│  │  all_path.py  #定义文件路径      
│  │  api_request.py   #发送请求      
│  │  assert_api.py    #请求断言      
│  │  backup_recovery.py    #备份恢复操作逻辑      
│  │  condition.py     #跳过用例逻辑          
│  │  db.py             #数据库读取操作      
│  │  exchange_data.py  #接口参数的数据替换和提取      
│  │  logger.py         #系统日志打印        
│  │  operation_excle.py #读取写入excel      
│  │  read_file.py      #读取配置文件yaml       
│  │  send_email.py     #发送邮件      
│  │__init__.py      

├─config  #配置文件和相关资源      
│      1.jpg      
│      3.jpg      
│      config.yaml            
│      mydb.sql      
│      mydb_bak.sql      
│      Start_server.bat      

├─data   #测试数据用例表格      
│      case_data.xlsx      

├─logs  #日志打印文件      
│      20220921.log      
│      __init__.py      

├─report_zip  #测试完成后测试报告压缩包发送邮件      
│      allure-report.zip      
│      __init__.py      

├─target   #运行测试保持的测试报告路径      
├─test_caes   #测试用例文件夹      
│  │  test_001.py  #执行测试用例      
│  └─__init__.py      

│─解析    #解析框架重要的模块      
│        Faker库造测试数据.py      
│        flask开发2个http接口.py      
│        te_001.py      
│        te_09.py      
│        参数依赖01-jsonpath提取参数.py      
│        参数依赖02-locals()获取本地变量 及exec()执行字符串中的代码.py      
│        参数依赖03-Template 使用,替换参数.py      
│        参数依赖04-框架中替换参数及执行字符串中方法取值.py      
│        发送请求01-get方法中url参数拼接和写到请求体中的理解.py      
│        发送请求02-请求参类型params,json,data 含义.py      
│        发送请求03-理解两种发送请求方法.py      
│        发送请求04-封装请求参数转化的理解.py      
│        备份恢复sql-FTP上传文件.py      
│        备份恢复sql-ssh执行命令.py      
│        继承方法以及传参.py      
│        邮件.py      

│  conftest.py  #pytest conftest      
│  info.txt    #项目结构      
│  LICENSE      
│  README.md   #项目说明      
│  requirements.txt #项目依赖            
│  requirements_all.txt  #本地环境依赖      
│  run.py   #运行入口      
│  搭建服务器说明.txt  #  搭建接口服务说明      

img__1.png

安装教程

  1. python3.10.7 需要安装pytest==7.1.3
  2. 安装依赖环境 pip install -r requirements.txt -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
  3. 安装allrue报告allure-2.18.1.zip
  4. 安装java环境,allure需要java环境

使用说明

  1. 根据搭建后台服务说明搭建服务

    部署方法:     
    官方部署文档:http://doc.ruoyi.vip/ruoyi-vue     
    部署视频:https://www.bilibili.com/video/BV1vX4y1Z7k8     
  2. 配置config\comfg.yaml参数

    server:
      # 本地接口服务http://192.168.1.234:8056/prod-api/
    
      dev: http://127.0.0.1:8083
      test: http://192.168.1.182:8056/prod-api
      prod: http://192.168.1.234:8056/prod-api/
    
    
    
    Operations_db: # 操作数据库
      assert_db: True  #  True  启用数据库查询断言   False  不启用
      backup: True   #启动测试前启动备份数据库  数据库服务器 linux,windows都适用
      recovery: True  #测试完成后恢复备份数据库  数据库服务器 linux,windows都适用
    
    
    severity:
      case_severity: ["P1","P2","P3","P4"]#用例等级 "["P1","P2","P3","P4","P5"]"  设置需要测试用例等级
      cor_rel_case_severity: #用例等级和allure报告优先级的对应关系
        P1: blocker
        P2: critical
        P3: normal
        P4: minor
        P5: trivial
    
    # 基准的请求头信息
    request_headers: {}
    
    # 基准存放提取参数的池子
    extra_pool: {"token":'',"username":"admin","password":"admin123"}
    
    # 基准的请求参数
    request_parameters: {}
    
    test_case_type:  # 用例读取的来源  为True读取文件类型,为False不读取,如excel 和yaml都为True时 读取累加
      dev:
        - read: True  #  True  读取excle文件中的用例   False  不读取
          test_case: ./data/env_dev/case_excle/
          file: xlsx
          order: 1   #顺序
    
        - read: True  #  True  False  读取yaml文件中的用例
          test_case: ./data/env_dev/case_yaml/
          file: yaml
          order: 0  #顺序
    
    
    
      test:
        - read: True  #  True  读取excle文件中的用例   False  不读取
          test_case: ./data/env_test/case_excle/
          file: xlsx
          order: 1   #数字排序顺序
    
        - read: True  #  True  False  读取yaml文件中的用例
          test_case: ./data/env_test/case_yaml/
          file: yaml
          order: 0  #数字排序顺序
    
      prod:
        - read: False  #  True  读取excle文件中的用例   False  不读取
          test_case: ./data/env_prod/case_excle/
          file: xlsx
          order: 1   #顺序
    
        - read: True  #  True  False  读取yaml文件中的用例
          test_case: ./data/env_prod/case_yaml/
          file: yaml
          order: 0  #顺序
    
    
    
    email:
      # 发件人邮箱
      user:  huoyan_hyh@163.com
      # 发件人邮箱授权码
      password:  DYTKTEPXOZXXZMHK
      # 邮箱host
      host:  smtp.163.com
      contents:  解压allure-report.zip(接口测试报告)后,进入解压目录,运行Start_server.bat(需要有python环境),在浏览器输入http://127.0.0.1:5188/allure-report 即可查看测试报告。出现“Serving HTTP on 0.0.0.0 port 5188 (http://0.0.0.0:5188/) ...”表示服务启动完成,可以输入 http://127.0.0.1:5188/allure-report 地址查看报告
      # 收件人邮箱
      addressees: ["397135766@qq.com", "huangpeng245@163.com"] #["397135766@qq.com", "178999718@qq.com"]
      title:  接口自动化测试报告(见附件)
      # 附件
      enclosures: ./report_zip/allure-report.zip
    
    # 数据库校验- mysql
    database:
      #host: "192.168.1.183" #localhost  127.0.0.1
      dev: #开发环境数据库
        db_type: postgresql   #mysql,postgresql 数据库类型
        data:
          host: "192.168.1.234" #localhost  127.0.0.1
          port: 5432
          user: postgres
          # 不用''会被解析成int类型数据
          password: root
          database: mydb
    
    
    
      test: #测试环境数据库
        db_type: mysql   #mysql,postgresql 数据库类型
        data:
          host: "127.0.0.1" #localhost  127.0.0.1
          port: 3306
          user: root
          # 不用''会被解析成int类型数据
          password: root
          database: ry-vue
    
    
    
      prod: #测试环境数据库
        db_type: mysql   #mysql,postgresql 数据库类型
        data:
          host: "192.168.1.234" #localhost  127.0.0.1
          port: 3306
          user: root
          # 不用''会被解析成int类型数据
          password: '123456'
          database: ry-vue
  3. 配置数据库查询断言和备份恢复
    数据库查询断言配置:test_caes/test_001.py

     import pytest,allure
     from common.assert_api import AssertApi
     from common.api_request import Api_Request
     from common.read_exce_yaml_caes import get_yaml_excle_caes
     
     #@allure.epic(ReadFile.read_config("$.project_name"))  # 项目名称
     class Test():
     
         @pytest.mark.parametrize("case",get_yaml_excle_caes('test'))
         @allure.step
         def test_001(self,case,get_db,env_url):
     
             response=(Api_Request.api_data(case,env_url))
     
             assert AssertApi().assert_api(response,case,get_db)
    
    

    备份恢复数据库:conftest.py
    autouse=False 为True时开启数据库备份恢复功能,为False时不开启备份恢复功能

    #备份恢复数据库
    @pytest.fixture(scope="session", autouse=True)
    def bac_rec(Acmdopt):
        db_data = dict(ReadFile.read_config('$.database.%s'%Acmdopt))
        BR = BackupRecovery(db_data['data'])
        db_bak_path = './config/ry-vue_bak.sql'  # 当前数据库备份文件
        db_re_path = './config/ry-vue_re.sql'  # 初始化好测试数据的数据库sql文件
        backup = ReadFile.read_config('$.Operations_db.backup')
        if backup:
            BR.backup(db_bak_path)
            BR.recovery(db_re_path)
        yield
        recovery = ReadFile.read_config('$.Operations_db.recovery')
        if recovery:
            BR.recovery(db_bak_path)
    
    
  4. 运行run.py

    import pytest,shutil,subprocess
    from shutil import copy
    from common.read_file import ReadFile
    from common.send_email import EmailServe
    from common import all_path
    from common.exchange_data import ExchangeData
    
    def run():
        setting = dict(ReadFile.read_config('$.email')) #获取邮件相关配置信息
    
        try:
            shutil.rmtree(all_path.targetPath) #删除allure历史数据
        except:
            pass
    
        # --reruns = 3  失败重试
        pytest.main(['./test_caes', '-vs', "--env=test","--reruns=3", "--alluredir", "target/allure-results"])  # pytest测试框架主程序运行
    
        copy(all_path.Start_server_bat, all_path.targetPath) #拷贝 启动服务器脚本(config/Start_server.bat),由config目录拷贝到target目录下进行压缩打包发送邮件
        copy(all_path.environmentPath, all_path.allure_results)  # 拷贝 环境配置文件
    
        allure_html = 'allure generate ./target/allure-results -o ./target/allure-report --clean'  # 生成allure的html报告
        subprocess.call(allure_html, shell=True)  # 生成allure的html报告
    
        EmailServe.send_email(setting,all_path.targetPath,ExchangeData.get_pytest_summary()) #发送邮件
    
    if __name__ == '__main__':
        run()
    
  5. 查看allure报告
    target/allue-report/index.html pycharm中右击 Run"index.html" img_1.png img_2.png img_3.png img_4.png img_5.png img_6.png img_11.png

联系方式

img_12.png img_9.png

备注:如群码过期请添加微信,备注 “加群” 拉入群

Academic Free License (“AFL”) v. 3.0 This Academic Free License (the "License") applies to any original work of authorship (the "Original Work") whose owner (the "Licensor") has placed the following licensing notice adjacent to the copyright notice for the Original Work: Licensed under the Academic Free License version 3.0 1) Grant of Copyright License. Licensor grants You a worldwide, royalty-free, non-exclusive, sublicensable license, for the duration of the copyright, to do the following: a) to reproduce the Original Work in copies, either alone or as part of a collective work; b) to translate, adapt, alter, transform, modify, or arrange the Original Work, thereby creating derivative works ("Derivative Works") based upon the Original Work; c) to distribute or communicate copies of the Original Work and Derivative Works to the public, under any license of your choice that does not contradict the terms and conditions, including Licensor’s reserved rights and remedies, in this Academic Free License; d) to perform the Original Work publicly; and e) to display the Original Work publicly. 2) Grant of Patent License. Licensor grants You a worldwide, royalty-free, non-exclusive, sublicensable license, under patent claims owned or controlled by the Licensor that are embodied in the Original Work as furnished by the Licensor, for the duration of the patents, to make, use, sell, offer for sale, have made, and import the Original Work and Derivative Works. 3) Grant of Source Code License. The term "Source Code" means the preferred form of the Original Work for making modifications to it and all available documentation describing how to modify the Original Work. Licensor agrees to provide a machine-readable copy of the Source Code of the Original Work along with each copy of the Original Work that Licensor distributes. Licensor reserves the right to satisfy this obligation by placing a machine-readable copy of the Source Code in an information repository reasonably calculated to permit inexpensive and convenient access by You for as long as Licensor continues to distribute the Original Work. 4) Exclusions From License Grant. Neither the names of Licensor, nor the names of any contributors to the Original Work, nor any of their trademarks or service marks, may be used to endorse or promote products derived from this Original Work without express prior permission of the Licensor. Except as expressly stated herein, nothing in this License grants any license to Licensor’s trademarks, copyrights, patents, trade secrets or any other intellectual property. No patent license is granted to make, use, sell, offer for sale, have made, or import embodiments of any patent claims other than the licensed claims defined in Section 2. No license is granted to the trademarks of Licensor even if such marks are included in the Original Work. Nothing in this License shall be interpreted to prohibit Licensor from licensing under terms different from this License any Original Work that Licensor otherwise would have a right to license. 5) External Deployment. The term "External Deployment" means the use, distribution, or communication of the Original Work or Derivative Works in any way such that the Original Work or Derivative Works may be used by anyone other than You, whether those works are distributed or communicated to those persons or made available as an application intended for use over a network. As an express condition for the grants of license hereunder, You must treat any External Deployment by You of the Original Work or a Derivative Work as a distribution under section 1(c). 6) Attribution Rights. You must retain, in the Source Code of any Derivative Works that You create, all copyright, patent, or trademark notices from the Source Code of the Original Work, as well as any notices of licensing and any descriptive text identified therein as an "Attribution Notice." You must cause the Source Code for any Derivative Works that You create to carry a prominent Attribution Notice reasonably calculated to inform recipients that You have modified the Original Work. 7) Warranty of Provenance and Disclaimer of Warranty. Licensor warrants that the copyright in and to the Original Work and the patent rights granted herein by Licensor are owned by the Licensor or are sublicensed to You under the terms of this License with the permission of the contributor(s) of those copyrights and patent rights. Except as expressly stated in the immediately preceding sentence, the Original Work is provided under this License on an "AS IS" BASIS and WITHOUT WARRANTY, either express or implied, including, without limitation, the warranties of non-infringement, merchantability or fitness for a particular purpose. THE ENTIRE RISK AS TO THE QUALITY OF THE ORIGINAL WORK IS WITH YOU. This DISCLAIMER OF WARRANTY constitutes an essential part of this License. No license to the Original Work is granted by this License except under this disclaimer. 8) Limitation of Liability. Under no circumstances and under no legal theory, whether in tort (including negligence), contract, or otherwise, shall the Licensor be liable to anyone for any indirect, special, incidental, or consequential damages of any character arising as a result of this License or the use of the Original Work including, without limitation, damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses. This limitation of liability shall not apply to the extent applicable law prohibits such limitation. 9) Acceptance and Termination. If, at any time, You expressly assented to this License, that assent indicates your clear and irrevocable acceptance of this License and all of its terms and conditions. If You distribute or communicate copies of the Original Work or a Derivative Work, You must make a reasonable effort under the circumstances to obtain the express assent of recipients to the terms of this License. This License conditions your rights to undertake the activities listed in Section 1, including your right to create Derivative Works based upon the Original Work, and doing so without honoring these terms and conditions is prohibited by copyright law and international treaty. Nothing in this License is intended to affect copyright exceptions and limitations (including “fair use” or “fair dealing”). This License shall terminate immediately and You may no longer exercise any of the rights granted to You by this License upon your failure to honor the conditions in Section 1(c). 10) Termination for Patent Action. This License shall terminate automatically and You may no longer exercise any of the rights granted to You by this License as of the date You commence an action, including a cross-claim or counterclaim, against Licensor or any licensee alleging that the Original Work infringes a patent. This termination provision shall not apply for an action alleging patent infringement by combinations of the Original Work with other software or hardware. 11) Jurisdiction, Venue and Governing Law. Any action or suit relating to this License may be brought only in the courts of a jurisdiction wherein the Licensor resides or in which Licensor conducts its primary business, and under the laws of that jurisdiction excluding its conflict-of-law provisions. The application of the United Nations Convention on Contracts for the International Sale of Goods is expressly excluded. Any use of the Original Work outside the scope of this License or after its termination shall be subject to the requirements and penalties of copyright or patent law in the appropriate jurisdiction. This section shall survive the termination of this License. 12) Attorneys’ Fees. In any action to enforce the terms of this License or seeking damages relating thereto, the prevailing party shall be entitled to recover its costs and expenses, including, without limitation, reasonable attorneys' fees and costs incurred in connection with such action, including any appeal of such action. This section shall survive the termination of this License. 13) Miscellaneous. If any provision of this License is held to be unenforceable, such provision shall be reformed only to the extent necessary to make it enforceable. 14) Definition of "You" in This License. "You" throughout this License, whether in upper or lower case, means an individual or a legal entity exercising rights under, and complying with all of the terms of, this License. For legal entities, "You" includes any entity that controls, is controlled by, or is under common control with you. For purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. 15) Right to Use. You may use the Original Work in all ways not otherwise restricted or conditioned by this License or by law, and Licensor promises not to interfere with or be responsible for such uses by You. 16) Modification of This License. This License is Copyright © 2005 Lawrence Rosen. Permission is granted to copy, distribute, or communicate this License without modification. Nothing in this License permits You to modify this License as applied to the Original Work or to Derivative Works. However, You may modify the text of this License and copy, distribute or communicate your modified version (the "Modified License") and apply it to other original works of authorship subject to the following conditions: (i) You may not indicate in any way that your Modified License is the "Academic Free License" or "AFL" and you may not use those names in the name of your Modified License; (ii) You must replace the notice specified in the first paragraph above with the notice "Licensed under <insert your license name here>" or with a notice of your own that is not confusingly similar to the notice in this License; and (iii) You may not claim that your original works are open source software unless your Modified License has been approved by Open Source Initiative (OSI) and You comply with its license review and certification process.

简介

Aip接口自动化测试python+pytest+allure+request+jsonpath+excle,一个支持仅编辑Excle就可实现的接口自动化测试框架。 展开 收起
Python 等 5 种语言
AFL-3.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
Python
1
https://gitee.com/HP_mojin/pytest_allure_request_20220811.git
git@gitee.com:HP_mojin/pytest_allure_request_20220811.git
HP_mojin
pytest_allure_request_20220811
Aip接口自动化测试pytest+allure+request+jsonpath+excle
master

搜索帮助