在python中Template可以將字符串的格式固定下來,重復(fù)利用。 同一套測(cè)試框架為了可以復(fù)用,所以我們可以將用例部分做參數(shù)化,然后運(yùn)用到各個(gè)項(xiàng)目中。
代碼如下:
coding=utf-8'''作者:大石功能:自動(dòng)生成pyunit框架下的接口測(cè)試用例環(huán)境:python2.7.6用法:將用戶給的參數(shù)處理成對(duì)應(yīng)格式,然后調(diào)用模塊類生成函數(shù),并將參數(shù)傳入即可''' from string import Template#動(dòng)態(tài)生成單個(gè)測(cè)試用例函數(shù)字符串def singleMethodCreate(MethodList,interfaceNamePara): code=Template('''/n def test_${testcase}(self): u"""${testcaseName}""" headers = $headers data = $data re = requests.$method(url='$url',headers=headers,data=data) status_code = re.status_code s = str(status_code) json = re.text logging.info('-'*5+'返回狀態(tài)碼是'+s+'-'*5) logging.info('-'*5+'返回結(jié)果集是'+json+'-'*5) assert status_code == 200 assert json['status'] == 'ok'''') string = code.substitute(testcase=MethodList["testcase"],testcaseName=MethodList["TestcaseName"], method=MethodList['method'],url=MethodList['url'],headers=MethodList['headers'],data=MethodList['data'], ) return string #拼接單個(gè)的測(cè)試用例函數(shù)字符串為完整字符串并傳回主函數(shù)#MethodParaList獲取測(cè)試用例部分listdef methodCreate(MethodParaList,interfaceNamePara): string = "" for MethodPara in MethodParaList: string2=singleMethodCreate(MethodPara,interfaceNamePara) string=string+string2 return string #構(gòu)造單個(gè)測(cè)試集def singleTestsuitCreate(MethodList,parameters): code = Template('''suite.addTest(${className}("test_${testcase}"))''') string = code.substitute(testcase = MethodList["testcase"],className = parameters[0]) return string #添加測(cè)試集def addtestsuit(MethodParaList,interfaceNamePara): string = "" for MethodPara in MethodParaList: string2 = singleTestsuitCreate(MethodPara,interfaceNamePara) string=string+string2 return string #生成測(cè)試用例類函數(shù)字符串def modelClassCreate(parameters): modelCode = methodCreate(parameters[2],parameters[1]) adtestsuit = addtestsuit(parameters[2],parameters) code = Template('''#coding: utf-8"""作者:大石功能:待執(zhí)行的接口測(cè)試用例環(huán)境:python2.7.6用法:通過框架自動(dòng)觸發(fā)調(diào)用"""import unittest,requests,datetime,sys,logging,BSTestRunner,time,osfrom Log import Logclass ${className}(unittest.TestCase): u"""待測(cè)試接口:${interfaceName}""" def setUp(self): logging.info('-'*5+"begin test"+"-"*5) def tearDown(self): logging.info('-'*5+"end test"+'-'*5) ${model}if __name__ == "__main__": #解決UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 97: ordinal not in range(128) reload(sys) sys.setdefaultencoding('utf8') #構(gòu)造測(cè)試集 suite = unittest.TestSuite() ${testsuite} #定義date為日期,time為時(shí)間 date=time.strftime("%Y%m%d") time1=time.strftime("%H%M%S") now=time.strftime("%Y-%m-%d-%H_%M_%S",time.localtime(time.time())) #創(chuàng)建路徑 path='F:/test/study/yaml/test_log/'+now+"/" #解決多次執(zhí)行時(shí)報(bào)路徑已存在的錯(cuò)誤 try: os.makedirs(path) except: if path!= None: logging.error(u'當(dāng)前路徑已經(jīng)存在') filename=path+'Report.html' fp=file(filename,'wb') #日志記錄 Log.log() #執(zhí)行測(cè)試 runner =BSTestRunner.BSTestRunner(stream=fp,title=u'下單平臺(tái)接口測(cè)試用例',description=u'接口用例列表:') runner.run(suite) fp.close()''') fileStr = code.substitute(className=parameters[0],interfaceName=parameters[1],testsuite=adtestsuit,model=modelCode) f=open(parameters[0]+".py",'w') f.write(fileStr) f.close()
新聞熱點(diǎn)
疑難解答
圖片精選