最近参加了一次新公司测试团队技术分享会,有大佬分享了关于接口自动化框架【python+requests+ddt+unittest+jenkins】,印象很深刻的是他的脚本测试用例的设计和request()方法的运用。因为我之前做的分享都是关于某个请求方法的运用,这次特地对request()方法做个分享。
def request(method, url, **kwargs):
"""Constructs and sends a :class:`Request `.
:param method: method for the new :class:`Request` object.
:param url: URL for the new :class:`Request` object.
:param params: (optional) Dictionary, list of tuples or bytes to send
in the query string for the :class:`Request`.
:param data: (optional) Dictionary, list of tuples, bytes, or file-like
object to send in the body of the :class:`Request`.
:param json: (optional) A JSON serializable Python object to send in the body of the :class:`Request`.
:param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.
:param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`.
:param files: (optional) Dictionary of ``'name': file-like-objects`` (or ``{'name': file-tuple}``) for multipart encoding upload.
``file-tuple`` can be a 2-tuple ``('filename', fileobj)``, 3-tuple ``('filename', fileobj, 'content_type')``
or a 4-tuple ``('filename', fileobj, 'content_type', custom_headers)``, where ``'content-type'`` is a string
defining the content type of the given file and ``custom_headers`` a dict-like object containing additional headers
to add for the file.
:param auth: (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth.
:param timeout: (optional) How many seconds to wait for the server to send data
before giving up, as a float, or a :ref:`(connect timeout, read
timeout) ` tuple.
:type timeout: float or tuple
:param allow_redirects: (optional) Boolean. Enable/disable GET/OPTIONS/POST/PUT/PATCH/DELETE/HEAD redirection. Defaults to ``True``.
:type allow_redirects: bool
:param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.
:param verify: (optional) Either a boolean, in which case it controls whether we verify
the server's TLS certificate, or a string, in which case it must be a path
to a CA bundle to use. Defaults to ``True``.
:param stream: (optional) if ``False``, the response content will be immediately downloaded.
:param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair.
:return: :class:`Response ` object
:rtype: requests.Response
Usage::
>>> import requests
>>> req = requests.request('GET', 'https://httpbin.org/get')
"""
# By using the 'with' statement we are sure the session is closed, thus we
# avoid leaving sockets open which can trigger a ResourceWarning in some
# cases, and look like a memory leak in others.
with sessions.Session() as session:
return session.request(method=method, url=url, **kwargs)
上图是源码,可以看到request()方法有3个参数(method、url、**kwargs):
method:请求方法,常见有GET请求,POST请求【此外还有HEAD、PUT、PATCH、DELETE、OPTIONS】(前6种就是HTTP协议所对应的请求方式,OPTIONS事实上是向服务器获取一些服务器跟客户端能够打交道的参数)
url:请求的URL地址
**kwargs 是一个可变的参数类型,在传实参时,以关键字参数的形式传入,python会自动解析成字典的形式。
介绍下一些最最常用的可选参数:
params: 字典或元组列表或字节,作为参数增加到url中;一般用于get请求,post请求也可用(不常用)。
data:字典,元组列表,字节或文件对象,作为post请求的参数。
json: JSON格式的数据,作为post请求的json参数。
files: 字典类型,传输文件,作为post请求文件流数据。
headers: 字典, HTTP请求头信息。
我在本地电脑模拟几个接口来做这部分的分享;可以先看看Get请求在JMeter的多种请求方式,方便跟我下面设计的用例做对比
在request()方法允许使用params关键字参数,以一个字符串字典来提供这些参数,params 往url后面添加参数的。
此图用例是传的URL已经添加实际参数
上面图片中的写法只是加个思路,加深认识而已。
交流技术 欢迎+QQ 153132336 zy
个人博客 https://blog.csdn.net/zyooooxie