接口自动化测试框架编写之路(1):读取txt文件运行多个测试用例
作者:互联网
方法一:获取所有用例py对应的绝对路径
def get_case_path():
"""
function:获取所有需要执行的python文件的绝对路径
return: cases_paths
"""
cases_paths = []
txt_path = base_dir_path + '\\' + 'cases_list'
with open(txt_path, 'r') as f:
while True:
line = f.readline()
if not line:
break
if not line.startswith('#'):
cases_paths.append(base_dir_path + '\\' + line.replace('\n', ''))
return cases_paths
方法二:生产测试用例集合,将所有测试用例实例加入其中
def test_suit():
"""
function:将每个测试用例实例添加到all_suit测试用例集合中
return: all_suit
"""
all_suit = unittest.TestSuite()
for i in get_case_path():
file_name = i.split('\\')[-1]
file_path = i.split(file_name)[0]
suit = unittest.defaultTestLoader.discover(file_path, file_name)
all_suit.addTests(suit)
return all_suit
最后:调用mian函数执行测试用例集
if __name__ == '__main__':
runner = unittest.TextTestRunner()
runner.run(test_suit())
以下为框架目录:
标签:__,读取,测试用例,file,suit,path,cases,txt 来源: https://blog.csdn.net/qq_35395179/article/details/114196847