接口自动化测试框架
目录结构
7 部分(5个目录、2个文件):
api/: 存储接口对象层(自己封装的 接口)
scripts/: 存储测试脚本层 (unittest框架实现的 测试类、测试方法)
data/: 存储 .json 数据文件
report/: 存储 生成的 html 测试报告
common/: 存储 通用的 工具方法
config.py: 存储项目的配置信息(全局变量)
run_suite.py: 组装测试用例、生成测试报告的 代码
一、Requests框架
简介:
Requests库 是 Python编写的,基于urllib 的 HTTP库,使用方便。
安装:
pip install requests
查验:
- 步骤1:pip 中查验
# 方法1
pip list
# 方法2
pip show 库名
- 步骤2:pycharm 中查验
设置http请求语法
resp = requests.请求方法(url='URL地址', params={
k:v}, headers={
k:v},data={
k:v}, json={
k:v}, cookies='cookie数据'(如:令牌))
请求方法:
get请求 - get()
post请求 - post()
put请求 - put()
delete请求 - delete()
url: 待请求的url - string类型
params:查询参数 - 字典
headers:请求头 - 字典
data:表单格式的 请求体 - 字典
json:json格式的 请求体 - 字典
cookies:cookie数据 - string类型
resp:响应结果
入门案例
使用Requests库访问 百度 http://www.baidu.com
import requests
resp=requests.get(url="http://www.baidu.com")
print(resp.text)
案例
使用Requests库,完成 平安保险核保接口调用。返回 ”200“ 即可。
import requests
import json
url = "http://mcp-test-health.pingan.com.cn/gateway/mcp/outChannel/validate.do"
params = {
"c": "WI"}
headers = {
"Content-Type": "application/json"}
body = {
"requestId": "hebaojiaoyanma_3453946894536",
"c": "WI",
"data": {
"productId": "A100000013",
"outChannelOrderId": "66323958403338552251",
"effDate": "2020-12-20",
"applyDate": "2020-12-19",
"totalPremium": "357.00",
"isNoticeConfirm": "Y",
"isShareCoverage": "N",
"discount": "1",
"applicant": {
"name": "士发放",
"birthday": "1980-01-01",
"sex": "M",
"idType": "1",
"idno": "110101198001010117",
"contactInfo": {
"mobile": "16351656161",
"email": "123@qq.com",
"contactAddress": {
"provinceCode": "110000",
"cityCode": "110000",
"areaCode": "110101"
}
},
"extend": {
"taxType": "null",
"realCheckType": "1"
}
},
"insurants": [
{
"seqno": "1",
"name": "华东师范",
"birthday": "1998-12-22",
"sex": "F",
"idType": "2",
"idno": "41151990221",
"socialSecurity": "Y",
"relationshipWithApplicant": "2",
"relationshipWithPrimaryInsurant": "1",
"contactInfo": {
"email": "123@qq.com"
},
"coverages": [
{
"planType": "0",
"sumInsured": "100000.00",
"benLevel": "09",
"period": "12",
"periodDay": "0",