测试数据创建后需要对其删除,不然可能产生脏数据,对开发和测试、生产环境造成一定影响。
其接口框架是基于Python,API规范基于REST。
产生原因
改进前:清除资源的操作放在每个正向测试用例里,没有在setUp和tearDown中删除。
改进后:在setUp中初始化资源id=None,每次创建资源时会生产一个资源id,然后在tearDown中,判断资源的id是否为None,if不为None,then delete 资源并将资源id重新赋值为None。
这样不管有没有执行成功,最终都会在tearDown中删除被创建成功的资源,初始化工作放在setUp中。
代码对比
改进前:清除资源的操作放在每个正向测试用例里,没有在setUp和tearDown中删除。代码如下:
python
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)#放在测试用例中删除
改进后:
setUp和tearDown中改进:
python
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):
python
#判断资源的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 '------------------------------------------------------'
测试用例中将其删除代码注释:
python
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)
总结
发现问题要多思考,并努力把其解决。
最后感谢每一个认真阅读我文章的人,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走:
这些资料,对于【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴上万个测试工程师们走过最艰难的路程,希望也能帮助到你!