接口测试框架基本能力
项目管理 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
复杂数据解析
模板技术mustache
结构化响应断言 JSON XML
def test_hog(self):
url="https://home.testing-studio.com/categories.json"
res = requests.get(url)
assert res.json()['']
json path
XPATH | JSONPath | Result |
---|
/store/book/author | $.store.book[*].author | the author of all books in the st | //author | $…author | all authors | /store/* | $.store.* | all things in store, which are some books and a red bicycle. | /store//price | $.store…price | the 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
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
HTTP Basic
|