Python測試WebService接口的實現(xiàn)示例
WebService是什么?
簡單的說WebService是一個SOAP(面向服務(wù)的編程)的架構(gòu),它是不依賴于語言,不依賴于平臺,可以實現(xiàn)不同的語言(通過 xml 描述)間的相互調(diào)用,通過Internet進行基于Http協(xié)議的網(wǎng)絡(luò)應(yīng)用間的交互。通過SOAP在Web上提供的軟件服務(wù),使用WSDL文件進行說明,并通過UDDI進行注冊。(概念性的東西大家可以自行搜索補充)
測試環(huán)境準備
python2.7 + httplib 內(nèi)置庫
數(shù)據(jù)準備
這里就定義了兩個case:
- case1是一個正向case, 根據(jù)正確的nameid查詢用戶信息。
- case2是一個反向case, 給出一個錯誤的nameid 查詢用戶信息。
然后將這兩個case 存放到一個dict 中,最后引入代碼里面進行請求使用。
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 驗證通過")
elif(key=="fail" and "resultStatusFlag=\"FAIL" in str(resp)):
# print(resp)
print("case2 驗證通過")
if __name__ == '__main__':
Featchinfo()結(jié)果輸出:
case2 驗證通過
case1 驗證通過
總結(jié)
通過以上簡單的幾步就可以完成WebService Api的測試,對于示例中的測試數(shù)據(jù)大家可以根據(jù)Api文檔的描述不斷的豐富測試場景。
到此這篇關(guān)于Python測試WebService接口的文章就介紹到這了,更多相關(guān)PythonWebService接口測試內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python之a(chǎn)scii轉(zhuǎn)中文的實現(xiàn)
這篇文章主要介紹了Python之a(chǎn)scii轉(zhuǎn)中文的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-05-05
linux系統(tǒng)使用python監(jiān)測系統(tǒng)負載腳本分享
這篇文章主要介紹了linux系統(tǒng)使用python監(jiān)測系統(tǒng)負載腳本,大家參考使用吧2014-01-01

