Python測試WebService接口的實(shí)現(xiàn)示例
WebService是什么?
簡單的說WebService是一個(gè)SOAP(面向服務(wù)的編程)的架構(gòu),它是不依賴于語言,不依賴于平臺(tái),可以實(shí)現(xiàn)不同的語言(通過 xml 描述)間的相互調(diào)用,通過Internet進(jìn)行基于Http協(xié)議的網(wǎng)絡(luò)應(yīng)用間的交互。通過SOAP在Web上提供的軟件服務(wù),使用WSDL文件進(jìn)行說明,并通過UDDI進(jìn)行注冊。(概念性的東西大家可以自行搜索補(bǔ)充)
測試環(huán)境準(zhǔn)備
python2.7 + httplib 內(nèi)置庫
數(shù)據(jù)準(zhǔn)備
這里就定義了兩個(gè)case:
- case1是一個(gè)正向case, 根據(jù)正確的nameid查詢用戶信息。
- case2是一個(gè)反向case, 給出一個(gè)錯(cuò)誤的nameid 查詢用戶信息。
然后將這兩個(gè)case 存放到一個(gè)dict 中,最后引入代碼里面進(jìn)行請求使用。
data.py文件內(nèi)容如下:
#正向的case
case1='''<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Header>
<OGHeader xmlns="http://webservices.micros.com/og/4.3/Core/" transactionID="2019062118114433750450">
<Origin entityID="OW1" systemType="WEB"/>
<Destination entityID="TI" systemType="ORS"/>
</OGHeader>
</soap:Header>
<soap:Body>
<FetchProfileRequest xmlns="http://webservices.micros.com/oqq/5.1/Name.wsdl">
<NameID type="INTERNAL">186217986</NameID>
</FetchProfileRequest>
</soap:Body>
</soap:Envelope>'''
#反向的case
case2='''<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Header>
<OGHeader xmlns="http://webservices.micros.com/og/4.3/Core/" transactionID="2019062118114433750450">
<Origin entityID="OW1" systemType="WEB"/>
<Destination entityID="TI" systemType="ORS"/>
</OGHeader>
</soap:Header>
<soap:Body>
<FetchProfileRequest xmlns="http://webservices.micros.com/oqq/5.1/Name.wsdl">
<NameID type="INTERNAL">1862179863</NameID>
</FetchProfileRequest>
</soap:Body>
</soap:Envelope>'''
dict1={"success":case1,"fail":case2}
test.py文件內(nèi)容如下:
#coding=utf-8
import httplib
from data import dict1
def Featchinfo():
url="test.beat.com"
port=9700
path="/oqq_ws_51/Name.asmx"
header={'Content-Type' : 'text/xml; charset=utf-8'}
conn = httplib.HTTPConnection(url,port,timeout=10)
for key,value in dict1.items():
conn.request('POST',path,value.encode('utf-8'),header)
response=conn.getresponse()
resp=response.read()
if(key=="success" and "resultStatusFlag=\"SUCCESS" in str(resp)):
print("case1 驗(yàn)證通過")
elif(key=="fail" and "resultStatusFlag=\"FAIL" in str(resp)):
# print(resp)
print("case2 驗(yàn)證通過")
if __name__ == '__main__':
Featchinfo()結(jié)果輸出:
case2 驗(yàn)證通過
case1 驗(yàn)證通過
總結(jié)
通過以上簡單的幾步就可以完成WebService Api的測試,對于示例中的測試數(shù)據(jù)大家可以根據(jù)Api文檔的描述不斷的豐富測試場景。
到此這篇關(guān)于Python測試WebService接口的文章就介紹到這了,更多相關(guān)PythonWebService接口測試內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python 自動(dòng)批量打開網(wǎng)頁的示例
今天小編就為大家分享一篇python 自動(dòng)批量打開網(wǎng)頁的示例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-02-02
Python之a(chǎn)scii轉(zhuǎn)中文的實(shí)現(xiàn)
這篇文章主要介紹了Python之a(chǎn)scii轉(zhuǎn)中文的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-05-05
使用Python 統(tǒng)計(jì)高頻字?jǐn)?shù)的方法
今天小編就為大家分享一篇使用Python 統(tǒng)計(jì)高頻字?jǐn)?shù)的方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01
linux系統(tǒng)使用python監(jiān)測系統(tǒng)負(fù)載腳本分享
這篇文章主要介紹了linux系統(tǒng)使用python監(jiān)測系統(tǒng)負(fù)載腳本,大家參考使用吧2014-01-01

