Aip接口自动化测试框架pytest+allure+request+jsonpath+excle

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

介绍

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

源码:https://gitee.com/HP_mojin/pytest_allure_request_20220811

Aip接口自动化测试框架pytest+allure+request+jsonpath+excle
Aip接口自动化测试框架pytest+allure+request+jsonpath+excle

; 软件架构

软件架构说明

项目结构:
├─common
│  │  all_path.py
│  │  api_request.py
│  │  assert_api.py
│  │  Bak_Rec_DB.py
│  │  condition.py
│  │  condition_b.py
│  │  db.py
│  │  exchange_data.py
│  │  logger.py
│  │  loggers.py
│  │  logger_bak.py
│  │  operation_excle.py
│  │  read_file.py
│  │  RemoteServe.py
│  │  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
│  info.txt
│  LICENSE
│  README.md
│  requirements.txt
│  requirements_all.txt
│  run.py
│  搭建服务器说明.txt

Aip接口自动化测试框架pytest+allure+request+jsonpath+excle

安装教程

  1. python10 需要安装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. 根据搭建后台服务说明搭建服务
1. 源码地址:https://gitee.com/wBekvam/vueShop-api-server
2. 文件中配置数据库 /config/default.json
3. 导入数据库 db/mysdb.sql
4. 项目所在主目录下
      设置淘宝镜像:npm config set registry https://registry.npm.taobao.org
      安装依赖: npm install
      启动项目 :node app.js
5.接口文档
https://gitee.com/wBekvam/vueShop-api-server/blob/master/api%E6%8E%A5%E5%8F%A3%E6%96%87%E6%A1%A3.md
6.登录接口
http://127.0.0.1:8888/api/private/v1/login
{
"username": "admin",
"password": "123456"
}

  1. 配置config\comfg.yaml参数
server:

  test: http://192.168.1.15:8888/api/private/v1
  dev: http://127.0.0.1:8888/api/private/v1
severity:
  case_severity: ["P1","P2","P3","P4"]
  cor_rel_case_severity:
    P1: blocker
    P2: critical
    P3: normal
    P4: minor
    P5: trivial

request_headers: {}
file_path:
  test_case: ./data/case_data.xlsx
  report: target/
  log: log/run{time}.log

email:

  user:  huo***@163.com

  password:  DY***XZMHK

  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", "1***18@qq.com"]
  title:  接口自动化测试报告(见附件)

  enclosures: ./report_zip/allure-report.zip

database:

  host: "127.0.0.1"
  port: 3306
  user: root

  password: root
  db_name:  mydb
  charset:  utf8mb4

  ssh_server:
    port: 22
    username: root
    password: root

    private_key_file: ''

    privat_passowrd: ''

    mysql_container:

    sql_data_file: ./backup_sqls/
  1. 配置数据库查询断言和备份恢复
    数据库查询断言配置:test_caes/test_001.py
import pytest,allure

from common.assert_api import AssertApi
from common.operation_excle import operation_excle
from common.api_request import Api_Request
from common.read_file import ReadFile

Sheet=operation_excle.read_excel(ReadFile.read_config('$..test_case'), ReadFile.read_config('$..case_severity'))

class Test():

    @pytest.mark.parametrize("case",Sheet)
    def test_001(self,case,get_db):

        response=(Api_Request.api_data(case))

        assert AssertApi().assert_api(response,case,get_db)

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


@pytest.fixture(scope='session',autouse=False)
def BakRecDB():

    host = ReadFile.read_config('$.database.host')
    ssh_port = ReadFile.read_config('$.database.ssh_server.port')
    ssh_user = ReadFile.read_config('$.database.ssh_server.username')
    ssh_pwd = ReadFile.read_config('$.database.ssh_server.password')
    sql_data_file = ReadFile.read_config('$.database.ssh_server.sql_data_file')

    BR=BakRecDB(host=host, port=ssh_port, username=ssh_user, password=ssh_pwd)
    BR.backups_sql()
    yield
    BR.recovery_sql()

  1. 运行run.py
import pytest,shutil,subprocess
from shutil import copy
from common.read_file import ReadFile
from common.send_email import EmailServe
from common.all_path import targetPath,Start_server_bat

def run():

    setting = ReadFile.read_config('$.email')
    try:
        shutil.rmtree('./target')
    except:
        pass

    pytest.main(['./test_caes','-vs',"--alluredir","target/allure-results"])
    allure_html = 'allure generate ./target/allure-results -o ./target/allure-report --clean'
    subprocess.call(allure_html, shell=True)

    copy(Start_server_bat, targetPath)

    Files_path='./target'
    EmailServe.send_email(setting,Files_path)

if __name__ == '__main__':
    run()

  1. 查看allure报告
    target/allue-report/index.html pycharm中右击 Run”index.html”
    Aip接口自动化测试框架pytest+allure+request+jsonpath+excle
    Aip接口自动化测试框架pytest+allure+request+jsonpath+excle
    Aip接口自动化测试框架pytest+allure+request+jsonpath+excle
    Aip接口自动化测试框架pytest+allure+request+jsonpath+excle

Aip接口自动化测试框架pytest+allure+request+jsonpath+excle

Aip接口自动化测试框架pytest+allure+request+jsonpath+excle
Aip接口自动化测试框架pytest+allure+request+jsonpath+excle

https://gitee.com/HP_mojin/pytest_allure_request_20220811 来了请点个 Star,谢谢!!

Original: https://blog.csdn.net/qq_42846555/article/details/126974870
Author: 默金……
Title: Aip接口自动化测试框架pytest+allure+request+jsonpath+excle

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/774034/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球