IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 开发测试 -> 接口测试框架基本能力介绍 -> 正文阅读

[开发测试]接口测试框架基本能力介绍

接口测试框架基本能力

项目管理 pip , virtualenv

用例编写: pytest

领域能力:app, web, http

执行调度:pytest, pycharm, shell , jenkins

测试报告:allure2

HTTP测试能力

请求方法构造:get, postm put ,delete, head…
请求体构造:form, json, xml, binary
响应结果分析:status code, response body, json path, xpath

Requests框架特点

  • 功能全面: http/https支持全面
  • 使用简单:简单易用,不用关心底层细节
  • 定制性高:借助于hook机制完成通用处理
常见http请求构造方法
r = requests.put(url, data={"key":"value})
r = requests.delete()
r = requests.head()
r = requests.options()      
                            
r.status_code                            
                            
请求目标构造

url , query

请求参数构造
  • get query: path, query
  • post body:
    • form
    • 结构化请求:json, xml., json rpc
    • binary
GetQuery请求

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

r = requests.get('https://httpbin.org.get', param=payload)

Form请求参数构造
# 用来模拟登录

payload = {'key1':'value1', 'key2':'value2'}
res = requests.post("https://httpbin.org/post", data=payload)
文件上传
files = {'file':open('report.xls', 'rb')}
res = requests.post(url, files=files)
header构造
  • 普通的header:

    headers = {'user-agent': 'my-app/0.1.1'}
    rest = requests.get(urel, headers=headers)
    
    
  • cookie

    cookies = dict(cookies_are = 'working')
    res = requests.get(url, cookies=cookies)		
    

    ?

响应结果
  • 基本信息

    r.url, r.status_code, r.headers, r.cookies

  • 响应结果

    r.text = r.encoding + r.content
    r.json() = r.encoding+ r.content+ content type json
    r.raw.read(10)
    
    
  • 对应的请求内容:r.request

JSON请求体构造

payload = {"some":"data"}

res = requests.post(url, json=payload)

xml请求
import requests


xml = """
<?xml version='1.0' encoding='utf-8'?>
<a>666</a>
"""
headers = {'Content-Type':'application/xml'}
res = requests.post("http://httpbin.org/post", data=xml, headers=headers).text
复杂数据解析
  • 数据保存:将复杂的xml或者json请求体保存到文件模板中

  • 数据处理:

    • 使用mustache, freemaker等工具解析
    • 简单的字符串替换
    • 使用json xml api进行结构化解析
  • 数据生成:输出最终结果

    ?

模板技术mustache
  • import pystache
    
    
    pystache.render(
    	"Hi {{person}}!",
    	{"person":"uli_bo"}
    )
    
    
    ########### result
    Hi uli_bo!
    
    
    
结构化响应断言 JSON XML
def test_hog(self):
	url="https://home.testing-studio.com/categories.json"
	res = requests.get(url)
	assert res.json()['']

json path

XPATHJSONPathResult
/store/book/author$.store.book[*].authorthe author of all books in the st
//author$…authorall authors
/store/*$.store.*all things in store, which are some books and a red bicycle.
/store//price$.store…pricethe price of everything in the store.
//book[3]$…book[2]the third book
//book[position()< 3]$…book[0,1]the first two books
$…book[:2]
//book[last()]$…book[(@.length-1)]the last book in order
$…book[-1:]
//book[isbn]$…book[?(@.isbn)]filter all books with isbn number
//book[price<10]$…book[?(@.price<10)]filter all books cheapier than 10
//*$…*all Elements in XML document. All members of JSON stricture.

XPATH

from requests_xml import XMLSession
session = XMLSession()
r = ression.get("https://www.nasa.gov/rss/dyn/lg_image_of_the_day.rss")
r.xml.links
item = r.xml.xpath('//item', first=True)
print(item.text)

XML解析

import xml.etree.ElementTree as ET


root = ET.fromstring(countrydata)

root.findall(".")
root.findall("./country/neighbor")
root.findall(".//year/..[@name='Singapore']")
root.findall(".//*[@name='Singapore']/year")
root.findall(".//neihbor[2]")

hamcrest断言体系

安装:pip install pyhamcrest

  • 扩建自带assert 体系:assert, assertEqual

  • Hamcrest体系:assertThat

  • Object

    • equal_to
    • has_length
    • has_property
    • has_properties
    • has_string
    • instance_of
    • none, not_none
    • same_instance
    • calling, raises
  • Number

    • close_to
    • greater_than
  • Text

    • contains_string
    • ends_with
    • equal_to_ignoring_case
    • equal_to_ignoring_whitespace
    • matches_regexp
    • starts_with
    • string_contains_in_order
  • Logical

    • all_of - and together all matchers
    • any_of - or together all matchers
    • anything - match anything, useful in composite mathcers when you don’t want
    • is_not,not_ - negate the matcher
  • Sequence

    • contains - exactly match the entire squence
    • contains_inanyorder - match the entire sequence, but in any order
    • has_item -match if given item appears in the squence
    • has_items -match if all given items appear in the sequence, in any oder
    • is_in - match if item appears in the given squence
    • only_contains -match if sequence’s items appear in given list
    • empty - match if the sequence is empty
  • Dictory

    • has_entries - match dictionary with list of key-value pairs
    • has_entry- match dictionary containing a key-value pair
    • has_key- match dictionary with a key
    • has_value - match dictionary with a value

    ?

schema校验
  • 相关网站:https://jsonschema.net/ http://json-schema.org
  • 生成schema文件
  • 根据需要添加自定义规则
使用简单案例:
from jsonschema import validate


def test_get_login_jsonschema(self):
	url = "https://testerhome.com/api/v3/topics.json"
	data = requests.get(url, params={"limit":"2"}).json()
	schema = json.load(open("topice_schema.json"))
	validate(data, schema=schema)
schema自动校验
  • 每次运行的时候自动保存当前的schema
  • 下次运行对比上次的schema如果大仙变更就报错
  • saveSchema + diffSchema
Cookie简介
  • 在接口测试过程中,很多情况下需要发送的请求附带cookies,才会得到正常的想用结果,所以使用python+ requests进行接口自动化测试也是同理,需要在构造接口测试用例时加入cookie
  • 传递Cookie的两种方式
    • 通过请求头信息传递
    • 通过请求的关键字参数cookies传递
自定义header
  • 以字典键值对的形式设置

    url = "https://httpbin.testing-studio.com/cookies"
    headers = {"Cookies":"working=1", "User-Agent":"python-requests/2.23.0"}
    res = requests.get(url, headers=headers)
    print(res.request.headers)
    
HTTP Basic
  • 基本认证(Basic access authentication) 是允许http用户代理(如:网页浏览器)在请求是,提供 用户名和密码的一种方式

  • GET / private/index.html HTTP/1.0
    Host: localhost
    Authorization: Basic QWxdfaaeETQAGDAFDASF==
    
  • 演示

    • 自我搭建本地测试环境:$ docker run -p 80:80 kennethreitz/httpbin

    • 使用charles抓包查看具体请求信息

    • 在自动化测试的过程中,可以使用auth参数传递认证信息

      import requests
      from requests import HTTPBasicAuth
      
      
      def test_oauth(self):
      	res = requests.get("https://home.testing-studio.com//basic-auth/aaa/123",
          auth=HTTPBasicAuth("aaa","123"))
          print(res.text)
          
          
      ######result
      PASSED                       [100%]{
        "authenticated": true, 
        "user": "aaa"
      }
      
      

      ?

  开发测试 最新文章
pytest系列——allure之生成测试报告(Wind
某大厂软件测试岗一面笔试题+二面问答题面试
iperf 学习笔记
关于Python中使用selenium八大定位方法
【软件测试】为什么提升不了?8年测试总结再
软件测试复习
PHP笔记-Smarty模板引擎的使用
C++Test使用入门
【Java】单元测试
Net core 3.x 获取客户端地址
上一篇文章           查看所有文章
加:2021-07-14 23:15:03  更:2021-07-14 23:15:41 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/7 16:23:41-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码