参考文档:http://blog.csdn.net/xiaoxinyu316/article/details/53170463 python利用unittest进行测试用例执行的几种方式
总策略:python利用unittest 通过testsuite来执行测试用例
总结构:
第一个部分: mspLibrary.py
主要功能是:发送request请求,获取返回的状态码
这次写的是MSP门诊部门的接口。门诊的接口data数据主要有2个部分组成:主表数据和明细表数据。data部分的数据,根据实际业务写函数来组成满足条件的data数据格式。
#coding=utf-8 import requests import json #封装数据 def msp_getData(key, value, isEnd = False): if isEnd: #最后一个不需要加 , data = '"' + key + '"' + ':' + '"' + value + '"' else: data = '"' + key + '"' + ':' + '"' + value + '"' + ',' return data #发送request请求 def msp_request(url,data,headers): result = [] response = requests.post(url,data,headers = headers) result.append(response.status_code) if response.status_code == 200: result.append(response.text) return result def msp_getJsonValue(jsonData, key): #key传入这个参数,利用传化成的字典 通过:字典名[key名]获取key的值 text = json.loads(jsonData) #利用json模块内的loads函数,将字符串转化成字典 return text[key] #门诊报文 - 获取主表数据 #将主表数据的每个属性设置成变量,给定一个初始值 def mzbw_getMainTable(originalId="183011270", areaCode="520222", areaName="白银区", medicineCode="5202220905040021", patientId="806953", identifyId="520222197307203610", name="liu0401", sex="1", age="25", national="1", newBornStatus="0", height="178", weight="65", bornWeight="6.2", pregStatus="0", pregTime="0", momChildBind="0", insuredType="2", clinicType="1", allergyDrugs="null", hospId="5100", hospName="白银区人民医院", hospLevel="3", doctorId="123456", doctorName="定", diagnoseAreaCode="N71.101", diagnoseArea="慢性子宫内膜炎", diagnoseStdCode="", diagnoseStd="", isSingleDisease="3", payType="1", recipeDate="2017-07-13 00:00:00", settleDate="2017-07-13 00:00:00", totalCost="50000.0000", medicalCost="2557.4000", drugCost="2714.4000", drugInsuredCost="2714.4000", serCost="27.4000", serInsuredCost="27.4000", matCost="27.4000", matInsuredCost="27.4000", balanceCost="262527.4000", startingLine="27.4000", balanceCostSum="2577257.4000", disableType="0", uploadDate="2017-07-13 12:22:48", uploadUnit="1", indate="2017-07-13 12:22:48", clinicDep="内科"): data = "" data += msp_getData("originalId", originalId) data += msp_getData("areaCode", areaCode) data += msp_getData("areaName", areaName) data += msp_getData("medicineCode", medicineCode) data += msp_getData("patientId", patientId) data += msp_getData("identifyId", identifyId) data += msp_getData("name", name) data += msp_getData("sex", sex) data += msp_getData("age", age) data += msp_getData("national", national) data += msp_getData("newBornStatus", newBornStatus) data += msp_getData("height", height) data += msp_getData("weight", weight) data += msp_getData("bornWeight", bornWeight) data += msp_getData("pregStatus", pregStatus) data += msp_getData("pregTime", pregTime) data += msp_getData("momChildBind", momChildBind) data += msp_getData("insuredType", insuredType) data += msp_getData("clinicType", clinicType) data += msp_getData("allergyDrugs", allergyDrugs) data += msp_getData("hospId", hospId) data += msp_getData("hospName", hospName) data += msp_getData("hospLevel", hospLevel) data += msp_getData("doctorId", doctorId) data += msp_getData("doctorName", doctorName) data += msp_getData("diagnoseAreaCode", diagnoseAreaCode) data += msp_getData("diagnoseArea", diagnoseArea) data += msp_getData("diagnoseStdCode", diagnoseStdCode) data += msp_getData("diagnoseStd", diagnoseStd) data += msp_getData("isSingleDisease", isSingleDisease) data += msp_getData("payType", payType) data += msp_getData("recipeDate", recipeDate) data += msp_getData("settleDate", settleDate) data += msp_getData("totalCost", totalCost) data += msp_getData("medicalCost", medicalCost) data += msp_getData("drugCost", drugCost) data += msp_getData("drugInsuredCost", drugInsuredCost) data += msp_getData("serCost", serCost) data += msp_getData("serInsuredCost", serInsuredCost) data += msp_getData("matCost", matCost) data += msp_getData("matInsuredCost", matInsuredCost) data += msp_getData("balanceCost", balanceCost) data += msp_getData("startingLine", startingLine) data += msp_getData("balanceCostSum", balanceCostSum) data += msp_getData("disableType", disableType) data += msp_getData("uploadDate", uploadDate) data += msp_getData("uploadUnit", uploadUnit) data += msp_getData("indate", indate) data += msp_getData("clinicDep", clinicDep) return data #门诊报文 - 获取明细表 #将每个明细表的属性设置为变量,给定一个初始值 def msbw_getDetailTable(originalId="183022370", clinicInfoId="", areaCode="520222", hospItemCode= "Z0101050030a4", hospItemName="蜜炼川贝枇杷膏", areaItemCode="Z0101050030a4", areaItemName="蜜炼川贝枇杷膏", coreItemCode="", coreItemName="", goodsName="蜜炼川贝枇杷膏", dosage="1", spec="1", countNum="1.5", countUnit="克", packNum="2", packUnit="克", saleUnit="克", unit="30.0000", count="2", cost="50.0000", drugRoute="1", drugDoseSingle="1", drugRate="1", drugTime="饭前", drugGoal="治疗", groupNum="001", type="0", drugOrdersStart="2017-07-13", drugOrdersEnd="2117-01-01", exeDate="2017-07-13", docterId="123456", doctorName="meng", doctorTitle="主治医师", recipeDate="2017-07-13 00:00:00", indate="2017-07-13 00:00:00", drugType="1", rate="0.2", drugCatalog="0"): data = '' data += msp_getData("originalId", originalId) data += msp_getData("clinicInfoId", clinicInfoId) data += msp_getData("areaCode", areaCode) data += msp_getData("hospItemCode", hospItemCode) data += msp_getData("hospItemName", hospItemName) data += msp_getData("areaItemCode", areaItemCode) data += msp_getData("areaItemName", areaItemName) data += msp_getData("coreItemCode", coreItemCode) data += msp_getData("coreItemName", coreItemName) data += msp_getData("goodsName", goodsName) data += msp_getData("dosage", dosage) data += msp_getData("spec", spec) data += msp_getData("countNum", countNum) data += msp_getData("countUnit", countUnit) data += msp_getData("packNum", packNum) data += msp_getData("packUnit", packUnit) data += msp_getData("saleUnit", saleUnit) data += msp_getData("unit", unit) data += msp_getData("count", count) data += msp_getData("cost", cost) data += msp_getData("drugRoute", drugRoute) data += msp_getData("drugDoseSingle", drugDoseSingle) data += msp_getData("drugRate", drugRate) data += msp_getData("drugTime", drugTime) data += msp_getData("drugGoal", drugGoal) data += msp_getData("groupNum", groupNum) data += msp_getData("type", type) data += msp_getData("drugOrdersStart", drugOrdersStart) data += msp_getData("drugOrdersEnd", drugOrdersEnd) data += msp_getData("exeDate", exeDate) data += msp_getData("docterId", docterId) data += msp_getData("doctorName", doctorName) data += msp_getData("doctorTitle", doctorTitle) data += msp_getData("recipeDate", recipeDate) data += msp_getData("indate", indate) data += msp_getData("drugType", drugType) data += msp_getData("rate", rate) data += msp_getData("drugCatalog", drugCatalog, True) return data #将主表数据和明细表数据进行相加,整合,形成最终的data业务数据格式
def msbw_getAllData(mainTable, detailTable): data = 'data={ "outpData": { ' data += mainTable data += '"cureDetailList": [{ ' data += detailTable data += ' }], },"finishType":"0" }' data += '&platformCode=mykjyxgs×tamp=20161118111111&veritify=DV%2B/IYNDELLstC2awpOoJtaB7WReqRk5cU3YFhqCv34=' return data #本模块执行,会执行if判断下的操作,用于本模块的测试函数工作
if __name__ == "__main__": mainTable = mzbw_getMainTable() detailTable = msbw_getDetailTable() data = msbw_getAllData(mainTable, detailTable) print data #mz_request(data1, data2)
第二个部分:mspxx.py
主要作用是:将发送request请求中的url,headers数据写入一个类,方便需要的地方进行调用
#coding=utf-8 import unittest class MSP(unittest.TestCase): #继承,继承是继承类类型
url = 'http://10.12.3.26/auditPort/port/uploadOutpData/1.do' headers = {'Accept-Encoding': 'gzip,deflate', 'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8', 'User-Agent': 'Jakarta Commons-HttpClient/3.1', 'Host': '10.12.3.26'}
第三个部分:testcase_originalId.py
创建测试用例,此部分用于填写测试数据,获取request请求的返回内容,通过设置断点对返回的内容进行判断。
#coding=utf-8 import unittest import sys import mspxx
sys.path.append(r"../../lib") #执行本模块,使用的路径
sys.path.append(r"../lib") #模块被调用时,使用的路径
import mspLibrary class Test_originalId(mspxx.MSP): #继承一个类 导入这个包,包.类名 def testcase1(self): mainTable = mspLibrary.mzbw_getMainTable(originalId='183011303')
detailTable = mspLibrary.msbw_getDetailTable(originalId='183022403') data = mspLibrary.msbw_getAllData(mainTable, detailTable) ret = mspLibrary.msp_request(self.url, data, self.headers) #获取继承的类的值,用 self. 的格式
self.assertEqual(ret[0],200) #设置断点,判断执行返回的状态码值是否等于200 rst = mspLibrary.msp_getJsonValue(ret[1], 'rst') self.assertEqual(rst, '0') #设置断点,判断返回ret的值是否等于0 print ret if __name__ == '__main__': unittest.main()
第四个部分:testsuite_originalId.py
创建测试集,先获取一个对象,用对象对调相应的函数,将测试用例进行添加,形成测试集合
#coding=utf-8 import sys sys.path.append(r'../testcase') #执行本模块使用的路径 sys.path.append(r'testcase') #当模块被调用时使用的路径 import testcase_originalId import unittest def suite1(): #获取一个suite对象 suite = unittest.TestSuite() #添加测试用例 suite.addTest(testcase_originalId.Test_originalId('testcase1')) return suite
第五个部分:run.py
执行测试集
#coding=utf-8 from testsuite import testsuite_originalId import unittest def run(): #获取一个runner的对象 runner = unittest.TextTestRunner() #获取一个suite suite = testsuite_originalId.suite1() #执行 runner.run(suite) run()