God新增功能:
1.用例测试结果入库
2.短链接实时监控
3.通过域名访问,查看测试情况
一、测试结果入库:
之前采用的HtmlTestRunner.py直接生成测试报告,根据生成的测试报告查看测试情况:
顺便总结一下优缺点:
优点:
· 方便,只需要引用然后执行以下就能完成unittest对于用例结果的处理;
· 简单,不需要考虑任何关于生成测试报告的问题;
· 快速,对于快速搭建一套完成的测试系统,这个强大的库无疑省去了将近50%的力气;
缺点:
· 封闭,封闭性比较强,并没有开发很多可用的接口(至少我一个都不知道);
· 捕获异常不够准确,用的时间久了,会发现报告中错误的用例捕获的异常信息不准确;
· 自定义,不能满足日益增加的业务需求
为了满足对于测试结果输出准确性,故选择将测试结果自行处理,根据业务的具体情况,个性化的定义用例通过的准则;
· Mysql具体实现:
CREATE TABLE test_result ( `id` int(11) NOT NULL AUTO_INCREMENT, `if_name` varchar(10) Not Null comment '接口名称', `case_name` varchar(20) comment '用例名称', `status` varchar(5), `result` varchar(20), `response` varchar(1000), `url_target` varchar(20) comment '执行域名', `comment` varchar(30), `type` varchar(10) comment '执行接口类型', `uptm` timestamp not null, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=utf8; |
! 暂时这些字段,后期如果业务需要,可以随时添加
入库
用例中具体实现:
from Src.Function.ApiMethod import Api_urllib from Src.Function.LogMainClass import * from Src.Function.MySql import MySQL class CheckPay(Api_urllib): def setUp(self): self.comment = '测试提现记录' self.casename = "test_CheckPay" self.interfacename = 'd13' self.result = 'Fail' self.response = "Null" self.status = '0' def test_CheckPay(self): casename = self.casename response = '' status_code = '' transnumber = Api_urllib.read_from_file('d08ford12d13').split(',')[0] try: # 正常接口请求 cookie = Api_urllib.read_cookie_file('100') parameter = {'r': '123', 'transnumber': transnumber} response, status_code = Api_urllib.getInterface_requests_status(parameter, headers=cookie) # 自定义当用例执行时,校验到哪些信息属于Pass self.result = "Pass" except Exception as msg: print (msg) raise finally: # 将本条测试用例 入库 ,其中:set_test_result 为自己封装类 MySQL.set_test_result(response, status_code, self.interfacename, self.casename, self.comment, self.result) def tearDown(self): do something |
说明:
1.在setup套件中初始化变量,因为用例中首先执行setup;
2.test_checkpay为用例主体,其中通过封装方法: Api_urllib.getInterface_requests_status()获取接口的response和status_code;
3.根据测试情况,定义该条用例认定为通过的条件
4.通过封装类方法:MySQL.set_test_result()将用例中必要用例进行入库操作,在此方法中我对response进行了2次处理,从而增加入库信息的丰富性
5.其中set_test_result()方法主要对json格式的respose进行二次处理,以及对一些异常返回情况进行处理
def set_test_result(cls, response, status_code, interfacename, casename, comment, result): status = '' try: status = response['succ'] cls.response_new = 'succ:%s' % response['succ'] if response['succ'] != '1': cls.response_new = response['msg'] except: status = status_code #` todo_string() `封装的处理字符串方法 cls.response_new = MySQL.todo_string(response) finally: MySQL.insert(interfacename, casename, comment, status, result, cls.response_new) |