# test-engine **Repository Path**: jeanlv/test-engine ## Basic Information - **Project Name**: test-engine - **Description**: 开发测试执行驱动,包含API和Web UI测试 - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-01-25 - **Last Updated**: 2025-10-10 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 测试执行引擎 ## API测试引擎架构说明 技术栈: - Python3.13 - requests - requests_toolbelt - jsonpath - jmespath - pymysql - cx_Oracle - redis - rsa - faker 本次测试引擎开发不采用单元测试框架 ### API测试执行引擎架构说明 ![engine](./docs/images/APIEngine.png) 注:若没有采用单元测试框架进行执行引擎的开发,则不需要“用例转换器” ### 数据结构设计 #### 测试环境配置(env_config) ```json { "base_url": "http://127.0.0.1:8000", "headers": { "Content-Type": "application/json" }, "global_variables": { "username": "admin", "password": "123456", "uid": 12345678 }, "global_func": "print(\"func\")", "db": [ { "name": "mydb", "type": "mysql", "config": { "host": "127.0.0.1", "port": 3306, "user": "root", "password": "123456" } } ] } ``` - 接口基础url - 全局请求头 - 全局变量 - 全局工具函数 - 数据库配置 #### 用例基础格式(case_data) ```json [ { "name": "测试用例集合名称", "cases": [ { "info": { "title": "登录接口", "desc": "用于用户认证" }, "apis": { "url": "/api/users/login", "method": "post" }, "headers": { "Content-Type": "application/json" }, "request": { "params": {}, "data": {}, "json": { "username": "${{username}}", "password": "${{password}}" }, "files": {} }, "setup_script": "print('-----前置脚本------')\nmobile = global_func.random_mobile()\nprint('手机号码:', mobile)\n# 将手机号保存到全局变量中\ntest.save_global_variables('mobile', mobile)\nprint('当前的全局全局变量:', env)\n# print('当前的用例数据:', data)\n# test.del_global_variables('username')\n# 查询SQL语句\n# res = db.forest.execute(\"select * from forest_test.auth_user\")\n# print(res)", "teardown_script": "print('-----断言脚本------')\nres = response.json()\nprint('响应数据:', res)\ntoken = test.json_extract(res, '$..token')\nprint('token----', token)\n# 保存到当前测试环境数据中\ntest.save_global_variables('token', token)\nprint('当前测试环境中的数据', env)\n# 通过正则表达式提取数据\ntest.re_extract(res, r\"'username': '(.+?)'\")\n# 断言\nstatus = response.status_code\ntest.assertion('eq', 200, status)\n" } ] } ] ``` - 测试组件名称(业务流名称) - 用例集合(list) - 用例基础信息 - 用例名称 - 用例说明 - 接口信息 - 接口地址 - 请求方法 - 请求头信息 - 请求参数 - 查询参数 - 请求体参数 - 表单 - json - 文件上传 - 前置脚本 - 随机生成测试数据 - 查询数据库 - 接口加密 - 后置脚本 - 数据提取 - 对实际结果与预期结果断言 单个用例格式 ```json { "info": { "title": "登录接口", "desc": "用于用户认证" }, "apis": { "url": "/api/users/login", "method": "post" }, "headers": { "Content-Type": "application/json" }, "request": { "params": {}, "data": {}, "json": { "username": "${{username}}", "password": "${{password}}" }, "files": {} }, "setup_script": "print('-----前置脚本------')\nmobile = global_func.random_mobile()\nprint('手机号码:', mobile)\n# 将手机号保存到全局变量中\ntest.save_global_variables('mobile', mobile)\nprint('当前的全局全局变量:', env)\n# print('当前的用例数据:', data)\n# test.del_global_variables('username')\n# 查询SQL语句\n# res = db.forest.execute(\"select * from forest_test.auth_user\")\n# print(res)", "teardown_script": "print('-----断言脚本------')\nres = response.json()\nprint('响应数据:', res)\ntoken = test.json_extract(res, '$..token')\nprint('token----', token)\n# 保存到当前测试环境数据中\ntest.save_global_variables('token', token)\nprint('当前测试环境中的数据', env)\n# 通过正则表达式提取数据\ntest.re_extract(res, r\"'username': '(.+?)'\")\n# 断言\nstatus = response.status_code\ntest.assertion('eq', 200, status)\n" } ``` ### 使用说明 #### 执行函数说明 #### 数据结构说明 #### 前后置脚本 ## Web UI测试引擎说明 ### Web UI测试执行引擎架构说明 技术栈: - Python3.13 - playwright / python -m playwright install(安装浏览器驱动) ### 用例数据格式设计 #### 执行测试环境数据的配置(env_config) ```json { "is_debug": false, "browser_type": "chromium", "host": "127.0.0.1:9222", "global_variable": { "username":"admin", "password":"123456" } } ``` - 是否是无头模式运行(false:否,启动浏览器, true:是,不启动浏览器) - 浏览器驱动类型 - 浏览器驱动地址 - 全局变量 #### 测试用例基础格式(suites) ##### 测试套件数据格式 ```json { "id": "编号", "name": "登录功能测试", "desc": "用户登录操作", "setup_step": [ { "operate": "打开浏览器", "method": "open_browser", "params": { "browser_type": "chromium" } }, { "operate": "打开网页", "method": "open_url", "params": { "url": "http://localhost:8088/login" } }, { "operate": "等待2秒", "method": "sleep", "params": { "timeout": 2000 } } ], "cases": [] } ``` - 测试套件编号 - 测试套件名称 - 测试套件说明 - 测试套件公共前置步骤 - 用例数据 ##### 用例数据格式 ```json { "id": "编号1", "name": "登录密码错误", "skip": false, "steps": [ { "operate": "输入账号", "method": "fill_value", "params": { "locator": "", "value": "" } }, { "operate": "输入密码", "method": "fill_value", "params": { "locator": "", "value": "" } }, { "operate": "点击登录", "method": "click", "params": { "locator": "" } } ] } ``` - 测试用例编号 - 测试用例名称 - 是否跳过 - 测试用例具体步骤 ##### 测试步骤格式 ```json { "operate": "打开网页", "method": "open_url", "params": { "url": "https://www.ketangpai.com/#/login" } } ``` - 测试步骤操作名称 - 测试步骤操作方法 - 测试步骤参数 ## 打包Python项目 1. 安装Python构建工具 ```sh python -m pip install --upgrade build ``` 2. 进入到打包的项目目录下 3. 项目目录下新增配置文件`pyproject.toml` `pyproject.toml `文件是用来配置项目的打包信息的。在文件中,需要指定项目的名称、版本号、作者信息、描述以及其他一些相关信息。 配置文件示例: ```toml # 用于定义项目的构建系统配置。 [build-system] # 指定构建系统工具的要求。使用 "hatchling" 作为构建系统工具。 requires = ["hatchling"] # 指定构建系统的后端实现。 build-backend = "hatchling.build" # 项目相关的各种信息。 [project] # 项目的名称。 name = "TestEngine" # 项目的版本号。 version = "1.0.0" # 作者信息列表,包含作者的姓名和电子邮件。 authors = [ { name = "jeanlv", email = "lvjing0705@126.com" }, ] # 项目的描述。 description = "自主研发测试引擎" # 项目的 README 文件的名称。 readme = "README.md" # 许可证信息,包含许可证文件的名称。 license = { file = "LICENSE" } # 项目所需的 Python 版本。 requires-python = ">=3.11.3" # 项目的分类器,用于指定项目的分类标签。例如编程语言、许可证、操作系统等。 classifiers = [ "Programming Language :: Python :: 3", "License :: OSI Approved :: MIT License", "Operating System :: OS Independent", ] # 项目的依赖列表,指定其他项目或库的依赖关系。 dependencies = [ "requests==2.32.3", "requests-toolbelt ==1.0.0", "jsonpath==0.82.2", "jmespath==1.0.1", "pymsql==1.1.1", "cx_Oracle==8.3.0", "redis==5.2.1", "rsa==4.9", "faker==33.3.1" ] [tool.hatch.build.targets.wheel] packages = ["core"] # 项目的相关网址 [project.urls] # 项目的主页网址。 "Homepage" = "http://xxx.xxx.xxx" ``` 4. 项目目录下新增文件`LICENSE` LICENSE是许可文件,参考以下输入既可 ``` Copyright (c) 2018 The Python Packaging Authority 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. ``` 5. 执行打包 ```sh python -m build ```