在进行接口测试时,我们可以使用已有的工具(如: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的简单接口测试的更多相关文章

  1. 基于Python Requests的数据驱动的HTTP接口测试

    发表于:2017-8-30 11:56  作者:顾翔   来源:51Testing软件测试网原创 http://www.51testing.com/html/69/n-3720769-2.html   ...

  2. 基于python的直播间接口测试实战 详解结合项目

    基于python的直播间接口测试详解 一.基本用例内容描述 以设置白名单 /advisor/setUserWhiteList.do接口为例,该方法为POST at first,先要导入一些常用到的模块 ...

  3. Python+requests 发送简单请求--》获取响应状态--》获取请求响应数据

    Python+requests 发送简单请求-->获取响应状态-->获取请求响应数据 1.环境:安装了Python和vscode编译器(Python自带的编译器也ok).fiddler抓包 ...

  4. 基于Python PIL实现简单图片格式转化器

    基于Python PIL实现简单图片格式转化器 目录 基于Python PIL实现简单图片格式转化器 1.简介 2.前期资料准备 2.1.1如何实现图片格式转换? 2.1.2如何保存需要大小的图片? ...

  5. 基于Python+Requests+Pytest+YAML+Allure实现接口自动化

    本项目实现接口自动化的技术选型:Python+Requests+Pytest+YAML+Allure ,主要是针对之前开发的一个接口项目来进行学习,通过 Python+Requests 来发送和处理H ...

  6. Python Requests库简单入门

    我对Python网络爬虫的学习主要是基于中国慕课网上嵩天老师的讲授,写博客的目的是为了更好触类旁通,并且作为学习笔记之后复习回顾. 1.引言 requests 库是一个简洁且简单的处理HTTP请求的第 ...

  7. 基于Python + requests 的web接口自动化测试框架

    之前采用JMeter进行接口测试,每次给带新人进行培训比较麻烦,干脆用python实现,将代码和用例分离,易于维护. 项目背景 公司的软件采用B/S架构,进行数据存储.分析.管理 工具选择 pytho ...

  8. python+requests+unittest API接口测试

    黑熊再网上查找了下接口测试相关的资料,大都重点是以数据驱动的形式,见用例维护在文本或表格中,而没有说明怎么样去生成想要的用例, 问题: 测试接口时,比如参数a,b,c,我要先测a参数,有(不传,为空, ...

  9. 基于python创建一个简单的HTTP-WEB服务器

    背景 大多数情况下主机资源只有开发和测试相关人员可以登录直接操作,且有些特定情况"答辩.演示.远程"等这些场景下是无法直接登录主机的.web是所有终端用户都可以访问了,解决了人员权 ...

随机推荐

  1. 72. Edit Distance (JAVA)

    Given two words word1 and word2, find the minimum number of operations required to convert word1 to  ...

  2. 4、MySQL 申明变量给查询数据编号

    摘自: https://www.cnblogs.com/qixuejia/archive/2010/12/21/1913203.html https://blog.csdn.net/arbben/ar ...

  3. Ubuntu伪破解Navicat12方法

    一.去官网下载navicat112_premium_cs_x64 for linux版本二.用tar解压安装包三.navicat解压即可用,直接进入解压后的目录,然后用‘./’运行start_navi ...

  4. php判断变量是否为数字is_numeric()

    is_numeric — 检测变量是否为数字或数字字符 <?php $tests = array( "31", 1380, "1e4", "no ...

  5. Web Api 接口测试工具:Swagger

    前言:WebApi接口开发完毕后,交付给前端人员或手机端开发者时接口说明文档是必不可少的配套设备,如果公司流程不规范大家使用口口相传的交接方式,而且没有改进的欲望,那你可以到此为止了.Swagger是 ...

  6. C#基础知识之理解Cookie和Session机制

    会话(Session)跟踪是Web程序中常用的技术,用来跟踪用户的整个会话.常用的会话跟踪技术是Cookie与Session.Cookie通过在客户端记录信息确定用户身份,Session通过在服务器端 ...

  7. [COGS 755]山海经:线段树

    网上似乎这道题的题解很少?写一个吧 我跟这道题的渊源追溯到了上个学期刚刚学线段树的那一天... 当时线段树专题前边的题都是一些板子就不一会就水过了,然后就看到了最后一题的它:山海经 那一个上午,我竭尽 ...

  8. Linux的解压缩相关命令

    Linux的解压缩相关命令 知识点: 1.zip命令 2.tar命令 3.压缩和解压常用组合

  9. Q15格式表示负小数

    1.用Q15.16-bit格式,表示出-0.5? 解析:其实很简单,Q15是dsp里为了优化浮点的,就是将小数* 2^15. 例如:0.333 * 32768 = 10911.744  取整数就是10 ...

  10. 随堂小测APP使用体验

    随堂小测APP使用体验 先要去注册账号需要填写用户名.密码.手机号.学号/教师号.学校.专业.即可注册,注册成功后,即可登录APP进,登陆进去以后.会有两个界面,课堂和我的,注册.登录简单,通俗易懂, ...