好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

话不多说,全是干货!!!Python接口自动化测试

一、基础准备

环境搭建

工欲善其事必先利其器,废话不多说。我们先开始搭建环境。

#?创建项目目录mkdir?InterfaceTesting#?切换到项目目录下cd?InterfaceTesting#?安装虚拟环境创建工具pip?install? Virtual env#?创建虚拟环境,env代表虚拟环境的名称,可自行定义 Virtual env?env#?启动虚拟环境,执行下面命令后会发现路径上有?(env)?字样的标识source?env/Scripts/activate#?查看?(env)?环境下使用的?Python?和?pip?工具版本ls?env/Scripts/#?***?安装?requests?***pip?install?requests#?退出虚拟环境,退出后路径上的?(env)?字样的标识消失cd?env/Scripts/deactivate#?导出环境所需要的模块的清单pip?freeze?>>?requirements.txt#?上传?GitHub?时,将下面项忽略上传echo?env/?>>?.gitignore
echo?InterfaceTesting.iml?>>?.gitignore
echo?__pycache__/?>>?.gitignore#?将代码传至?GitHub#?本地仓初始化git?init#?创建本地仓与?GitHub?仓的远程链接git?remote?add?github?你的github仓的地址#?将代码添加到暂存区git?add?.#?将代码提交到?git?commit?-m?"init?environment"#?将代码上传到GitHub仓中git?push?github?master

初始化环境的项目结构示例如下:

2. 接口基础知识

- [ ] 2.1 接口分类 接口一般来说有两种,一种是程序内部的接口,一种是系统对外的接口。

(1) webservice接口:走soap协议通过http传输,请求报文和返回报文都是xml格式的,我们在测试的时候都要通过工具才能进行调用,测试。 (2) http api接口:走http协议,通过路径来区分调用的方法,请求报文都是key-value形式的,返回报文一般都是json串,有get和post等方法。

- [ ] 2.2 接口请求类型 根据接口的请求方法,常用的几种接口请求方式:

(1) GET:从指定资源获取数据 (2) POST:向指定的资源请求被处理的数据(例如用户登录) (3) PUT:上传指定的URL,一般是修改,可以理解为数据库中的 update (4) DELETE:删除指定资源

二、Requests 快速上手

1. requests基础 ??所有的数据测试目标以一个开源的接口模拟网站【HTTPBIN】为测试对象。

1.1 发送请求

#!/usr/bin/env?python#?-*-?encoding:?utf-8?-*-"""
@File????:???requests_send_request.py
@Time????:???2019/9/2?11:54
@Author??:???Crisimple
@Github?:????https://crisimple.github.io/
@Contact?:???Crisimple@foxmail测试数据
@License?:???(C)Copyright?2017-2019,?Micro-Circle
@Desc????:???None
"""import?requests#?1.requests请求方式#?(1)?GET请求方式httpbin_get?=?requests.get('http://httpbin.org/get',?data={'key':?'value'})print('httpbin_get:?',?httpbin_get.text)#?(2)?POST请求方式httpbin_post?=?requests.post('https://httpbin.org/post',?data={'key':?'value'})print('httpbin_post:?',?httpbin_post.text)#?(3)?PUT请求方式
?httpbin_put?=?requests.put('https://httpbin.org/put',?data={'key':?'value'})print('httpbin_put:?',?httpbin_put.text)#?(4)?DELETE请求方式httpbin_delete?=?requests.delete('https://httpbin.org/delete',?data={'key':?'value'})print('httpbin_delete',?httpbin_delete)#?(5)?PATCH亲求方式httpbin_patch?=?requests.patch('https://httpbin.org/patch',?data={'key':?'value'})print('httpbin_patch',?httpbin_patch)

1.2 参数传递 ??常用的参数传递形式有四种: 【GitHub示例】

(1)字典形式的参数:payload = {‘key1’: ‘value1’, ‘key2’: ‘value2’}(2) 元组形式的参数:payload = ((‘key1’, ‘value1’), (‘key2’, ‘value2’))(3) 字符串形式的参数:payload = {‘string1’, ‘value1’}(4) 多部份编码的文件:files = {# 显示设置文件名、文件类型和请求头’file’: (‘report.xls’, open(‘report.xls’, ‘rb’), ‘application/vnd.ms-excel’, {‘Expires’: ‘0’})}

#!/usr/bin/env?python#?-*-?encoding:?utf-8?-*-"""
@File????:???response_code.py
@Time????:???2019/9/2?15:41
@Author??:???Crisimple
@Github?:????https://crisimple.github.io/
@Contact?:???Crisimple@foxmail测试数据
@License?:???(C)Copyright?2017-2019,?Micro-Circle
@Desc????:???None
"""import?requests#?1.?返回接口状态码:200def?response_200_code():interface_200_url?=?'https://httpbin.org/status/200'response_get?=?requests.get(interface_200_url)response_get_code?=?response_get.status_codeprint('response_get_code:?',?response_get_code)response_200_code()#?2.返回接口状态码:400def?response_400_code():interface_400_url?=?'https://httpbin.org/status/400'response_get?=?requests.get(interface_400_url)response_get_code?=?response_get.status_codeprint('response_get_code:?',?response_get_code)response_400_code()

(2)响应头

#!/usr/bin/env?python#?-*-?encoding:?utf-8?-*-"""
@File????:???response_content.py
@Time????:???2019/9/2?15:41
@Author??:???Crisimple
@Github?:????https://crisimple.github.io/
@Contact?:???Crisimple@foxmail测试数据
@License?:???(C)Copyright?2017-2019,?Micro-Circle
@Desc????:???None
"""import?requests#?1.?返回接口状态码:#?(1).?返回接口状态码:200def?response_200_code():interface_200_url?=?'https://httpbin.org/status/200'response_get?=?requests.get(interface_200_url)response_get_code?=?response_get.status_codeprint('response_get_code:?',?response_get_code)response_200_code()#?(2).返回接口状态码:400def?response_400_code():interface_400_url?=?'https://httpbin.org/status/400'response_get?=?requests.get(interface_400_url)response_get_code?=?response_get.status_codeprint('response_get_code:?',?response_get_code)response_400_code()#?(3)?重定向接口返回状态码:301def?response_301_code():interface_url?=?'https://butian.360.cn'response_get?=?requests.get(interface_url)response_get_code?=?response_get.status_codeprint('response_get_code:?',?response_get_code)response_301_code()#?------------------------------------------------------#?2.?响应内容??响应内容的请求头、查看文本、编码方式、二进制响应、原始响应。def?response_contents():url?=?'https://httpbin.org/get'response_get?=?requests.get(url=url)#?响应头print('response_get_headers',?response_get.headers)#?响应文本print('response_get_text:?',?response_get.text)#?文本编码方式print('response_get_encoding:?',?response_get.encoding)#?二进制响应内容print('response_get_content:?',?response_get.content)#?原始响应内容origin_content?=?response_get.raw
????origin_content_read?=?origin_content.read(10)print('origin_content:?',?origin_content)print('origin_content_read:?',?origin_content_read)response_contents()

1.4 接口其他处理 【GitHub示例】

(1) 操作cookies

import?requestsimport?time

url?=?'https://httpbin.org/get'def?operator_cookies():r?=?requests.get(url)print('r.cookies:?',?r.cookies)jar?=?requests.cookies.RequestsCookieJar()jar.set('tasty_cookie',?'yum',?domain='httpbin.org',?path='/cookies')jar.set('gross_cookie',?'blech',?domain='httpbin.org',?path='/elsewhere')r2?=?requests.get(url=url,?cookies=jar)print('r2.text',?r2.text)operator_cookies()

(2) 请求历史

import?requests

url?=?'https://httpbin.org/get'def?request_history():r?=?requests.get(url=url)print('r.history:?',?r.history)request_history()

(3) 超时请求

requests?在经过?timeout?参数设定的秒数时间之后停止等待响应。import?requestsimport?timedef?timeout():print(time.time())url?=?'https://httpbin.org/get'print(time.time())r?=?requests.get(url,?timeout=5)print(time.time())timeout()

(4) 错误与异常

常见的错误异常有:

· 遇到网络问题(如:DNS 查询失败、拒绝连接等时),requests 会抛出一个 ConnectionError 异常。· 如果 HTTP 请求返回了不成功的状态码, Response.raise_for_status() 会抛出一个 HTTPError异常。· 若请求超时,则超出一个 Timeout 异常。· 若请求超过了设定的最大重定向次数,则会抛出一个 TooManyRedirects 异常。· 所有 Requests 显式抛出的异常都继承自 requests.exceptions.RequestsException。

2.requests 高级应用 2.1 会话对象 2.2 请求与响应对象 2.3 准备的请求 2.4 SSL证书验证 2.5 客户端证书

今天小编先码到这,有想学Python接口自动化测试的可以加我们的群(785128166),里面有各种资源,面试题,视频资源,还有大牛为你解疑答惑

查看更多关于话不多说,全是干货!!!Python接口自动化测试的详细内容...

  阅读:32次