接口测试框架
测试框架基本能力
项目管理:pip,virtualenv
用例编写:pytest
领域能力:App, web,http
执行调度:pytest,pycharm,shell,jenkins
测试报告:allure
Http测试能力
请求方法构造:post、get、put、delete、head...
请求体构造:form、json、xml、binary
响应结果分析:status code、response body、json path、xpath
requests框架
功能全面:http/https支持全面
使用简单:简单易用,不用关心底层细节
定制性高:借助于hook机制完成通用处理
1. 常见http请求方法构造
import requests
r=requests.put('https://httpbin.org/put',data={'key':'value'}) r=requests.delete('https://httpbin.org/delete') r=requests.head('https://httpbin.org/get') r=requests.options('https://httpbin.org/get')
2. 请求参数构造
get query:path、query
payload={'key1':'value1','key2':'value2'}
r=requests.get('https://httpbin.org/get',params=payload)
post body:
form:
结构化请求:json、xml、json rpc
binary
header构造
普通的header:
headers={'user-agent':'my-app/1.0'}
r=requests.get(url,headers=headers)
cookie
cookies = dict(cookies_are='working')
r=requests.get(url,cookies=cookies)
3. 响应结果
基本信息: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 XML
1. json请求
def test_post_json(self): payload = { 'level': 1, 'name': 'jiage' } r = requests.post('http://httpbin.testing-studio.com/post', json=payload) print(r.text) assert r.status_code == 200
响应结果
"args": {}, "data": "{\"level\": 1, \"name\": \"jiage\"}", #把payload转化成了json字符串发送出去 "files": {}, "form": {}, #已json格式请求,form为空 "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Connection": "close", "Content-Length": "29", "Content-Type": "application/json", #将content-type改成了json "Host": "10.0.6.2", "User-Agent": "python-requests/2.24.0" }, "json": { #服务器收到请求后转义出来的,展现出更结构化的数据 "level": 1, "name": "jiage" }, "origin": "10.0.6.1", "url": "http://10.0.6.2/post" }
2. xml请求
requests并没有对xml进行封装,这块我们更多的使用data来进行发送,所以需要配置header
def test_xml(self): xml='''<?xml version='1.0' encoding='utf-8'> <a>test xml</a>''' headers={'Content-Type':'application/xml'} r =requests.post('http://httpbin.testing-studio.com/post',data=xml,headers=headers) print(r.text)
响应结果
"args": {}, "data": "<?xml version='1.0' encoding='utf-8'>\n <a>test xml</a>", "files": {}, "form": {}, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Connection": "close", "Content-Length": "65", "Content-Type": "application/xml", "Host": "10.0.6.2", "User-Agent": "python-requests/2.24.0" }, "json": null, "origin": "10.0.6.1", "url": "http://10.0.6.2/post" }
复杂数据解析
数据保存:将复杂的xml或者json请求体保存到文件模板中
数据处理:
使用mustache、freemarker等工具解析
使用简单的字符串替换
使用json xml api进行结构化解析
数据生成:输出最终结果

结构化响应断言JSON XML
1. json断言
def test_post_json(self): payload = { 'level': 1, 'name': 'jiage' } r = requests.post('http://httpbin.testing-studio.com/post', json=payload) print(r.text) assert r.status_code == 200 assert r.json()['json']['level']==1 #request提供的json断言
json path

使用jsonpath需要导入python的第三方库jsonpath
from jsonpath import jsonpath import requests def test_jsonpath(self): r = requests.get('https://ceshiren.com/categories.json') print(r.text) assert r.status_code == 200 print(jsonpath(r.json(),'$..name')) assert jsonpath(r.json(),'$..name')[0]=="社区治理"
2. xml断言
from requests_xml import XMLSession session = XMLSession() r = session.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"]')
3. hamcrest断言
框架自带assert 体系:assert, assertEqual
Hamcrest体系:assert_that
from hamcrest import * class TestApp: def test_hamcrest(self): # assert_that(10,equal_to(9),'这是一个提示') assert_that(8,close_to(10,2)) assert_that("contains some string",contains_string("string"))
详细可以参考:https://github.com/hamcrest/PyHamcrest
4. schema断言
schema校验:https://jsonschema.net/
生成schema文件
根据需要添加自定义规则
这篇解释地很直白:https://cloud.tencent.com/developer/article/1005810
schema自动校验
每次运行的时候自动保存当前的schema,下次运行对比上次的schema如果发现变更就报错,saveSchema+diffSchema
header cookie处理
cookie使用场景
在接口测试过程中,很多情况下,需要发送的请求附带cookies,才会得到正常的响应结果。所以使用python+requests进行接口自动化测试也是同理,需要在 构造接口测试用例时加入cookie
传递cookie的两种方式
通过请求头信息传递
import requests url = "https://httpbin.org/cookies" headers = {"Cookie":"working=1","User-agent":"python-requests"} r = requests.get(url, headers=headers) print(r.requests.headers)
通过请求的关键字参数cookies传递
import requests url = "https://httpbin.org/cookies" headers = {"User-agent":"python-requests"} cookies=dict(cookies_are='working2') r = requests.get(url, headers=headers, cookies=cookies) print(r.requests.headers)
认证体系
def test_auth(self): from requests.auth import HTTPBasicAuth url = "http://httpbin.testing-studio.com/basic-auth/tester1/123456" r = requests.get(url,auth=HTTPBasicAuth("tester1","123456")) print(r.text)

浙公网安备 33010602011771号