Pytest框架可以结合Pytest-allure插件,生成漂亮的allure测试报告,但是在生成报告文件后我们怎样能够按照自定义时间清除对应的测试报告呢,且听分解
首先生成测试报告文件的时候可以将生成的报告的:.xml,.html文件可以根据时间戳添加后缀名
if __name__ == '__main__':
clearlogs().Clear_logs()
file_name = os.path.basename(__file__).split('.')[0]
time_stamp = str(int(time.time()))
new_dir_name = str(file_name) + time_stamp
new_html_path = f'{BASE_PATH}/report/html/' + new_dir_name
os.mkdir(new_html_path)
pytest.main(['-s', '-v', '-W',
'--reruns=1', '--reruns-delay=2',
'--alluredir', f'{BASE_PATH}/report/xml/' + new_dir_name])
command_word = f'allure generate {BASE_PATH}/report/xml/{new_dir_name} -o {BASE_PATH}/report/html/{new_dir_name} --clean'
os.system(command_word)
先找出生成的html文件
def get_all_html(self):
html_list = []
for cur_path, cur_dirs, cur_files in os.walk(self.HTML):
for name in cur_dirs:
html_list.append(os.path.join(cur_path, name))
return html_list
再找出生成的xml文件
def get_all_xml(self):
xml_list = []
for cur_path, cur_dirs, cur_files in os.walk(self.XML):
for name in cur_dirs:
xml_list.append(os.path.join(cur_path, name))
return xml_list
统计出生成的.html+.xml文件名称
def get_all_file(self):
all_html = clearlogs().get_all_html()
all_xml = clearlogs().get_all_xml()
res_list = UtilsAll().add_list(all_html, all_xml)
return res_list
将大于N天的根据配置报告文件删除
def Clear_logs(self):
file_list = clearlogs().get_all_file()
for file in file_list:
f = os.path.split(file)[1]
if re.match('main', f) is not None:
t = f[-10:]
if int(time.time()) - int(t) >= 24 * 60 * 60 * int(self.N):
self.log.info(f"开始执行清理{self.N}天以前测试报告文件")
self.log.info(f"正在清理的报告文件:{file}")
shutil.rmtree(file)
self.log.info(f"清理报告:{file}完成")
else:
pass
定义 cleanlogs类
class clearlogs(object):
def __init__(self):
self.N = NUMBER
self.HTML = RESULT_HTML_FILE_PATH
self.XML = RESULT_XML_FILE_PATH
self.log = Logger().my_log()