pytest 测试报告定期删除

我们在服务器部署自动化测试框架时,随着运行次数的增加,不可避免的就是测试报告的冗余

下面方法便可定期进行数据报告的删除

在pytest框架中可引入 fixture函数,进行每次用例执行前后调用此方法,fixture为pytest的精髓所在,详细用法不做解释,可参考官方使用文档

# 报告删除1
@pytest.fixture(scope='session')
def Clear_logs(N):
N=3
file_list = get_all_file()
# 遍历报告文件列表
for file in file_list:
if file.startswith('../report') and len(file) <= 37:
# 拆分时间后缀
f = os.path.split(file)[1]
t = f[-19:-9]

# 判断是否大于前N天时间,单位为:秒
if time.time() - StrToTimestamp(t) >= 24 * 60 * 60 * int(N):
print('当前删除文件夹: %s' % file)
# 递归删除
shutil.rmtree(file)

# 返回目标日志文件下的文件夹列表2
def get_all_file(path='../reports/'):
file_list = []
# 向下遍历报告文件夹,cur_path:根目录 ,cur_dirs:子级文件夹 ,cur_files: ,
for cur_path, cur_dirs, cur_files in os.walk(path):
for name in cur_dirs:
file_list.append(os.path.join(cur_path, name))
return file_list

# 格式化时间转时间戳3
def StrToTimestamp(Str=None, format='%Y-%m-%d'):
# 格式化时间转时间戳:
if Str:
# 时间字符串解析为时间元组。
timep = time.strptime(Str, format)
res = time.mktime(timep)
else:
res = time.time()
return int(res)
posted @ 2021-06-22 17:38  老祝头  阅读(155)  评论(0编辑  收藏  举报