接口自动化测试之requests库详解

597 篇文章 16 订阅
6 篇文章 1 订阅

由于web接口自动化测试需要用到python的第三方库–requests库,运用requests库可以模拟发送http请求,再结合unittest测试框架,就能完成web接口自动化测试。

今天先来总结一下requests库的用法。希望对大家有帮助!

1.GET请求

1.1查看get函数的使用

>>> help(requests.get)          #查看requests库的属性get请求函数的使用

Help on function get in module requests.api:

get(url, params=None, **kwargs)

    Sends a GET request.

    :param url: URL for the new :class:`Request` object.

    :param params: (optional) Dictionary or bytes to be sent in the query string for the :class:`Request`.

    :param \*\*kwargs: Optional arguments that ``request`` takes.

    :return: :class:`Response <Response>` object

    :rtype: requests.Response

1.2 requests的get函数的入参说明

url:调用接口的URL地址。

params:为可选参数,该参数是一个字典类型。数据会以键/值对的形式置于 URL 中,跟在一个问号的后面。举例说明,若r=requests.get(“http://httpbin.org/get”,params={‘key1’:‘value1’,‘key2’:‘value2’}) 那么,最终请求的目标URL为http://bin.org/get?key1=val1& key2=val2。

**kwargs:其他可选参数,例如headers,files,cookies,auth,timeout,json等。

例如:auth=(‘username’,’password’):接口安全测试中用到的用户认证。

例如:headers = {‘user-agent’: ‘my-app/0.0.1’}:可定制发送请求头。

1.3 requests函数的返回值(http响应)

返回的是response类对象(requests.models.Response)。来自requests模块 models.py里的Response类

>>> r=requests.get('http://httpbin.org/get')  #查看response的属性

>>> dir(r)

['__attrs__', '__bool__', '__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__getstate__', '__hash__', '__init__', '__iter__', '__module__', '__new__', '__nonzero__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setstate__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_content', '_content_consumed', 'apparent_encoding', 'close', 'connection', 'content', 'cookies', 'elapsed', 'encoding', 'headers', 'history', 'is_permanent_redirect', 'is_redirect', 'iter_content', 'iter_lines', 'json', 'links', 'ok', 'raise_for_status', 'raw', 'reason', 'request', 'status_code', 'text','url']

对于返回对象常用的属性如下:

json():生成json数据对象的方法。如果 JSON 解码失败, r.json 就会抛出一个异常。例如,响应内容是 401 (Unauthorized),尝试访问 r.json 将会抛出 ValueError: No JSON object could be decoded 异常。

status_code:响应状态码。

encoding:编码,如utf-8。

url:目标url。

headers:响应头。类型为字典类型,若键不存在则返回None。

text:响应内容。字符串方式,会自动根据响应头部的字符编码进行解码。如果你改变了编码r.encoding,每当你访问 r.text ,Request 都将会使用 r.encoding 的新值。

content:二进制响应内容。字节方式,会自动为你解码gzip和deflate压缩。

raw:原始响应内容,也就是 urllib 的 response 对象,请求中要加stream=True,再使用 r.raw.read() 读取。

r.raise_for_status:失败请求(非200响应)抛出异常。

1.4举例说明

>>> payload={'key1':'value1','key2':'value2'}
>>> r=requests.get("http://httpbin.org/get",params=payload)
>>> r.status_code
200
>>> r.json()

{u'origin': u'113.98.252.236', u'headers': {u'Host': u'httpbin.org', u'Accept-Encoding': u'gzip, deflate', u'Accept': u'*/*', u'User-Agent': u'python-requests/2.7.0 CPython/2.7.11 Windows/7'}, u'args': {u'key2': u'value2', u'key1': u'value1'}, u'url': u'http://httpbin.org/get?key2=value2&key1=value1'}

>>> r.url  #url:params数据会以键/值对的形式置于 URL 中,跟在一个问号的后面。
u'http://httpbin.org/get?key2=value2&key1=value1'
>>> r.headers

{'content-length': '334', 'server': 'nginx', 'connection': 'keep-alive', 'access-control-allow-credentials': 'true', 'date': 'Fri, 09 Dec 2016 09:04:40 GMT', 'access-control-allow-origin': '*', 'content-type': 'application/json'}

>>> r.headers['content-type']
'application/json'
>>> r.raw
<requests.packages.urllib3.response.HTTPResponse object at 0x0000000002E64E10>
>>> r.cookies
<RequestsCookieJar[]>
>>> r.text

u'{\n "args": {\n "key1": "value1", \n "key2": "value2"\n }, \n "headers": {\n "Accept": "*/*", \n "Accept-Encoding": "gzip, deflate", \n "Host": "httpbin.org", \n "User-Agent": "python-requests/2.7.0 CPython/2.7.11 Windows/7"\n }, \n "origin": "113.98.252.236", \n "url": "http://httpbin.org/get?key2=value2&key1=value1"\n}\n'

>>> r.content

'{\n "args": {\n "key1": "value1", \n "key2": "value2"\n }, \n "headers": {\n "Accept": "*/*", \n "Accept-Encoding": "gzip, deflate", \n "Host": "httpbin.org", \n "User-Agent": "python-requests/2.7.0 CPython/2.7.11 Windows/7"\n }, \n "origin": "113.98.252.236", \n "url": "http://httpbin.org/get?key2=value2&key1=value1"\n}\n'

>>> payload = {'key1': 'value1', 'key2': 'value2'}

>>> r = requests.get("http://httpbin.org/get", params=payload,stream=True)

>>> r.raw

<requests.packages.urllib3.response.HTTPResponse object at 0x0000000003105940>

>>> r.raw.read()

'{\n  "args": {\n    "key1": "value1", \n    "key2": "value2"\n  }, \n  "headers": {\n    "Accept": "*/*", \n    "Accept-Encoding": "gzip, deflate", \n    "Host": "httpbin.org", \n    "User-Agent": "python-requests/2.7.0 CPython/2.7.11 Windows/7"\n  }, \n  "origin": "113.98.252.236", \n  "url": "http://httpbin.org/get?key2=value2&key1=value1"\n}\n'

1.5用fiddler查看抓包情况

r.json():json数据,可以看出与1.4中的r.json()值一致。

在这里插入图片描述r.headers:响应头数据,可以看出与1.4中r.headers值一致。
在这里插入图片描述r.raw:响应原始数据
在这里插入图片描述

1.6 get请求总结

综上所述,通过requests.get(“某url”,params={字典类型参数键值对})模拟浏览器发送一个http的请求(其中请求的方法是get,请求的url地址如下形式

http://httpbin.org/get?key2=value2&key1=value1),服务器处理数据后,会返回一个response对象,通过读取response对象的属性值,如json数据,可以做一系列的断言,从而验证该接口返回的数据是否正确。

2.POST请求

2.1查看post函数的使用

>>> help(requests.post)          #查看requests库的属性post请求函数的使用

post(url, data=None, json=None, **kwargs)
Sends a POST request.

:param url: URL for the new :class:`Request` object.
:param data: (optional) Dictionary, bytes, or file-like object to send in the body of the :class:`Request`.
:param json: (optional) json data to send in the body of the :class:`Request`.
:param \*\*kwargs: Optional arguments that ``request`` takes.
:return: :class:`Response <Response>` object
:rtype: requests.Response

2.2 requests的post函数的入参说明

url:调用接口的URL地址。

data:为可选参数,该参数是一个字典类型。

json:为可选参数,该参数是一个json类型。

**kwargs:其他可选参数,例如headers等。

2.3 requests函数的返回值(http响应)

同1.3

json():生成json数据对象的方法。如果 JSON 解码失败, r.json 就会抛出一个异常。例如,响应内容是 401 (Unauthorized),尝试访问 r.json 将会抛出 ValueError: No JSON object could be decoded 异常。

status_code:响应状态码。

encoding:编码,如utf-8。

url:目标url。

headers:响应头。类型为字典类型,若键不存在则返回None。

text:响应内容。字符串方式,会自动根据响应头部的字符编码进行解码。如果你改变了编码r.encoding,每当你访问 r.text ,Request 都将会使用 r.encoding 的新值。

content:二进制响应内容。字节方式,会自动为你解码gzip和deflate压缩。

raw:原始响应内容,也就是 urllib 的 response 对象,请求中要加stream=True,再使用 r.raw.read() 读取。

r.raise_for_status:失败请求(非200响应)抛出异常。

2.4举例说明

>>> payload = {'key1': 'value1', 'key2': 'value2'}

>>> r = requests.post("http://httpbin.org/post", data=payload)

>>> r.status_code

200

>>> r.json()

{u'files': {}, u'origin': u'113.98.252.236', u'form': {u'key2': u'value2', u'key1': u'value1'}, u'url': u'http://httpbin.org/post', u'args': {}, u'headers': {u'Content-Length': u'23', u'Accept-Encoding': u'gzip,deflate', u'Accept': u'*/*', u'User-Agent': u'python-requests/2.7.0 CPython/2.7.11 Windows/7', u'Host': u'httpbin.org', u'Content-Type': u'application/x-www-form-urlencoded'}, u'json': None, u'data': u''}

>>> r.url

u'http://httpbin.org/post'

>>> r.headers

{'content-length': '461', 'server': 'nginx', 'connection': 'keep-alive', 'access-control-allow-credentials': 'true', 'date': 'Mon, 12 Dec 2016 02:46:14 GMT', 'access-control-allow-origin': '*', 'content-type': 'application/json'}

>>> r.headers['content-type']

'application/json'

>>> r.raw

<requests.packages.urllib3.response.HTTPResponse object at 0x0000000002EE4A58>

>>> r.text

u'{\n  "args": {}, \n  "data": "", \n  "files": {}, \n  "form": {\n    "key1": "value1", \n    "key2": "value2"\n  }, \n  "headers": {\n    "Accept": "*/*", \n    "Accept-Encoding": "gzip, deflate", \n    "Content-Length": "23", \n    "Content-Type": "application/x-www-form-urlencoded", \n    "Host": "httpbin.org", \n    "User-Agent": "python-requests/2.7.0 CPython/2.7.11 Windows/7"\n  }, \n  "json": null, \n  "origin": "113.98.252.236", \n  "url": "http://httpbin.org/post"\n}\n'

>>> r.content

'{\n  "args": {}, \n  "data": "", \n  "files": {}, \n  "form": {\n    "key1": "value1", \n    "key2": "value2"\n  }, \n  "headers": {\n    "Accept": "*/*", \n    "Accept-Encoding": "gzip, deflate", \n    "Content-Length": "23", \n    "Content-Type": "application/x-www-form-urlencoded", \n    "Host": "httpbin.org", \n    "User-Agent": "python-requests/2.7.0 CPython/2.7.11 Windows/7"\n  }, \n  "json": null, \n  "origin": "113.98.252.236", \n  "url": "http://httpbin.org/post"\n}\n'

>>> payload = {'key1': 'value1', 'key2': 'value2'}

#获取原始响应内容

>>> r = requests.post("http://httpbin.org/post", data=payload,stream=True)

>>> r.raw

<requests.packages.urllib3.response.HTTPResponse object at 0x0000000003105208>

>>> r.raw.read()

'{\n  "args": {}, \n  "data": "", \n  "files": {}, \n  "form": {\n    "key1": "value1", \n    "key2": "value2"\n  }, \n  "headers": {\n    "Accept": "*/*", \n    "Accept-Encoding": "gzip, deflate", \n    "Content-Length": "23", \n    "Content-Type": "application/x-www-form-urlencoded", \n    "Host": "httpbin.org", \n    "User-Agent": "python-requests/2.7.0 CPython/2.7.11 Windows/7"\n  }, \n  "json": null, \n  "origin": "113.98.252.236", \n  "url": "http://httpbin.org/post"\n}\n'

#使用 json 参数直接传递

>>> payload = {'key1': 'value1', 'key2': 'value2'}

>>> r = requests.post("http://httpbin.org/post", json=payload)

>>> r.text

u'{\n  "args": {}, \n  "data": "{\\"key2\\": \\"value2\\", \\"key1\\": \\"value1\\"}", \n  "files": {}, \n  "form": {}, \n  "headers": {\n    "Accept": "*/*", \n    "Accept-Encoding": "gzip, deflate", \n    "Content-Length": "36", \n    "Content-Type": "application/json", \n    "Host": "httpbin.org", \n    "User-Agent": "python-requests/2.7.0 CPython/2.7.11 Windows/7"\n  }, \n  "json": {\n    "key1": "value1", \n    "key2": "value2"\n  }, \n  "origin": "113.98.252.236", \n  "url": "http://httpbin.org/post"\n}\n'

2.5用fiddler查看抓包情况

r.json():json数据,可以看出与2.4中的r.json()值一致。

在这里插入图片描述注:通过json进行传参的json数据
在这里插入图片描述
r.headers:响应头数据,可以看出与2.4中r.headers值一致。
在这里插入图片描述
r.raw:响应原始数据
在这里插入图片描述

2.6 post请求总结

综上所述,通过requests.post(“某url”,data={字典类型参数键值对})模拟浏览器发送一个http的请求(其中请求的方法是post,请求的url地址如下形式

http://httpbin.org/get),服务器处理数据后,会返回一个response对象,通过读取response对象的属性值,如json数据,可以做一系列的断言,从而验证该接口返回的数据是否正确。

3.其他用法

3.1 定制请求头

>>> payload = {'key1': 'value1', 'key2': 'value2'}
>>> headers = {'user-agent': 'my-app/0.0.1'}
>>> r = requests.post("http://httpbin.org/post", data=payload,headers=headers)

在这里插入图片描述
用fiddler抓包,可以看到,发送请求的请求头中的user-agent的值为设置的值。

注意: 所有的 header 值必须是 string、bytestring 或者 unicode。

3.2上传文件

>>> import os

>>> os.getcwd()

'D:\\pythontest'

>>> f=open('1.txt','w+')

>>> f.write('test') 

>>> os.listdir('D:\\pythontest')

['1.txt',]

>>> import requests

>>> url = 'http://httpbin.org/post'

>>> files = {'file': open('1.txt', 'rb')}

>>> r = requests.post(url, files=files) 

>>> r.text

u'{\n  "args": {}, \n  "data": "", \n  "files": {\n    "file": ""\n  }, \n  "form": {}, \n  "headers": {\n    "Accept": "*/*", \n    "Accept-Encoding": "gzip, deflate", \n    "Content-Length": "141", \n    "Content-Type": "multipart/form-data; boundary=37de3eb22a754f34849771891b77bd23", \n    "Host": "httpbin.org", \n    "User-Agent": "python-requests/2.7.0 CPython/2.7.11 Windows/7"\n  }, \n  "json": null, \n  "origin": "113.98.252.236", \n  "url": "http://httpbin.org/post"\n}\n'

在这里插入图片描述用fiddler抓包,可以看到,响应的JSON数据中有file。

3.3 cookies的发送

>>> url = 'http://httpbin.org/cookies'
>>> cookies = dict(cookies_are='working')
>>> r = requests.get(url, cookies=cookies)
>>> r.text
u'{\n "cookies": {\n "cookies_are": "working"\n }\n}\n'

4.知识拓展

4.1关于GET和POST的区别

通过上面抓包可以看出:

GET请求的数据会附在URL之后(就是 把数据放置在HTTP协议头中),以?分割URL和传输数据,参数之间以&相连,如:login.action?name=hyddd& password=idontknow&verify=%E4%BD%A0%E5%A5%BD。如果数据是英文字母/数字,原样发送,如果是空格,转换为+,如果是中文/其他字符,则直接把字符串用BASE64加密,得出如:%E4%BD%A0%E5%A5%BD,其中%XX中的XX为该符号以 16进制表示的ASCII。

POST把提交的数据则放置在是HTTP包的包体中。

4.2关于请求的Headers的Accept-Encoding说明

请求的headers中的client项中的Accept-Encoding的,例如Accept-Encoding: gzip, deflate。浏览器申明自己接收的编码方法,通常指定压缩方法,是否支持压缩,支持什么压缩方法(gzip,deflate)(注意:这不是只字符编码)。

4.3关于响应的Headers的Content-Type说明

Content-Type是返回消息中非常重要的内容,表示后面的文档属于什么MIME类型。

Content-Type: [type]/[subtype]; parameter。例如最常见的就是text/html,它的意思是说返回的内容是文本类型,这个文本又是HTML格式的。原则上浏览器会根据 Content-Type来决定如何显示返回的消息体内容。

type有下面的形式:

Text:用于标准化地表示的文本信息,文本消息可以是多种字符集和或者多种格式的;

Multipart:用于连接消息体的多个部分构成一个消息,这些部分可以是不同类型的数据;

Application:用于传输应用程序数据或者二进制数据;

Message:用于包装一个E-mail消息;

Image:用于传输静态图片数据;

Audio:用于传输音频或者音声数据;

Video:用于传输动态影像数据,可以是与音频编辑在一起的视频数据格式。

subtype用于指定type的详细形式。

parameter可以用来指定附加的信息,更多情况下是用于指定text/plain和text/htm等的文字编码方式的charset参数。

注:如果想要做好web自动化接口测试,必须要了解HTTP协议

在这里插入图片描述
希望本文对你有所帮助~~如果对软件测试、接口测试、自动化测试、面试经验交流感兴趣可以加入我们。642830685,免费领取最新软件测试大厂面试资料和Python自动化、接口、框架搭建学习资料!技术大牛解惑答疑,同行一起交流。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值