目录
1)环境准备:
一、安装python
二、安装虚拟环境:
三、安装requests库:
四、http测试工具:
五、在本地搭建httpbin:
(2)requests.get()
一、看下方法定义:
二、get方法简单使用:
(3)requests.post()
一、方法定义:
二、post方法简单使用:
写在最后
接口测试的方式有很多,比如可以用工具(jmeter,postman)之类,也可以自己写代码进行接口测试,工具的使用相对来说都比较简单,重点是要搞清楚项目接口的协议是什么,然后有针对性的进行选择,甚至当工具不太适合项目时需要自己进行开发。
在我们项目的初期,我们采用的是jmeter进行接口测试,当时觉得这个工具上手简单,团队成员学习成本低,并且接口测试的脚本稍微调整一下还可以用来做性能测试。
不过随着项目规模、团队人数的不断增长,渐渐的这个工具有适应不了当前项目的需求了,为此我们项目也重新开发了相关接口自动化的平台。但是,但是。。。可能是我让大家中毒太深,现在很多同学一提到接口测试关联到jmeter,为此,我深深感到不安。毕竟jmeter只是个工具,换个项目换个协议你是否还能玩转接口测试呢?session和cookie有什么区别?工具又是怎么实现的呢?
比如session如何保存,接口依赖如何处理,case如何管理及执行顺序,测试数据如何管理等等题,这个过程也有助于我们更加深刻的理解接口测试和http协议。
本文主要采用python语言,python中http协议接口相关的库有urllib,urllib2以及reqeusts库,这其中reqeusts库用来起来最方便,因此我也主要采用requests库来做http协议的接口测试。首先来看下需要哪些环境信息:
mac下自带安装了python,这个不多说了。
我们在一台机器上可以安装多个python版本,为了使每个版本的环境相互不受干扰,可以安装虚拟环境,安装方法如下:
1、安装virtualenv:pip install virtualenv
2、新建名为venv的虚拟环境:virtualenv venv
3、进入新环境:source venv/bin/activate
4、退出:deactivate
>>>pip install requests
ps:用python做http协议的接口测试会用到这个库。
一个使用 Python + Flask 编写的 HTTP 请求和响应服务,该服务主要用于测试 HTTP 库。后续测试我们都基于这个网站。
http://httpbin.org
考虑到测试时要不断访问 httpbin 网站,请求过多担心被拉到黑名单,我们自己在本志搭建一套httpbin服务。
1、安装:pip install gunicorn
2、安装:pip install httpbin
3、启动:gunicorn httpbin:app
至此,环境搭建已经完毕,可以开始玩了~
环境搭建好后,接下来我们先来了解一下requests的一些简单使用,主要包括:
本节首先来了解一下requests库中如何发送get请求:
1、到官方文档去了下requests.get()方法的定义,如下:
2、点击右上角的【source】,看一下它的源码如下:
看到最后一行return,get方法最后是通过调用 requests.request 方法实现的,其实在其它的请求方法如post,put,head,delete等方法都是调用的request方法,然后把请求方法的类型传递给request方法第一个参数。
3、HTTP协议是一个基于请求/响应模式的、无状态的,应用层协议。既然有请求,就有响应,来看下resquest中常用的响应信息:
1、不带参数的get:
# -*- coding:utf-8 -*-
#不带参数的get
import requests
import json
host = "http://httpbin.org/"
endpoint = "get"
url = ''.join([host,endpoint])
r = requests.get(url)
#response = r.json()
print type(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
import requests
import json
host = "http://httpbin.org/"
endpoint = "get"
url = ''.join([host,endpoint])
params = {"show_env":"1"}
r = requests.get(url=url,params=params)
print r.url
输出:
http://httpbin.org/get?show_env=1
{
'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': 'http: //httpbin.org/get?show_env=1'
}
3、带header的get:
# -*- coding:utf-8 -*-
import requests
import json
host = "http://httpbin.org/"
endpoint = "get"
url = ''.join([host,endpoint])
headers = {"User-Agent":"test request headers"}
r = requests.get(url)
r = requests.get(url,headers=headers)
#response = r.json()
print (eval(r.text))['headers']['User-Agent']
输出:
test request headers
4、同时带参数和header:
# -*- coding:utf-8 -*-
import requests
import json
host = "http://httpbin.org/"
endpoint = "get"
url = ''.join([host,endpoint])
headers = {"User-Agent":"test request headers"}
params = {"show_env":"1"}
r = requests.get(url)
r = requests.get(url,headers=headers,params=params)
#response = r.json()
print (eval(r.text))['headers']['User-Agent']
print r.url
输出:
test request headers
http://httpbin.org/get?show_env=1
1、到官方文档去了下requests.post()方法的定义,如下:
2、源码:
3、常用返回信息:
1、带数据的post:
# -*- coding:utf-8 -*-
import requests
import json
host = "http://httpbin.org/"
endpoint = "post"
url = ''.join([host,endpoint])
data = {'key1':'value1','key2':'value2'}
r = requests.post(url,data=data)
#response = r.json()
print (r.text)
输出:
{
"args": {},
"data": "",
"files": {},
"form": {
"key1": "value1",
"key2": "value2"
},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Connection": "close",
"Content-Length": "23",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.18.1"
},
"json": null,
"origin": "183.14.133.88",
"url": "http://httpbin.org/post"
}
2、带header的post:
# -*- coding:utf-8 -*-
import requests
import json
host = "http://httpbin.org/"
endpoint = "post"
url = ''.join([host,endpoint])
headers = {"User-Agent":"test request headers"}
# r = requests.post(url)
r = requests.post(url,headers=headers)
#response = r.json()
输出:
{
"args": {},
"data": "",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Connection": "close",
"Content-Length": "0",
"Host": "httpbin.org",
"User-Agent": "test request headers"
},
"json": null,
"origin": "183.14.133.88",
"url": "http://httpbin.org/post"
}
3、带json的post:
# -*- coding:utf-8 -*-
import requests
import json
host = "http://httpbin.org/"
endpoint = "post"
url = ''.join([host,endpoint])
data = {
"sites": [
{ "name":"test" , "url":"www.test.com" },
{ "name":"google" , "url":"www.google.com" },
{ "name":"weibo" , "url":"www.weibo.com" }
]
}
r = requests.post(url,json=data)
# r = requests.post(url,data=json.dumps(data))
response = r.json()
输出:
{
"args": {},
"data": "{\"sites\": [{\"url\": \"www.test.com\", \"name\": \"test\"}, {\"url\": \"www.google.com\", \"name\": \"google\"}, {\"url\": \"www.weibo.com\", \"name\": \"weibo\"}]}",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Connection": "close",
"Content-Length": "140",
"Content-Type": "application/json",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.18.1"
},
"json": {
"sites": [
{
"name": "test",
"url": "www.test.com"
},
{
"name": "google",
"url": "www.google.com"
},
{
"name": "weibo",
"url": "www.weibo.com"
}
]
},
"origin": "183.14.133.88",
"url": "http://httpbin.org/post"
}
4、带参数的post:
# -*- coding:utf-8 -*-
import requests
import json
host = "http://httpbin.org/"
endpoint = "post"
url = ''.join([host,endpoint])
params = {'key1':'params1','key2':'params2'}
# r = requests.post(url)
r = requests.post(url,params=params)
#response = r.json()
print (r.text)
输出:
{
"args": {
"key1": "params1",
"key2": "params2"
},
"data": "",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Connection": "close",
"Content-Length": "0",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.18.1"
},
"json": null,
"origin": "183.14.133.88",
"url": "http://httpbin.org/post?key2=params2&key1=params1"
}
5.普通文件上传:
# -*- coding:utf-8 -*-
import requests
import json
host = "http://httpbin.org/"
endpoint = "post"
url = ''.join([host,endpoint])
#普通上传
files = {
'file':open('test.txt','rb')
}
r = requests.post(url,files=files)
print (r.text)
输出:
{
"args": {},
"data": "",
"files": {
"file": "hello world!\n"
},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Connection": "close",
"Content-Length": "157",
"Content-Type": "multipart/form-data; boundary=392865f79bf6431f8a53c9d56c62571e",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.18.1"
},
"json": null,
"origin": "183.14.133.88",
"url": "http://httpbin.org/post"
}
6.定制化文件上传:
# -*- coding:utf-8 -*-
import requests
import json
host = "http://httpbin.org/"
endpoint = "post"
url = ''.join([host,endpoint])
#自定义文件名,文件类型、请求头
files = {
'file':('test.png',open('test.png','rb'),'image/png')
}
r = requests.post(url,files=files)
print (r.text)heman793
7.多文件上传:
# -*- coding:utf-8 -*-
import requests
import json
host = "http://httpbin.org/"
endpoint = "post"
url = ''.join([host,endpoint])
#多文件上传
files = [
('file1',('test.txt',open('test.txt', 'rb'))),
('file2', ('test.png', open('test.png', 'rb')))
]
r = requests.post(url,files=files)
print (r.text)
8.流式上传:
# -*- coding:utf-8 -*-
import requests
import json
host = "http://httpbin.org/"
endpoint = "post"
url = ''.join([host,endpoint])
#流式上传
with open( 'test.txt' ) as f:
r = requests.post(url,data = f)
print (r.text)
输出:
{
"args": {},
"data": "hello world!\n",
"files": {},
"form": {},
"headers": {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Connection": "close",
"Content-Length": "13",
"Host": "httpbin.org",
"User-Agent": "python-requests/2.18.1"
},
"json": null,
"origin": "183.14.133.88",
"url": "http://httpbin.org/post"
}
最后再唠唠一句,如果想以测试为长期发展职业目标,是需要时刻保持学习的,要使自己具备竞争力,无论你现在工作几年,只要行动起来,你就已经占优势了,好啦就到这里了,祝大家2022年能升职加薪,没入职的就早日拿到心仪公司的offer,事事顺遂。
这份软件测试学习资源整理起来也不容易,希望大家帮忙给「点赞」「收藏」,咱不做白嫖党!!