测试数据创建后需要对其删除,不然可能产生脏数据,对开发和测试、生产环境造成一定影响。
其接口框架是基于Python,API规范基于REST。
产生原因
改进前:清除资源的操作放在每个正向测试用例里,没有在setUp和tearDown中删除。
改进后:在setUp中初始化资源id=None,每次创建资源时会生产一个资源id,然后在tearDown中,判断资源的id是否为None,if不为None,then delete 资源并将资源id重新赋值为None。
这样不管有没有执行成功,最终都会在tearDown中删除被创建成功的资源,初始化工作放在setUp中。
代码对比
改进前:清除资源的操作放在每个正向测试用例里,没有在setUp和tearDown中删除。代码如下:
def test_konwledges_check_by_list_ok(self):"""lc知识点API:成功创建2个知识点后,批量查询知识点详细信息\"""title = 'knowledges'flag = 0response = self.knowledge.add_knowledges(title, flag)message = "创建知识点接口返回失败"code = 200data = self.rest_o.parse_response(response, code, message)self.knowledges_id1 = data['identifier']response = self.knowledge.add_knowledges(title, flag)message = "创建知识点接口返回失败"code = 200data = self.rest_o.parse_response(response, code, message)self.knowledges_id2 = data['identifier']knowledge_id_str = [self.knowledges_id1, self.knowledges_id2]response = self.knowledge.check_konwledges_details_by_list(knowledge_id_str)#print 'response: ' + str(response)message = "批量查询知识点详细信息接口返回失败"code = 200data = self.rest_o.parse_response(response, code, message)print dataassert data[self.knowledges_id1]assert data[self.knowledges_id2]self.knowledge.delete_konwledges(knowledge_id1)#放在测试用例中删除self.knowledge.delete_konwledges(knowledge_id2)#放在测试用例中删除
改进后:
setUp和tearDown中改进:
def setUp(self):print 'start run KnowledgeTest(知识点接口)......'self.knowledge = Knowledge()self.lessons = Lessons()self.instructional = Instructional(id)self.teaching_material = TeachingMaterial()self.rest_o = CoRestful.Restful()self.rand_o = CoRand()self.knowledges_id = Noneself.knowledges_id1 = Noneself.knowledges_id2 = Noneself.knowledge_relation_id = Nonedef tearDown(self):
#判断资源的id是否为None,if不为None,then delete 资源并将资源id重新赋值为None<span style="font-family: Arial, Helvetica, sans-serif;">,</span>这样不管有没有执行成功,最终都会在tearDown中删除被创建成功的资源if self.knowledge_relation_id != None:self.knowledge.delete_relation(self.knowledge_relation_id)if self.knowledges_id2 != None:self.knowledge.delete_konwledges(self.knowledges_id2)if self.knowledges_id1 != None:self.knowledge.delete_konwledges(self.knowledges_id1)if self.knowledges_id != None:self.knowledge.delete_konwledges(self.knowledges_id)print '------------------------------------------------------'
测试用例中将其删除代码注释:
def test_konwledges_check_by_list_ok(self):"""lc知识点API:成功创建2个知识点后,批量查询知识点详细信息\"""title = 'knowledges'flag = 0response = self.knowledge.add_knowledges(title, flag)message = "创建知识点接口返回失败"code = 200data = self.rest_o.parse_response(response, code, message)self.knowledges_id1 = data['identifier']response = self.knowledge.add_knowledges(title, flag)message = "创建知识点接口返回失败"code = 200data = self.rest_o.parse_response(response, code, message)self.knowledges_id2 = data['identifier']knowledge_id_str = [self.knowledges_id1, self.knowledges_id2]response = self.knowledge.check_konwledges_details_by_list(knowledge_id_str)#print 'response: ' + str(response)message = "批量查询知识点详细信息接口返回失败"code = 200data = self.rest_o.parse_response(response, code, message)print dataassert data[self.knowledges_id1]assert data[self.knowledges_id2]#注释原在测试用例中删除的代码#self.knowledge.delete_konwledges(knowledge_id1)#self.knowledge.delete_konwledges(knowledge_id2)
总结
发现问题要多思考,并努力把其解决。
最后: 为了回馈铁杆粉丝们,我给大家整理了完整的软件测试视频学习教程,朋友们 如果需要可以自行免费领取 【保证100%免费】
软件测试面试文档
我们学习必然是为了找到高薪的工作,下面这些面试题是来自阿里、腾讯、字节等一线互联网大厂最新的面试资料,并且有字节大佬给出了权威的解答,刷完这一套面试资料相信大家都能找到满意的工作。