接口自动化测试框架
Project description
1. 文档说明
本文档主要用于接口自动化测试框架说明以及使用。
1.1 环境准备
-
IDE:Pycharm 等;
-
Python 3.7以上;
-
引用类库,dg-itest; 命令:
pip install dg-itest
-
allure 本地环境配置
step 1 pip install allure-pytest
step 2 下载Allure版本:Download 并配置到PATH路径下 对应家目录下,.bash_profile
配置如下:# Setting PATH for Python 3.9 # The original version is saved in .bash_profile.pysave PATH="/Library/Frameworks/Python.framework/Versions/3.9/bin:${PATH}" export PATH # Setting ALLURE PATH for ALLURE export ALLURE=/Users/yaitzayoung/Downloads/Lib/allure-2.7.0/bin export PATH=$PATH:$ALLURE
step 3 allure(依赖jdk环境1.8+) Download
2. 目录结构
├── dg-itest
│ ├── test_res #测试用资源文件夹,需要上传文件时,对应文件归档至该目录
│ ├── test_report #测试报告生成目录,系统自动生成
│ └── test_src #测试用例归档目录,测试用例文件归档目录
2.1 test_res: 测试用资源文件夹
说明:测试资源文件归档至该目录下;例如:相关上传文件等。
2.5 test_src: 测试用例归档目录
说明:测试用例归档目录,针对不同的测试方案归档至相应目录,测试场景建议新建对应的测试类对应或者文件夹。目前支持.yml,.xls,.xlsx,.py文件类型的测试用例。
2.6 test_report: 测试报告生成目录
说明:测试报告生成目录。
3 测试执行
测试执行入口为main.py;运行main.py文件即可。对应测试用例选取执行根据用例标记来进行区分。
4. 用例设计
4.1 用例以py文件设计
- 一个测试套原则上为一个
python
类(对应一个py文件),一个测试套包含多个测试用例. - 测试用例再设计的时候,加上对应的中文描述,如下:
@allure.suite('任务增删改查') # 注释对应的测试类的中文描述
class TestBankFlowTask01:
@allure.testcase('任务创建及删除') # 注释对应的测试方法的中文描述
@allure.feature('任务操作') # 注释其对应的特性,可有可无
@pytest.mark.smoke # 标记对用测试用例的优先级别。 默认级别:Blocker(该用例执行失败,会造成中断缺陷), Critical(该用例执行失败,会造成临界缺陷,即功能点缺失), Normal(该用例执行失败,会造成普通缺陷), Minor(该用例执行失败,会造成次要缺陷), Trivial(该用例执行失败,会造成轻微缺陷)
def test_bank_flow_create_and_delete(self, api, task_create_and_delete):
"""
验证银行流水任务创建
"""
with allure.step('任务创建'): # 代码内为with,在方法则为@allure.step('任务创建')
task_id, task_name = task_create_and_delete
assert int(task_id), '流水任务创建验证'
with allure.step('任务删除'):
result_delete = api.bank_flow_task_delete(task_id=task_id)
assert result_delete.status_code == 200
4.2 用例以yaml文件设计
- 一个yml文件,文件命名规则(testXXXX.yml 或 testXXXX.yaml),可以包含一个或多个测试用例验证.
- 测试用例设计模板如下:
- test:
name: 登录接口 # 接口用例名称
request:
url: api/login # 接口请求url
method: post # 接口请求类型
params: { "json": {"username":"auto_test","password":"auto_test"}} #接口请求入参
validate:
- eq: $.status # 接口验证结果
- eq: $.code # 接口验证结果
- sa: { "token": "$.data.accessToken"} # 将会把本次请求的值存在$token$中,后续需要直接在params中传人$token$,会自动替换
expect:
json: { "code": 2000000, "status": 200 }
- test:
name: 查询接口
request:
url: api/list
method: post
params: { "json": {"start": 0,"number": 10},
"headers": { "Content-Type": "application/json", "Authorization": "Bearer $token$"}}
validate:
- eq: $.status # 接口验证结果
- eq: $.code # 接口验证结果
expect:
json: { "code": 2000000, "status": 200 }
- test:
name: 上传接口
request:
url: api/upload
method: get
params: { "data": {"start": 0,"number": 10},
"headers": { "Content-Type": "application/json"},
"files": ["test1.pdf", "test2.xlsx"]}
# 系统会自动根据files下的文件名到resource目录下查找对应文件封装后上传,文件不要重名
validate:
- eq: $.status # 接口验证结果
- eq: $.code # 接口验证结果
- sa: $.data.id # 存储上传文件的返回ID
expect:
json: { "code": 2000000, "status": 200 }
-
备注: yaml用例也支持以xlsx文件存储。
-
支持断言关键字:
- eq:将根据请求按照路径获取的值与expect进行判等
- sa:将把请求得到的值按照路径获取并以对应$key$存储以供后续使用。
5. py打包详情
5.1 安装打包工具
pip3 install whell -y
pip3 install twine -y
5.2 打包
python3 setup.py bdist_wheel
5.3 输出结果
.
├── README.md
├── build
│ ├── bdist.macosx-10.9-universal2
│ └── lib
├── dg_itest
│ ├── __init__.py
│ ├── __pycache__
│ ├── servers
│ └── utils
├── dg_itest.egg-info
│ ├── PKG-INFO
│ ├── SOURCES.txt
│ ├── dependency_links.txt
│ ├── not-zip-safe
│ ├── requires.txt
│ └── top_level.txt
├── dist
│ ├── dg-itest-0.0.9.tar.gz
│ └── dg_itest-0.0.9-py3-none-any.whl
├── publish.sh
├── requirements.txt
├── setup.py
├── test
│ ├── __pycache__
│ └── test_replace.py
├── test_cases
│ ├── main.py
│ ├── test_report
│ ├── test_res
│ └── test_src
└── venv
├── bin
├── lib
└── pyvenv.cfg
5.4 上传pypi
twine upload dist/*
6. 参考文档
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distribution
dg_itest-0.0.10-py3-none-any.whl
(14.4 kB
view hashes)
Close
Hashes for dg_itest-0.0.10-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0802eac93f63c95b356cb597927570d3a3b46147b0c631c0fd465605691561a7 |
|
MD5 | 9c02c4b594914fff6db14a56d7b15f10 |
|
BLAKE2b-256 | 920cb8d1c870e88baddf9d5db09bd555a420a444d8054972a99ba0f0902fa4c3 |