

演練環(huán)境:http://httpbin.testing-studio.com/
接口測(cè)試框架
requests庫(kù):https://requests.readthedocs.io/en/master/
接口請(qǐng)求構(gòu)造
請(qǐng)求目標(biāo)
? ? import requests
? ? r = requests.get('https://www.baidu.com')
請(qǐng)求參數(shù)構(gòu)造
get query:? ? path? ? query
post body:
? ? form
? ? 結(jié)構(gòu)化請(qǐng)求:json? ? xml? ? json rpc
? ? binary
Get?Query請(qǐng)求
payload = {'key1':'value1','key2':'value2'}
r = requests.get('https://httpbin.org/get',params=payload)

Form請(qǐng)求參數(shù)構(gòu)造
payload = {'key1':'value1','key2':'value2'}
r = requests.post('https://httpbin.org/post',data=payload)

文件上傳
files = {'file':open('report.xls','rb')}
r = requests.post(url,files=files)
header構(gòu)造
普通的header
? ? headers={'user-agent':'my-app/0.0.1'}
? ? r = requests.get(url,headers=headers)
cookie
? ? cookies = dict(cookies_are='working')
? ? r = requests.get(url,cookies=cookies)
接口測(cè)試斷言
響應(yīng)結(jié)果
基本信息:r.url? ? ?r.status_code? ? r.headers? ? r.cookies
響應(yīng)結(jié)果
? ? r.text = r.encoding+r.content
? ? r.json()=r.encoding+r.content+content type json
? ? r.raw.read(10)
對(duì)應(yīng)的請(qǐng)求內(nèi)容
? ? r.request
json/xml請(qǐng)求
json請(qǐng)求體構(gòu)造
? ? payload={'some':'data'}
? ? r = requests.post(url,json=payload)
xml請(qǐng)求體構(gòu)造
? ? import requests
? ? xml = """<?xml version='1.0' encoding='utf-8'?> <a>test</a>"""
? ? header = {'Content-Type':'application/xml'}
? ? r = requests.post(url,data=xml,headers=headers).text
復(fù)雜數(shù)據(jù)解析
? ? 數(shù)據(jù)保存:
? ? ? ? 將復(fù)雜的xml或json請(qǐng)求體保存到文件模板中
? ? 數(shù)據(jù)處理:
? ? ? ? 使用mustache、freemaker等工具解析
? ? ? ? 簡(jiǎn)單的字符串替換
? ? ? ? 使用json?xml?api進(jìn)行結(jié)構(gòu)化解析
? ? 數(shù)據(jù)生成:輸出最終結(jié)果
json斷言
? ? def?test_demo(self):
? ? ? ? ? ?r = requests.get(url)
? ? ? ? ? ? assert r.json()['key1']['key2'][0]['name']=='test'
jsonpath定位
導(dǎo)入jsonpath庫(kù)

????assert r.json()['key1']['key2'][0]['name']=='test'
????assert jsonpath(r.json(),'$..name')[0]=='test'
xpath斷言
? ? ? ? from requests_xml import XMLSession
? ? ? ? session = XMLSession()
? ? ? ? r = session.get(url)
? ? ? ? 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")
hamcrest斷言
? ? 框架自帶assert體系:assert、assertEqual
? ? Hamcrest體系:assertThat
導(dǎo)入hamcrest庫(kù)

schema斷言
jsonschema庫(kù)
schema自動(dòng)校驗(yàn)

header cookie處理

通過(guò)請(qǐng)求頭信息傳遞和通過(guò)cookies參數(shù)傳遞



認(rèn)證體系
基本認(rèn)證,允許http用戶(hù)代理在請(qǐng)求時(shí),提供用戶(hù)名和密碼的一種方式
