若有任何学习问题,能够添加做者微信:lockingfreehtml
Python接口测试实战1(上)- 接口测试理论
Python接口测试实战1(下)- 接口测试工具的使用
Python接口测试实战2 - 使用Python发送请求
Python接口测试实战3(上)- Python操做数据库
Python接口测试实战3(下)- unittest测试框架
Python接口测试实战4(上) - 接口测试框架实战
Python接口测试实战4(下) - 框架完善:用例基类,用例标签,从新运行上次失败用例
Python接口测试实战5(上) - Git及Jenkins持续集成
Python接口测试实战5(下) - RESTful、Web Service及Mock Server前端
更多学习资料请加QQ群: 822601020获取java
参考连接: https://blog.csdn.net/lch2848508/article/details/72729658node
REST:表述性状态转移或表现层状态转移,“表现”及每一个接口地址(URI)都表现为(视为)一个资源对象(文本资源、图片资源、服务资源),状态转移指经过POST/PUT方法发送完整的新状态信息来更改资源对象的状态
如某https://api.***.com/user资源状态为{"name": "Kaka", "age": 30}
,咱们经过POST/PUT请求发送新状态{"name": "Kaka", "age": 18}
来更新对象信息,完成状态转移git
URI 与URL的区别:URL值包含协议的连接,如
https://www.baidu.com
, 还有一种相对连接叫URN,如/doc/1.html
,这两种都能惟必定位一个资源,URI(统一资源定位符)包含URL和URNgithub
RESTful API是一种接口设计风格或规范,主要有如下特色:web
https://api.example.com/
https://api.example.com/v1/
https://api.example.com/v1/zoos
https://api.example.com/v1/zoos?limit=10 # 获取前10个
示例:数据库
https://api.github.com
受权 Basic Auth (superhin001, ***) 或 Oauth 2.0 Access Token: 1c4f679300f29ee4e7041028d49e504b9da145b1
json
GET https://api.github.com/user 获取用户信息
flask
POST/PATCH https://api.github.com/user 修改用户信息
POST/PATCH 数据
{ "login": "superhin001", "id": 21163682, "node_id": "MDQ6VXNlcjIxMTYzNjgy", "avatar_url": "https://avatars3.githubusercontent.com/u/21163682?v=4", "gravatar_id": "", "url": "https://api.github.com/users/superhin001", "html_url": "https://github.com/superhin001", "followers_url": "https://api.github.com/users/superhin001/followers", "following_url": "https://api.github.com/users/superhin001/following{/other_user}", "gists_url": "https://api.github.com/users/superhin001/gists{/gist_id}", "starred_url": "https://api.github.com/users/superhin001/starred{/owner}{/repo}", "subscriptions_url": "https://api.github.com/users/superhin001/subscriptions", "organizations_url": "https://api.github.com/users/superhin001/orgs", "repos_url": "https://api.github.com/users/superhin001/repos", "events_url": "https://api.github.com/users/superhin001/events{/privacy}", "received_events_url": "https://api.github.com/users/superhin001/received_events", "type": "User", "site_admin": false, "name": "我是韩老师", "company": null, "blog": "", "location": null, "email": "superhin@126.com", "hireable": null, "bio": null, "public_repos": 3, "public_gists": 0, "followers": 0, "following": 0, "created_at": "2016-08-22T01:12:32Z", "updated_at": "2018-09-14T02:33:43Z", "private_gists": 0, "total_private_repos": 0, "owned_private_repos": 0, "disk_usage": 45430, "collaborators": 0, "two_factor_authentication": false, "plan": { "name": "free", "space": 976562499, "collaborators": 0, "private_repos": 0 } }
GET https://api.github.com/user/keys 获取用户全部SSH-Key信息
POST https://api.github.com/user/keys 新建Key
POST 数据
{ "id": 30742411, "key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDfsTJs7mNWstJ+tO6O1jQHKdDdnldqlqkO0gAune9EH7oqICD1hP7c1duNZwvNnvyGa7SyqamIpNXmSYv303FEVAXzPsb9MzCChG16gzevQtbIX4Qt7vFOsHNSCikSCD/s6DMa0Koryiu7Yju5mW9UUnjVM+a1P80SOiK7p2UBQPFVKRrUtr0htV3U6a2rdP51Vzm2UCjChTUa4q7L3m4C7oB9aSvUsNTk+PmuJlAer4oOd7FsNPqD1Or3lRKAmgxbTX4xTaOkwibK0t2eYkh/VTUPMQ9wDwpa4hZLiEq9qSew3McCwsl70k4H0H4F/VwV2sSCXqZu274YmNDT5Hl3 hanzhichao@hanzhichao01", "title": "test3", "verified": true, "created_at": "2018-09-14T09:54:51Z", "read_only": false }
Web Service 是一种跨平台(Java对象,Python也能够调用)RPC(远程方法调用)解决方案。
基于SOAP协议,使用XML这种跨平台语言传输对远程方法的调用信息及返回结果,并提供WSDL接口描述服务
SOAP协议基于XML语言, SOAP消息体首先必须有个信封(Enelope),信封中能够有信息头(Header)和信息体(Body),其中Body中还能够包含错误信息(Fault)
基本格式以下:
<!-- 信封固定格式 指定命名空间为soapenv --> <soapenv:Envelope xmlns:soapenv="http://www.w3.org/2001/12/soap-envelope" soapenv:encodingStyle="http://www.w3.org/2001/12/soap-encoding"> <soapenv:Header> <!--信息头 可选,可写成但标签--> ...... </soapenv:Header> <soapenv:Body> <!--信息体 实际调用内容--> ...... </soapenv:Body> </soap:Envelope>
SOAP详细教程:http://www.runoob.com/soap/soap-header.html
使用SoupUI
示例接口: http://115.28.108.130:4000/?wsdl
因为Postman等不具有将wsdl接口信息解析成对象描述的功能,咱们使用另外一个SOAP接口专用的测试工具SoupUI
SoupUI下载地址: http://www.wmzhe.com/soft-32815.html
1.新建项目
2.发送接口
使用Fiddler抓包,查看raw格式:
POST http://115.28.108.130:4000/ HTTP/1.1 Accept-Encoding: gzip,deflate Content-Type: text/xml;charset=UTF-8 SOAPAction: "addUser" Content-Length: 370 Host: 115.28.108.130:4000 Connection: Keep-Alive User-Agent: Apache-HttpClient/4.1.1 (java 1.5) <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:user="UserService"> <soapenv:Header/> <soapenv:Body> <user:addUser> <!--Optional:--> <user:name>范冰冰</user:name> <!--Optional:--> <user:password>123456</user:password> </user:addUser> </soapenv:Body> </soapenv:Envelope>
使用Postman发送SOAP接口
text/xml 和 application/xml的区别: 当你 指定编码的时候,使用application/xml会安装xml指定的编码传输,而使用text/html会默认使用us-ascii编码编码传输数据
使用Python操做Web service接口
pip install suds-jurko
from suds.client import Client service = Client("http://115.28.108.130:4000/?wsdl").service # 获取远端服务对象 result = service.addUser("范冰冰", "123456") # 向本地方法同样调用 print(result) # 输出 用户已存在
使用requests库发送
import requests url = 'http://115.28.108.130:4000/' data = '''<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:user="UserService"> <soapenv:Header/> <soapenv:Body> <user:addUser> <!--Optional:--> <user:name>张三</user:name> <!--Optional:--> <user:password>123456</user:password> </user:addUser> </soapenv:Body> </soapenv:Envelope> '''.encode('utf-8') res = requests.post(url=url,data=data) print(res.text)
结果:
<?xml version='1.0' encoding='UTF-8'?> <soap11env:Envelope xmlns:soap11env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tns="UserService"><soap11env:Body><tns:addUserResponse><tns:addUserResult>用户已存在</tns:addUserResult></tns:addUserResponse></soap11env:Body></soap11env:Envelope>
XML: 可扩展标记语言,使用
<bookstore> <book category="COOKING"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category="CHILDREN"> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> <book category="WEB"> <title lang="en">Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book> </bookstore>
Python解析XML
from xml.etree import ElementTree d = '''<bookstore> <book category="COOKING"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category="CHILDREN"> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book> <book category="WEB"> <title lang="en">Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book> </bookstore> ''' root = ElementTree.fromstring(d) # 加载元素树(ElementTree)获得根节点 print(root.find(".")) # 选择当前节点 print(root.find("book")) # 选择标签为book的子节点 print(root.find("book[2]")) # 选择标签为book的第三个子节点 print(root.find("book[@category='COOKING']")) # 选择标签为book切标签属性为category="COOKING" print(root.find("./book/[title='Harry Potter']")) # 选择标签为book的节点中包含子标签title 切title的文本内容为Harry Potter
结果:
<Element 'bookstore' at 0x000002406B666688> <Element 'book' at 0x000002406B6666D8> <Element 'book' at 0x000002406B8600E8> <Element 'book' at 0x000002406B6666D8> <Element 'book' at 0x000002406B8600E8>
find()返回的是节点对象,能够经过.tag获取标签名,.attrib获取属性字典,.text获取文本
XPath选择器
经常使用的三种定位元素方法
Mock 即模拟,就是在测试过程当中,对于某些不容易构造或者不容易获取的对象,用一个虚拟的对象来建立以便测试的测试方法,其最大的优点就是降级先后端耦合度,使前端工程师能够不依赖后端返回数据,先开发前端样式以及逻辑处理
简单来讲: Mock是用了解决依赖问题的,将复杂的/不稳定的/还未创建的依赖对象用一个简单的假对象来代替
Mock Server 即Mock接口服务器,能够经过配置快速Mock出新的接口
Mock Server的使用范围
同时在接口还未开发好时,提供Mock接口(假接口)会比只有接口文档更直观,并能有效减小沟通成本和一些文档理解bug
Postman的Mock Server功能
Postman还能够基于Collection创建Mock Server,这里再也不详述
Python+Flask本身搭建Mock接口
使用Flask包咱们能够快速搭建Mock接口
pip install flask
from flask import Flask, request, jsonify, abort import random app = Flask(__name__) # 实例化一个Flask对象 @app.route("/api/user/reg/", methods=["POST"]) def reg(): if not request.json or not 'name' in request.json or not 'password' in request.json: abort(404) res = [ { "code": "100000", "msg": "成功", "data": { "name": "李六", "password": "e10adc3949ba59abbe56e057f20f883e" } }, { "code": "100001", "msg": "失败,用户已存在", "data": { "name": "李六", "password": "e10adc3949ba59abbe56e057f20f883e" } }, { "code": "100002", "msg": "失败,添加用户失败", "data": { "name": "李六", "password": "e10adc3949ba59abbe56e057f20f883e" } } ] return jsonify(random.choice(res)) if __name__ == '__main__': app.run()
此为北京龙腾育才 Python高级自动化(接口测试部分)授课笔记
课程介绍
想要参加现场(北京)/网络课程的能够联系做者微信:lockingfree
- 高效学习,快速掌握Python自动化全部领域技能
- 同步快速解决各类问题
- 配套实战项目练习