1.WriteIni.py
import ConfigParser
cf = ConfigParser.ConfigParser()
cf.add_section("PC_WSDL")
cf.set("PC_WSDL", "IP", 'localhost')
cf.add_section("VIP_WSDL")
cf.set("VIP_WSDL", "release_IP", 'localhost')
# write to file
with open("./soapTest.ini","w+") as f:
cf.write(f)
2.RunMain.py
#! /usr/bin/python
# coding:utf-8
__author__ = 'yanghaitao'
from XlsEngine import XlsEngine_wt
import DataEngine
import ConfigParser,time,Logging
import VIPSoap
import sys
reload(sys)
sys.setdefaultencoding( "utf-8" )
DATE=time.strftime(r'%Y-%m-%d-%H%M%S', time.localtime(time.time()))
cf = ConfigParser.ConfigParser()
cf.read('./soapTest.ini')
VIP_release_IP=cf.get("VIP_WSDL","release_IP") #读取VIP测试接口IP
#Excel的sheet中只保存同一接口的用例参数,不同接口保存在不同sheet中,通过修改sheet索引,获取不同接口的参数列表
Service1_dataList=DataEngine.data2List(r'.\DataSrc\dataCase.xls',1)
Service2_dataList=DataEngine.data2List(r'.\DataSrc\dataCase.xls',2)
Service3_datalist=DataEngine.data2List(r'.\DataSrc\dataCase.xls',3)
Service4_datalist=DataEngine.data2List(r'.\DataSrc\dataCase.xls',4)
def runTest(ip,list,serviceClass):
for i in range(len(list)):
try:
runtest=serviceClass(ip+str(list[i][0]))
dict=eval(str(list[i][2]))
test_rep=getattr(runtest,str(list[i][1]))(dict) #eval函数将str类型转换为dict类型,以传参给接口方法调用
DataEngine.resultCheck(test_rep,xlw,list,i)
except Exception,e:
print(str(list[i][1])+"\t"+str(e))
Logging.writeException(e)
if __name__ == '__main__':
xlw = XlsEngine_wt(r'.\DataSrc\\'+DATE+'.xls')
xlw.add_sheet('result')
runTest(VIP_release_IP,Service1_dataList,VIPSoap.Service1)
runTest(VIP_release_IP,Service2_dataList,VIPSoap.Service2)
runTest(VIP_release_IP,Service3_datalist,VIPSoap.Service3)
runTest(VIP_release_IP,Service4_datalist,VIPSoap.Service4)
xlw.save_xls()
用例参数Excel表中的第二列保存的是接口的方法名,在runTest方法中反射调用,传参,完成数据驱动接口自动化测试
结果报告如下:
执行结果为绿色代表测试通过,红色则为失败,这时候就可以去日志文件中定位具体返回结果,以解决问题