基于python+requests的简单接口测试
在进行接口测试时,我们可以使用已有的工具(如:jmeter)进行,也可以使用python+requests进行。以下为简单的接口测试模板:
一、提取常用变量,统一配置
新建一个config.py文件,用来存放统一变量
如:
# _*_ coding:;utf--8 _*_
import json #接口测试中,使用的头文件信息
headers={
"Accept":"application/json",
"Connection":"keep-alive",
"Accept-Language":"zh-Hans-CN;q=1",
"Accept-Encoding":"gzip, deflate"
} #测试地址:IP+端口
service="http://192.168.1.1:8080" #读取测试用例文件
interfile=open("D:\\testcase\接口测试脚本-总接口.csv","r")
intercase=interfile.readlines()
interfile.close() #读取测试结果文件,并保存结果
def resultmode(interresult):
testresult=open("D:\\testresult\\testresult.csv","a")
# interresult="测试结果"
testresult.write(interresult)
testresult.close()
二、提取requests的方法,方便调用
如:
#_*_ coding:utf-8 _*_
import json
from Interface_test.config import service,headers,resultmode,intercase
import requests
import re,time
import types def Interface_post(interface,paramter):
url = service + interface
# paramter = json.dumps(paramter)
try:
resp = requests.post(url, headers=headers, data=paramter,timeout=30)
resp.json()
return resp
except ValueError:
print("响应值不是json格式")
return resp
except ConnectionError:
print("网络异常")
return resp
except TimeoutError:
print("请求超时")
return resp
def Interface_get(interface,paramter):
url = service + interface
paramter = json.dumps(paramter) resp = requests.get(url, headers=headers,timeout=1)
# print(resp.json())
return resp
三、针对接口的测试
如:
# _*_ coding:utf-8 _*_ import json
from Interface_test.config import service,headers,intercase,resultmode
from Interface_test.mode import Interface_post
import re,time
import requests class interface_test():
def __init__(self):
"""init""" if __name__=='__main__': #输出接口测试执行的时间到结果文件
rtime=time.strftime("%Y-%m-%d %H-%M-%S", time.localtime())
resultmode("\n"+"本次测试时间为:"+rtime+"\n") #循环读取测试用例文件中的信息
for i in range(len(intercase)):
param=intercase[i].split(",") ###赋值接口请求路径
inter = param[2] ####赋值接口参数
para = (str(param[3])).strip() ###发起请求
responses=Interface_post(inter,para)
# print(responses.encoding)
# print(responses.text) # print(responses.cookies) ###查看请求结果
#请求响应code
code=responses.status_code
# print(responses.status_code==requests.codes.ok)
# print(responses.raise_for_status()) #判断请求结果
if code==200:
##当请求成功时,获取响应数据
results = json.loads((responses.content).decode())
##在响应数据中提取result的值
bool = results.get("result")
# body=results.get("body")
##判断result值:为1时成功,输出信息
if bool=='':
###将响应结果输出到文件
resultstring=param[0]+","+param[1]+","+str(code)+","+"请求成功,响应成功"+"\n"
resultmode(resultstring)
##判断result结果:为0时,表示请求发送成功,但是响应的数据有问题,返回响应的error信息
elif bool=='':
errorstring=results["body"]["errorDescription"]
resultstring=param[0]+","+param[1]+","+str(code)+","+"请求成功,响应报错"+\
","+errorstring+"\n"
resultmode(resultstring)
else:
resultstring = param[0] + "," +param[1]+","+ str(code)+ ","+"请求响应失败"+"\n"
resultmode(resultstring)
#当响应code不为200时,输出响应code
else:
resultstring = param[0] + "," +param[1]+","+ str(code) + ","+"请求响应失败"+"\n"
resultmode(resultstring)
四、涉及的知识点说明
1、requests
地址:http://docs.python-requests.org/zh_CN/latest/user/quickstart.html
2、文件操作
地址:https://www.cnblogs.com/smallstone2018/p/9841957.html
3、Json
使用json前,需要先导入包:import json
json与python的字典之间的转换
json模块有两个方法:
loads():将json数据转换成disc数据;
dumps():将dict数据转换成json数据;
load():读取json文件数据,转换成dict数据;
dump():将dict数据转换成json数据后 写入json文件;
基于python+requests的简单接口测试的更多相关文章
- 基于Python Requests的数据驱动的HTTP接口测试
发表于:2017-8-30 11:56 作者:顾翔 来源:51Testing软件测试网原创 http://www.51testing.com/html/69/n-3720769-2.html ...
- 基于python的直播间接口测试实战 详解结合项目
基于python的直播间接口测试详解 一.基本用例内容描述 以设置白名单 /advisor/setUserWhiteList.do接口为例,该方法为POST at first,先要导入一些常用到的模块 ...
- Python+requests 发送简单请求--》获取响应状态--》获取请求响应数据
Python+requests 发送简单请求-->获取响应状态-->获取请求响应数据 1.环境:安装了Python和vscode编译器(Python自带的编译器也ok).fiddler抓包 ...
- 基于Python PIL实现简单图片格式转化器
基于Python PIL实现简单图片格式转化器 目录 基于Python PIL实现简单图片格式转化器 1.简介 2.前期资料准备 2.1.1如何实现图片格式转换? 2.1.2如何保存需要大小的图片? ...
- 基于Python+Requests+Pytest+YAML+Allure实现接口自动化
本项目实现接口自动化的技术选型:Python+Requests+Pytest+YAML+Allure ,主要是针对之前开发的一个接口项目来进行学习,通过 Python+Requests 来发送和处理H ...
- Python Requests库简单入门
我对Python网络爬虫的学习主要是基于中国慕课网上嵩天老师的讲授,写博客的目的是为了更好触类旁通,并且作为学习笔记之后复习回顾. 1.引言 requests 库是一个简洁且简单的处理HTTP请求的第 ...
- 基于Python + requests 的web接口自动化测试框架
之前采用JMeter进行接口测试,每次给带新人进行培训比较麻烦,干脆用python实现,将代码和用例分离,易于维护. 项目背景 公司的软件采用B/S架构,进行数据存储.分析.管理 工具选择 pytho ...
- python+requests+unittest API接口测试
黑熊再网上查找了下接口测试相关的资料,大都重点是以数据驱动的形式,见用例维护在文本或表格中,而没有说明怎么样去生成想要的用例, 问题: 测试接口时,比如参数a,b,c,我要先测a参数,有(不传,为空, ...
- 基于python创建一个简单的HTTP-WEB服务器
背景 大多数情况下主机资源只有开发和测试相关人员可以登录直接操作,且有些特定情况"答辩.演示.远程"等这些场景下是无法直接登录主机的.web是所有终端用户都可以访问了,解决了人员权 ...
随机推荐
- 关于IDEA中@Autowired 注解报错~图文
例如鼠标放上去会报错如下: Could not autowire. No beans of 'StudentMapper' type found. less... (Ctrl+F1) Inspecti ...
- hostid - 显示当前主机的数字化标识
SYNOPSIS(总览) hostid [-v] DESCRIPTION(描述) 显示当前主机的数字化标识(以十六进制的形式表示). --help 显示帮助信息后退出 --version 输出版本信息 ...
- Centos下载安装grafana
grafana的官网下载:https://grafana.com/grafana/download 一.安装服务端图像呈现组件 # yum install -y fontconfig freetype ...
- linux系统批量修改root密码
#!/bin/bash ip_list=(192.168.36.12 192.168.36.13) remote_cmd="echo new-passwd | passwd --stdin ...
- Django的MTV模型
MTV模型 Django框架的设计模式借鉴了MVC框架的思想,也是分成三部分,来降低各个部分之间的耦合性. MTV框架是Django的框架,三部分为: Model Template(模板) View ...
- 这样讲 SpringBoot 自动配置原理,你应该能明白了吧
https://juejin.im/post/5ce5effb6fb9a07f0b039a14 前言 小伙伴们是否想起曾经被 SSM 整合支配的恐惧?相信很多小伙伴都是有过这样的经历的,一大堆配置问题 ...
- 基于Apache服务在centos7上搭建文件列表
参考文献: https://www.cnblogs.com/snake553/p/8856729.html https://blog.csdn.net/yejinxiong001/article/de ...
- 005-使用smtp发送邮件报警
创建监控项: 如果有两个触发条件则中间用 and 连接,or等 此上 触发器已经创建好了,但是触发器的动作还需要去定义: 默认动作是停用的需要手动打开:
- jenkins项目名称改后,同步nginx配置
jenkins项目名称修改后,workspace的名称会同步更改,构建完了和原来的不是一个路径,如果每个前端项目一个单独的tomcat的话,需要更改nginx配置 /etc/nginx/conf.d
- git 合并某个分支指定的文件
$ git checkout <another-branch> <path-to-file> [<one-more-file> ...] $ git status ...