登录
转载

python自动化(五)接口自动化:2.接口测试requests模块

发布于 2021-06-07 阅读 380
  • 后端
  • Python
转载

一.requests介绍

  • requests模块是python的第三方库,使用它我们可以非常方便的发起get,post等接口请求。并且获取接口的返回值。在自动化中我们常常使用requests模块来编写接口自动化测试用例。

  • requests的官方文档为:https://docs.python-requests.org/zh_CN/latest/

二.requests详解

1.安装

pip install requests

2.常用API

2.1发送请求相关的API

(1) 发送get请求API

requests.get('https://github.com/ouyi-github/') # 不带参数
requests.get(url='http://dict.baidu.com/s', params={'wd':'python','from':'zici'}) # 带参数。方式1
requests.get(url='https://dict.baidu.com/s?wd=python&from=zici') # 带参数。方式2

(2)发送post请求API

r = requests.post('http://httpbin.org/post', data = {'key':'value'}) #以form表单形式提交数据
r = requests.post('https://api.github.com/some/endpoint', data=json.dumps({'some': 'data'})) # 以json形式提交数据,方式1
r = requests.post('https://api.github.com/some/endpoint', json={'some': 'data'}) # 以json形式提交数据,方式2

# xml格式提交数据
heards = {'Content-type': 'text/xml'}
XML = '<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><Request xmlns="http://tempuri.org/"><jme><JobClassFullName>WeChatJSTicket.JobWS.Job.JobRefreshTicket,WeChatJSTicket.JobWS</JobClassFullName><Action>RUN</Action><Param>1</Param><HostIP>127.0.0.1</HostIP><JobInfo>1</JobInfo><NeedParallel>false</NeedParallel></jme></Request></soap:Body></soap:Envelope>'
url = 'http://jobws.push.mobile.xxxxxxxx.com/RefreshWeiXInTokenJob/RefreshService.asmx'
r = requests.post(url=url, heards=heards, data=XML)

(3)发送其他请求API

r = requests.put('http://httpbin.org/put', data = {'key':'value'})
r = requests.delete('http://httpbin.org/delete')
r = requests.head('http://httpbin.org/get')
r = requests.options('http://httpbin.org/get')

2.2 获取响应相关的API

r.encoding                       #获取当前的编码
r.encoding = 'utf-8'             #设置编码
r.text                           #以encoding解析返回内容。字符串方式的响应体,会自动根据响应头部的字符编码进行解码。
r.content                        #以字节形式(二进制)返回。字节方式的响应体,会自动为你解码 gzip 和 deflate 压缩。
r.headers                        #以字典对象存储服务器响应头,但是这个字典比较特殊,字典键不区分大小写,若键不存在则返回None
r.status_code                     #响应状态码
r.raw                             #返回原始响应体,也就是 urllib 的 response 对象,使用 r.raw.read()   
r.ok                              # 查看r.ok的布尔值便可以知道是否登陆成功
 #*特殊方法*#
r.json()                         #Requests中内置的JSON解码器,以json形式返回,前提返回的内容确保是json格式的,不然解析出错会抛异常
r.raise_for_status()             #失败请求(非200响应)抛出异常
r.cookies                        # 响应中的cookies

2.3 文件上传API

url = 'http://127.0.0.1:8080/upload'
files = {'file': open('/home/rxf/test.jpg', 'rb')}
r = requests.post(url, files=files)

2.4 请求中添加头部信息

data = {'some': 'data'}
headers = {'content-type': 'application/json',
           'User-Agent': 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:22.0) Gecko/20100101 Firefox/22.0'}
           
r = requests.post('https://api.github.com/some/endpoint', data=data, headers=headers)

2.5 请求中添加cookie信息

data = {'some': 'data'}
cookie = {'key':'value'}
r = requests.post('https://api.github.com/some/endpoint',data=data,cookies=cookie)

2.6 复杂数据添加到请求中

在这里插入图片描述 在这里插入图片描述

实战:企业微信添加成员API

第一步:分析企业微信接口文档

https://work.weixin.qq.com/api/doc/90000/90135/90195

第二步:将请求数据写入json文件

{
    "userid": "{{userid}}",
    "name": "{{name}}",
    "department": [1, 2],
    "mobile": "{{phone}}"
}

第三步;在代码中修改json文件内容,并作为请求的数据发送

import requests
import pystache

class TestDemo:
    def test_demo(self):
        with open('data/add_remenber.json','r',encoding='utf-8') as f:
            contect = f.read()
            rewrite = {
                'userid':'ojbkdsb1',
                'name':'ojbkdsbsd2',
                'phone':'13118175697'
            }
            re_contect = pystache.render(contect,rewrite) # 替换内容中的变量,contect为代替换的内容(需要为str),rewrite为替换规则
            print(re_contect)
            url = 'https://qyapi.weixin.qq.com/cgi-bin/user/create?access_token=3pFM-B9TUPfVp1LXzADf91gnk1Nuxlr92AlyZg0QYWF0G8VnXS5OIUOykSLBhAswELF_' \
                  'IcKkZ2cWlRtaXObX0fh6KpWv4-MgdAxtZJ' \
                  'j19zqxnWlHgWQfXeB9i3zPRgVt3Ohb1WPR9kRj72O1QXy9L1z1azQGmE_2RnIIqDcChIKEorbpSWj_ez0iHchE-9dkzIHsDZoMtNDekY3RVvCkyw&debug=1'
            r = requests.post(url,data=re_contect)
            print(r.json())

这样我们可以将复杂的请求数据存放在文件中,在使用代码修改数据内容后使用。

2.7 使用响应中的复杂内容断言

在做接口自动化测试时,有时我们需要使用响应的json或者xml数据作为断言。而有些数据嵌套特别深,这时我们如果从第一层往里取这个数据,会使得表达式特别长。

这时我们可以使用json path来获取json中的数据

(1)json path

安装:

pip install jsonpath
from jsonpath import jsonpath
jsonpath(json_content,'$.store.book[0].price') # 两个参数:json_content就是json内容,'$.store.book[0].price'是jsonpath表达式

JsonPath中的“根成员对象”始终称为$,无论是对象还是数组。

JsonPath表达式可以使用点表示法

$.store.book [0].title

或括号表示法

$['store']['book'][0]['title']

操作说明
$查询根元素。这将启动所有路径表达式。
@当前节点由过滤谓词处理。
*通配符,必要时可用任何地方的名称或数字。
深层扫描。 必要时在任何地方可以使用名称。
.点,表示子节点
[’’ (, ‘’)]括号表示子项
[ (, )]数组索引或索引
[start:end]数组切片操作
[?()]过滤表达式。 表达式必须求值为一个布尔值。

函数可以在路径的尾部调用,函数的输出是路径表达式的输出,该函数的输出是由函数本身所决定的。

函数描述输出
min()提供数字数组的最小值Double
max()提供数字数组的最大值Double
avg()提供数字数组的平均值Double
stddev()提供数字数组的标准偏差值Double
length()提供数组的长度Integer

过滤器是用于筛选数组的逻辑表达式。一个典型的过滤器将是[?(@.age > 18)],其中@表示正在处理的当前项目。 可以使用逻辑运算符&&和||创建更复杂的过滤器。 字符串文字必须用单引号或双引号括起来([?(@.color == ‘blue’)] 或者 [?(@.color == “blue”)]).

操作符描述
==left等于right(注意1不等于’1’)
!=不等于
<小于
<=小于等于
>大于
>=大于等于
=~匹配正则表达式[?(@.name =~ /foo.*?/i)]
in左边存在于右边 [?(@.size in [‘S’, ‘M’])]
nin左边不存在于右边
size(数组或字符串)长度
empty(数组或字符串)为空

实例:

{
    "store": {
        "book": [
            {
                "category": "reference",
                "author": "Nigel Rees",
                "title": "Sayings of the Century",
                "price": 8.95
            },
            {
                "category": "fiction",
                "author": "Evelyn Waugh",
                "title": "Sword of Honour",
                "price": 12.99
            },
            {
                "category": "fiction",
                "author": "Herman Melville",
                "title": "Moby Dick",
                "isbn": "0-553-21311-3",
                "price": 8.99
            },
            {
                "category": "fiction",
                "author": "J. R. R. Tolkien",
                "title": "The Lord of the Rings",
                "isbn": "0-395-19395-8",
                "price": 22.99
            }
        ],
        "bicycle": {
            "color": "red",
            "price": 19.95
        }
    },
    "expensive": 10
}

[$.store.book[*].author]获取json中store下book下的所有author值
[$..author]获取所有json中所有author的值
[$.store.*]store下的所有的东西
[$.store..price]获取json中store下所有price的值
[$..book[2]]获取json中book数组的第3个值
[$..book[-2]]倒数的第二本书
[$..book[0,1]]前两本书
[$..book[:2]]从索引0(包括)到索引2(排除)的所有图书
[$..book[1:2]]从索引1(包括)到索引2(排除)的所有图书
[$..book[-2:]]获取json中book数组的最后两个值
[$..book[2:]]获取json中book数组的第3个到最后一个的区间值
[$..book[?(@.isbn)]]获取json中book数组中包含isbn的所有值
[$.store.book[?(@.price < 10)]]获取json中book数组中price<10的所有值
[`$…book[?(@.price <= $[‘expensive’])]获取json中book数组中price<=expensive的所有值
[$..book[?(@.author =~ /.*REES/i)]]获取json中book数组中的作者以REES结尾的所有值(REES不区分大小写)
[$..*]逐层列出json中的所有值,层级由外到内
[$..book.length()]获取json中book数组的长度

2.8 jsonschema断言

(1)什么是jsonschema

JSON Schema是基于JSON格式,用于定义JSON数据结构以及校验JSON数据内容。

(2)为什么需要jsonschema断言

在实际开发过程中,接口返回的json数据十分庞大,如果我们需要对每一个数据都做断言,会导致断言代码量过大,且不灵活。

使用JSON Schema可以自动判断json数据类型是否符合我们预期

(3)使用jsonschema

第一步:编写jsonschema文件,来定义json数据类型格式
// filename = my_schema.json
{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "TestInfo",
    "description": "some information about test",
    "type": "object",
    "properties": {
        "name": {
            "description": "Name of the test",
            "type": "string"
        },
        "age": {
            "description": "age of test",
            "type": "integer"
        }
    },
    "required": [
        "name", "age"
    ]
}

jsonschema关键字的含义和作用

关键字描述
$schema表示该JSON Schema文件遵循的规范
title为该JSON Schema文件提供一个标题
description外层表示关于该JSON Schema文件的描述信息,内层表示关于该JSON数据的描述信息
type表示待校验元素的类型(例如,最外层的type为object表示待校验的是一个JSON对象,内层type分别表示待校验的元素类型为,整数,字符串,数字)
properties定义待校验的JSON对象中,各个key-value对中value的限制条件
required定义待校验的JSON对象中,必须存在的key
minimum用于约束取值范围,表示取值范围应该大于或等于minimum
exclusiveMinimum如果minimum和exclusiveMinimum同时存在,且exclusiveMinimum的值为true,则表示取值范围只能大于minimum
maximum用于约束取值范围,表示取值范围应该小于或等于maximum
exclusiveMaximum如果maximum和exclusiveMaximum同时存在,且exclusiveMaximum的值为true,则表示取值范围只能小于maximum
multipleOf用于约束取值,表示取值必须能够被multipleOf所指定的值整除
maxLength字符串类型数据的最大长度
minLength字符串类型数据的最小长度
pattern使用正则表达式约束字符串类型数据

type常见的取值

type取值对应的python数据类型
objectObject
arrayList
integerint
numberfloat或int
nullNone
boolean.Boolean
stringString
第二步:代码中使用jsonschema断言
import json
from jsonschema import validate
class TestDemo:
    def test_demo(self):
        # json内容
        json_content = {
            "name": "python",
            "age": 25
        }

        # schema文件
        with open('data/my_schema.json','r',encoding='utf-8') as f:
            my_schema = json.load(f)

        # 校验
        validate(instance=json_content,schema=my_schema) # 参数instance表示待校验的json数据,schema表示schema规则

2.9 请求中添加认证信息

一些http请求需要在请求数据中添加认证信息,才能正确响应。 在这里插入图片描述

那么如何在requests中添加认证信息呢??

在这里插入图片描述

2.10 requests请求发送图片

以企业微信上传图片为例:

class MaterialPage(BasePage):
    """素材管理page类"""
    def up_image(self,filename):
        """
        上传图片
        :return:
        """
        filedir = os.path.join(IMAGESDIR,filename)
        with open(file=filedir,mode='rb') as f:
            file = {"file": f}
            r = self.requests.post("https://qyapi.weixin.qq.com/cgi-bin/media/upload?type=image",files=file)
        return r

第一步:将图片读取为二进制文件

第二步:将读取的二进制文件,存进字典中,key为“file”

第三步:将字典传递到参数files中,发送请求

评论区

admin
14粉丝

打江山易,守江山难,负重前行,持续创新。

0

0

0

举报