Python测试WebService接口的实现示例
作者:互联网杂货铺
webService接口是走soap协议通过http传输,请求报文和返回报文都是xml格式的,本文主要介绍了Python测试WebService接口,具有一定的参考价值,感兴趣的可以了解一下
WebService是什么?
简单的说WebService是一个SOAP(面向服务的编程)的架构,它是不依赖于语言,不依赖于平台,可以实现不同的语言(通过 xml 描述)间的相互调用,通过Internet进行基于Http协议的网络应用间的交互。通过SOAP在Web上提供的软件服务,使用WSDL文件进行说明,并通过UDDI进行注册。(概念性的东西大家可以自行搜索补充)
测试环境准备
python2.7 + httplib 内置库
数据准备
这里就定义了两个case:
- case1是一个正向case, 根据正确的nameid查询用户信息。
- case2是一个反向case, 给出一个错误的nameid 查询用户信息。
然后将这两个case 存放到一个dict 中,最后引入代码里面进行请求使用。
data.py文件内容如下:
#正向的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文件内容如下:
#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()结果输出:
case2 验证通过
case1 验证通过
总结
通过以上简单的几步就可以完成WebService Api的测试,对于示例中的测试数据大家可以根据Api文档的描述不断的丰富测试场景。
到此这篇关于Python测试WebService接口的文章就介绍到这了,更多相关PythonWebService接口测试内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
