巧用PyUnit中unittest特性解决接口测试产生脏数据问题
一、背景
测试数据创建后需要对其删除,不然可能产生脏数据,对开发和测试、生产环境造成一定影响。其接口框架是基于Python,API规范基于REST。
二、产生原因
(1)改进前:清除资源的操作放在每个正向测试用例里,没有在setUp和tearDown中删除。
(2)改进后:在setUp中初始化资源id=None,每次创建资源时会生产一个资源id,然后在tearDown中,判断资源的id是否为None,if不为None,then delete 资源并将资源id重新赋值为None。这样不管有没有执行成功,最终都会在tearDown中删除被创建成功的资源,初始化工作放在setUp中。
三、代码对比
(1)改进前:清除资源的操作放在每个正向测试用例里,没有在setUp和tearDown中删除。代码如下:
def test_konwledges_check_by_list_ok(self):
"""
lc知识点API:成功创建2个知识点后,批量查询知识点详细信息\
"""
title = 'knowledges'
flag = 0
response = self.knowledge.add_knowledges(title, flag)
message = "创建知识点接口返回失败"
code = 200
data = self.rest_o.parse_response(response, code, message)
self.knowledges_id1 = data['identifier']
response = self.knowledge.add_knowledges(title, flag)
message = "创建知识点接口返回失败"
code = 200
data = 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 = 200
data = self.rest_o.parse_response(response, code, message)
print data
assert data[self.knowledges_id1]
assert data[self.knowledges_id2]
self.knowledge.delete_konwledges(knowledge_id1)#放在测试用例中删除
self.knowledge.delete_konwledges(knowledge_id2)#放在测试用例中删除
a.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 = None
self.knowledges_id1 = None
self.knowledges_id2 = None
self.knowledge_relation_id = None
def tearDown(self):
#判断资源的id是否为None,if不为None,then delete 资源并将资源id重新赋值为None,这样不管有没有执行成功,最终都会在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 = 0
response = self.knowledge.add_knowledges(title, flag)
message = "创建知识点接口返回失败"
code = 200
data = self.rest_o.parse_response(response, code, message)
self.knowledges_id1 = data['identifier']
response = self.knowledge.add_knowledges(title, flag)
message = "创建知识点接口返回失败"
code = 200
data = 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 = 200
data = self.rest_o.parse_response(response, code, message)
print data
assert data[self.knowledges_id1]
assert data[self.knowledges_id2]
#注释原在测试用例中删除的代码
#self.knowledge.delete_konwledges(knowledge_id1)
#self.knowledge.delete_konwledges(knowledge_id2)
发现问题要多思考,并努力把其解决。
版权声明:本文为博主原创文章,未经博主允许不得转载。