之前跟大家说过关于自动化内容,但是大框内容,不是经常能碰到的问答,但是大框里面充斥着很多细致的内容,比如关于接口自动化,想必很多小伙伴都不知道你这个内容,也不知道如何使用吧,但是大家不必担心,下面就演示给大家看。
一、准备工作:
需要使用的第三方库介绍
Requests
python中有许多针对http的库,例如自带的urllib2,但是自带的urllib2编写起来实在是太费精力,所以采用号称"HTTP for Humans"的requests库。
xlrd
xlrd使得python可以方便的对excel文件进行读写操作,此次通过xlrd读取excel文件中的测试数据。
http测试工具:
一个使用 Python + Flask 编写的 HTTP 请求和响应服务,该服务主要用于测试 HTTP 库。后续测试我们都基于这个网站。
在本地搭建httpbin:
考虑到测试时要不断访问 httpbin 网站,请求过多担心被拉到黑名单,我们自己在本志搭建一套httpbin服务。
1、安装:pip install gunicorn
2、安装:pip install httpbin
3、启动:gunicorn httpbin:app
二、实现代码:
get方法简单使用:
不带参数的get:
#-*-coding:utf-8-*- #不带参数的get importrequests importjson host="http://httpbin.org/" endpoint="get" url=''.join([host,endpoint]) r=requests.get(url) #response=r.json() printtype(r.text) print(eval(r.text))
输出:
{ 'origin':'183.14.133.88', 'headers':{ 'Connection':'close', 'Host':'httpbin.org', 'Accept-Encoding':'gzip, deflate', 'Accept':'*/*', 'User-Agent':'python-requests/2.18.1' }, 'args':{ }, 'url':'http://httpbin.org/get' }
2、带参数的get:
#-*-coding:utf-8-*- #带参数的get importrequests importjson host="http://httpbin.org/" endpoint="get" url=''.join([host,endpoint]) params={"show_env":"1"} r=requests.get(url=url,params=params) printr.url
输出:
{ 'origin':'183.14.133.88', 'headers':{ 'X-Request-Id':'ebe922b4-c463-4fe9-9faf-49748d682fd7', 'Accept-Encoding':'gzip, deflate', 'X-Forwarded-Port':'80', 'Total-Route-Time':'0', 'Connection':'close', 'Connect-Time':'0', 'Via':'1.1vegur', 'X-Forwarded-For':'183.14.133.88', 'Accept':'*/*', 'User-Agent':'python-requests/2.18.1', 'X-Request-Start':'1504755961007', 'Host':'httpbin.org', 'X-Forwarded-Proto':'http' }, 'args':{ 'show_env':'1' }, 'url':'https://www.tulingpyton.cn/d/file/p/20241208/vtiyydl3lch }
好啦,大家可以先消化了解下哦~如果还想了解更多的精彩内容,可以到python教学中心查询~