代码拉取完成,页面将自动刷新
使用
Python
语言 +Python
第三方库实现的接口自动化测试工具,使用该工具 Python版本 >= 3.7
Python
+ UnitTest
+ UnitTestReport
+ JsonPath
+ logging
,支持以下功能:
JsonPath
或正则断言├─common \\公共方法
│ │ common.py \\测试管理
│ │ ddt.py \\数据驱动 1
│
├─data \\测试用例
│ login_test.yaml
├─logs \\日志存储(默认被git忽略)
│
├─report \\测试报告存储(默认被git忽略)
│
├─settings \\路径读取
│ │ setting.py
│
├─testcase \\用例执行
│ │ testcase.py
│
├─utils \\工具类
│ ├─config \\读取config.ini文件
│ │ │ config.py
│ ├─request \\请求模块
│ │ │ request.py
│ ├─log \\日志模块
│ │ │ log.py
|
├─.gitignore \\项目git忽略文件
├─config.ini \\配置文件
├─debugtalk.py \\用户自定义函数
├─LICENSE \\开源MIT协议
├─main.py \\Python启动文件
├─README.MD \\项目简介
├─requirements.txt \\项目依赖库文件
├─采坑记录 \\问题记录
jsonpath==0.82
retry==0.9.2
unittestreport==1.5.1
requests~=2.24.0
assertpy~=1.1
colorlog~=6.7.0
https://boardmix.cn/app/editor/ytYA_5P6k7diz3vIkBTN3g
- id: 1
name: login
describe: 登录
request:
url: user/login
method: post
data:
username: yjc11
password: 123456
extract:
username: $..username
cookie: $.Set-Cookie
validate:
status_code: 200
errorCode:
actual: $.errorCode
expect: 0
compare: ==
username:
actual: $..username
expect: ${1-username}
compare: ==
属性名 | 默认值 | 可选值 | 是否必填 | 类型 | 注释 |
---|---|---|---|---|---|
id | 是 | int | 用例ID | ||
name | 是 | str | 用例名称 | ||
describe | 是 | str | 用例描述 | ||
skip | true/false | 否 | bool | 是否跳过 | |
retry | 否 | dict | 接口轮询使用,参考retry模型 | ||
request | 是 | dict | 发送请求,参考request模型 | ||
extract | 否 | dict | 提取动态参数实现上下游接口依赖 | ||
validate | 是 | dict | 校验,参考validate模型 |
属性名 | 默认值 | 可选值 | 是否必填 | 类型 | 注释 |
---|---|---|---|---|---|
tries | 是 | int | 最大重试次数 | ||
delay | 是 | int | 每次重试的时间间隔(单位:秒) |
属性名 | 默认值 | 可选值 | 是否必填 | 类型 | 注释 |
---|---|---|---|---|---|
url | 是 | str | 请求path | ||
method | GET、POST、DELETE、PUT等 | 是 | str | 请求方法 | |
headers | 否 | dict | 请求头 | ||
cookies | 否 | dict | 请求cookie | ||
params | 否 | dict | GET请求url?后的拼接参数 | ||
data | 否 | dict | 请求数据(适用Content-Type: application/x-www-form-urlencoded) | ||
json | 否 | dict | 请求数据(适用Content-Type: application/json) |
属性名 | 默认值 | 可选值 | 是否必填 | 类型 | 注释 |
---|---|---|---|---|---|
status_code | 是 | int | HTTP响应状态码 |
采用assertpy
第三方库去实现,具体使用请参考 assertpy 官方文档。
通常,断言失败会引发错误,从而立即停止测试执行。软断言是一种将断言失败收集在一起的方法,可以在结束时立即引发,而不会停止测试。要在assertpy
中使用软断言,只需使用with soft_assertions()
上下文管理器,如下所示:
from assertpy import assert_that, soft_assertions
with soft_assertions():
assert_that('foo').is_length(4)
assert_that('foo').is_empty()
assert_that('foo').is_false()
assert_that('foo').is_digit()
assert_that('123').is_alpha()
assert_that('foo').is_upper()
assert_that('FOO').is_lower()
assert_that('foo').is_equal_to('bar')
assert_that('foo').is_not_equal_to('foo')
assert_that('foo').is_equal_to_ignoring_case('BAR')
在块的末尾,所有断言失败都被收集在一起,并引发一个AssertionError
:
AssertionError: soft assertion failures:
1. Expected <foo> to be of length <4>, but was <3>.
2. Expected <foo> to be empty string, but was not.
3. Expected <False>, but was not.
4. Expected <foo> to contain only digits, but did not.
5. Expected <123> to contain only alphabetic chars, but did not.
6. Expected <foo> to contain only uppercase chars, but did not.
7. Expected <FOO> to contain only lowercase chars, but did not.
8. Expected <foo> to be equal to <bar>, but was not.
9. Expected <foo> to be not equal to <foo>, but was.
10. Expected <foo> to be case-insensitive equal to <BAR>, but was not.
测试报告这里采用 unittestreport 第三方库去实现,该库已实现了如下功能:
import time
from unittestreport import TestRunner, Load
from settings import *
# 1.加载用例到测试套件
suites = Load.discover(TEST_PATH, pattern='test*.py')
# 自动化测试报告名称
report_name = time.strftime("%Y%m%d_%H%M%S") + "_" + "测试报告.html"
# 2.创建一个用例运行程序
runner = TestRunner(suites,
filename=report_name,
report_dir=REPORT_PATH,
title='测试报告',
tester='叶鸟翼',
desc="接口自动化测试报告",
templates=2)
# 3.运行测试用例
runner.run()
在使用TestRunner
创建测试运行程序时,可以通过以下参数来,自定义报告的相关内容
suites
: 测试套件(必传)filename
: 指定报告文件名report_dir
: 指定存放报告路径title
: 指定测试报告的标题templates
: 可以指定1,2,3三个风格的模板tester
: 测试人员名称报告样式一:
报告样式二:
报告样式三:
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。