代码拉取完成,页面将自动刷新
import os
import pandas as pd
import re
print("start processing test results")
# 创建一个字典来统计每个 `a` 部分出现的次数
count={}
def process_txt_file(file_path,test_name):
data = []
with open(file_path, 'r',errors='ignore') as file:
lines=file.readlines()
# 修改判断状态为 Y 的条件:是否存在仅包含 'PASS' 的行(允许空格)
status = 'N' # 默认状态为失败
for line in lines:
if line.strip() == 'PASS': # 去掉两端空格后判断是否为 'PASS'
status = 'Y'
break
# 判断文件是否视为错误
if len(lines) < 5:
# 使用正则表达式匹配 "ok istio.io",中间允许任意数量的空格
has_ok_istio = any(re.match(r'^ok.*', line) for line in lines)
if not has_ok_istio:
print("\nTEST_NAME:", test_name, "\ngo test error \n",
"error_test_file_path:", lines[-1] if lines else "No lines",
"error_test_output_dir:", file_path)
status = 'N'
name = lines[-2].strip() + lines[-1].strip() if len(lines) >= 2 else "Unknown Test"
data.append([name, status, lines[0].strip() if lines else "No content"])
return data
else:
status = 'Y'
# 根据出现次数构造测试名
if(count[test_name]>1):
name=lines[-2].strip()+lines[-1].strip()
else:
name = lines[-2].strip()
flag=''
for i in range(len(lines)):
if lines[i].startswith("--- PASS"):
flag=lines[i].strip()
elif lines[i].startswith("--- FAIL"):
flag=lines[i].strip()
# 根据 status 动态决定最后一部分是 PASS 还是 FAIL
status_result = 'PASS' if status == 'Y' else 'FAIL'
if flag == '':
detail = "'" + status_result
else:
detail = "'" + lines[0].strip() + "\n" + flag + "\n" + status_result
data.append([name,status,detail])
return data
def count_a_occurrences(directory):
# 遍历目录中的所有文件
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith('.txt'):
parts = file.split('-', 1)
if len(parts) == 2:
test_name = parts[0]
if test_name in count:
count[test_name] += 1
else:
count[test_name] = 1
def main(input_directory, output_file):
count_a_occurrences(input_directory)
all_data = []
for file_name in os.listdir(input_directory):
if file_name.endswith('.txt'):
parts=file_name.split('-',1)
file_path = os.path.join(input_directory, file_name)
file_data = process_txt_file(file_path,parts[0])
if file_data:
for i in range(len(file_data)):
all_data.append(file_data[i])
# Create a DataFrame
columns = ['Test Name','Status','Run Details']
df = pd.DataFrame(all_data, columns=columns)
# Write DataFrame to an Excel file
df.to_excel(output_file, index=False)
print("success save test results to test_result.xlsx")
if __name__ == "__main__":
input_directory = 'test' # Change to your directory
output_file = 'test_result.xlsx'
main(input_directory, output_file)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。