測試用例基類:html
# coding=utf-8 import unittest import Logger log = Logger.Loger() class BaseCase(unittest.TestCase): def setUp(self): self.loginfo("============================================================") self.loginfo("%s is start" % self ) #print self._attribute.decode('utf=8') def tearDown(self): self.loginfo('test is stop') self.loginfo("============================================================\n") def loginfo(self, msgStr): log.info(msgStr) if __name__ == "__main__": unittest.main()
接口用例InterfaceTestCase.pyweb
#coding=utf-8 import unittest import requests from interfacetest.iutil import DataEngine from interfacetest.iutil import HttpEngine from IFBaseCase import BaseCase import datetime import Utils import ConfigParser class DoInterfaceTest(BaseCase): def runTest(self): sttime = datetime.datetime.now() domain,caselist,header = DataEngine.getCase() #讀取Excel獲取用例數據 self.loginfo("domain:"+domain) self.loginfo("header:"+str(header)+"\n") resultlist = [] for case in caselist: ''' case[0] 序號 case[1] 優先級 case[2] 模塊 case[3] 用例描述 case[4] 是否登陸 case[5] 接口url case[6] 方法 case[7] 參數 case[8] 指望結果 ''' self.loginfo("url:"+case[5]) self.loginfo("method:"+case[6]) starttime = datetime.datetime.now() url = domain+case[5] #拼接請求url method = case[6] #請求方法:post、get、delete data = eval(case[7]) #請求數據 sess=object isexcept = False if case[4] == "Y": #是否須要登陸 sess,isexcept = HttpEngine.login() #返回登陸的session和登陸是否異常 else: sess = requests.Session() result = "Failed" actualre = "" if isexcept: #若是登陸異常,直接中斷 actualre = sess else: #沒有登陸異常,繼續執行 re,isexcept = HttpEngine.getData(sess, url, data, header, method) #發送請求並獲取響應結果 if isexcept: #若是請求異常,直接中斷 actualre = re else: result,actualre = DataEngine.resultCheck(re, case[8]) #結果匹配 if "password" in case[7]: temp = eval(case[7]) temp['password'] = "******" case[7] = temp self.loginfo("data:"+str(case[7])) self.loginfo("結果:"+result+", 請求返回:"+actualre+"\n") stoptime = datetime.datetime.now() takentime = ((stoptime-starttime).microseconds)/1000 #獲取時間差,單位爲毫秒 if len(actualre) > 43: actualre = actualre[:43]+" ..." actualre = actualre.replace('<','<').replace('>','>') result_temp = [case[2], case[3], case[5], case[7], actualre, result, takentime] resultlist.append(result_temp) sptime = datetime.datetime.now() passcount,failcount = DataEngine.countResult(resultlist) #統計結果 totalcount = len(resultlist) tktime = ((sptime-sttime).microseconds)/1000/1000.000 #總體耗時,單位爲秒 sttime = sttime.strftime("%Y-%m-%d %H:%M:%S") tinfo = {'starttime':sttime, 'takentime':tktime, 'pass':passcount, 'fail':failcount, 'total':totalcount} conf = ConfigParser.ConfigParser() conf.read("config.conf") reportfile = conf.get("report", "report_path") logfile = conf.get("report", "log_path") Utils.createInterfaceReport(resultlist, tinfo, reportfile, logfile) if __name__ == "__main__": unittest.main()