接口地址:
http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl
1、通过python脚本,进行查询
#先导入需要的包,加载包文件
from suds.client import Client
#定义url
url = 'http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl'
#创建对象
client = Client(url)
print(client)
#进行soap接口测试
# 对象client调用services方法,再调用接口方法getMobileCodeInfo
res = client.service.getMobileCodeInfo('18561317472')
print(res)
#结果比对
print(type(res)) #打印接口的结果类型
ex_res = str(res) #把接口的结果转化为字符串类型
result = ex_res.find('山东联通GSM卡')
print(result)
if result>-1:
print("pass")
else:
print('no found')
2、将代码封装
#先导入需要的包,加载包文件
from suds.client import Client
class GMCInfo():
def __init__(self):
#初始化url
self.url = 'http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl'
def getmobilecodeinfo(self):
client = Client(self.url)
#进行soap接口测试
res = client.service.getMobileCodeInfo('18561317472') #对象client调用services方法,再调用接口方法getMobileCodeInfo
print(res)
#结果比对,把接口的结果转化为字符串类型
ex_res = str(res)
result = ex_res.find('山东联通GSM卡')
if result>-1:
print("pass")
else:
print('no found')
if __name__=='__main__':
obj = GMCInfo()
obj.getmobilecodeinfo()
3、用框架封装,进行soap进行接口测试
#先导入框架
import unittest2
from suds.client import Client
import datetime
class GMCInfo_uni(unittest2.TestCase): #使用unittest2的框架,需集成unittest2的TestCase类
def setUp(self): #TestCase类里的初始化方法
# 初始化url和tel属性
self.url = 'http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl'
self.tel = '18561317452'
def test_getmobilecodeinfo(self):
client = Client(self.url)
# 进行soap接口测试
res = client.service.getMobileCodeInfo(self.tel) # 对象client调用services方法,再调用接口方法getMobileCodeInfo
print(res)
self.ex_res = str(res) # 把接口的结果转化为字符串类型
def tearDown(self):
self.assertIn('山东联通GSM卡',self.ex_res)
if __name__=='__main__':
unittest2.main()