33解析 serial_opt.txt 日志文件并上报测试结果到后端API
44
55用法:
6- python parse_and_upload.py <log_file> <api_url> --branch <branch_name> --commit <commit_id> [--test-type <test_type>] [--dry-run]
6+ python parse_and_upload.py <log_file> <api_url> --branch <branch_name> --commit <commit_id> [--test-type <test_type>] [--dry-run] [--verbose]
77
88环境变量:
99 API_KEY: 后端API的认证密钥(非dry-run模式需要)
1313 export API_KEY=your_api_key_here
1414 python parse_and_upload.py serial_opt.txt http://localhost:8080/api/v1 --branch main --commit abc123def456
1515
16- # Dry-run模式(调试用 ,不需要API_KEY)
16+ # Dry-run模式(默认只显示统计信息 ,不需要API_KEY)
1717 python parse_and_upload.py serial_opt.txt http://localhost:8080/api/v1 --branch main --commit abc123def456 --dry-run
18+
19+ # Dry-run模式(显示完整详细信息,包括测试用例详情和JSON数据)
20+ python parse_and_upload.py serial_opt.txt http://localhost:8080/api/v1 --branch main --commit abc123def456 --dry-run --verbose
1821"""
1922
2023import os
@@ -572,6 +575,14 @@ def main():
572575 help = '干运行模式:只解析并显示结果,不上传到服务器'
573576 )
574577
578+ parser .add_argument (
579+ '--verbose' ,
580+ '--full-output' ,
581+ dest = 'verbose' ,
582+ action = 'store_true' ,
583+ help = '详细输出模式:在dry-run模式下显示完整的测试用例详情和JSON数据(默认只显示统计信息)'
584+ )
585+
575586 args = parser .parse_args ()
576587
577588 # 在dry-run模式下,不需要API Key
@@ -600,27 +611,19 @@ def main():
600611 print ("警告: 未找到任何测试用例" , file = sys .stderr )
601612 sys .exit (1 )
602613
603- print (f"✓ 找到 { len (test_cases )} 个测试用例" )
604-
605- # 显示解析结果摘要
614+ # 统计测试用例状态
606615 status_count = {}
607616 for tc in test_cases :
608617 status = tc .get ('status' , 'unknown' )
609618 status_count [status ] = status_count .get (status , 0 ) + 1
610619
611- print ("\n 测试用例状态统计:" )
612- for status , count in sorted (status_count .items ()):
613- print (f" { status } : { count } " )
614-
615620 # 根据测试用例状态确定整体状态
616621 overall_status = "passed"
617622 for tc in test_cases :
618623 if tc .get ('status' ) == 'failed' :
619624 overall_status = "failed"
620625 break
621626
622- print (f"\n 整体状态: { overall_status } " )
623-
624627 # 构建将要上传的payload
625628 payload = {
626629 "branch_name" : args .branch_name ,
@@ -630,31 +633,52 @@ def main():
630633 "test_cases" : test_cases
631634 }
632635
633- # 在dry-run模式下,显示详细信息
636+ # 在dry-run模式下,显示信息
634637 if args .dry_run :
635- # 显示测试用例详情
636- print_test_cases_details (test_cases )
637-
638- # 显示将要上传的JSON
639- print ("\n " + "=" * 80 )
640- print ("将要上传的JSON数据:" )
641- print ("=" * 80 )
642- print (json .dumps (payload , indent = 2 , ensure_ascii = False ))
643-
644- # 显示API信息
645- if not args .api_url .endswith ('/test-runs' ):
646- if args .api_url .endswith ('/' ):
647- url = f"{ args .api_url } test-runs"
638+ # 非verbose模式:只显示简洁的统计信息
639+ if not args .verbose :
640+ passed_count = status_count .get ('passed' , 0 )
641+ failed_count = status_count .get ('failed' , 0 )
642+ skipped_count = status_count .get ('skipped' , 0 )
643+ print (f"测试通过 { passed_count } 个,失败 { failed_count } 个" , end = "" )
644+ if skipped_count > 0 :
645+ print (f",跳过 { skipped_count } 个" )
648646 else :
649- url = f" { args . api_url } /test-runs"
647+ print ()
650648 else :
651- url = args .api_url
649+ # verbose模式:显示详细信息
650+ print (f"✓ 找到 { len (test_cases )} 个测试用例" )
651+
652+ print ("\n 测试用例状态统计:" )
653+ for status , count in sorted (status_count .items ()):
654+ print (f" { status } : { count } " )
655+
656+ print (f"\n 整体状态: { overall_status } " )
657+
658+ # 显示测试用例详情
659+ print_test_cases_details (test_cases )
660+
661+ # 显示将要上传的JSON
662+ print ("\n " + "=" * 80 )
663+ print ("将要上传的JSON数据:" )
664+ print ("=" * 80 )
665+ print (json .dumps (payload , indent = 2 , ensure_ascii = False ))
666+
667+ # 显示API信息
668+ if not args .api_url .endswith ('/test-runs' ):
669+ if args .api_url .endswith ('/' ):
670+ url = f"{ args .api_url } test-runs"
671+ else :
672+ url = f"{ args .api_url } /test-runs"
673+ else :
674+ url = args .api_url
675+
676+ print ("\n " + "=" * 80 )
677+ print (f"目标API地址: { url } " )
678+ print (f"请求方法: POST" )
679+ print (f"Content-Type: application/json" )
680+ print ("=" * 80 )
652681
653- print ("\n " + "=" * 80 )
654- print (f"目标API地址: { url } " )
655- print (f"请求方法: POST" )
656- print (f"Content-Type: application/json" )
657- print ("=" * 80 )
658682 print ("\n ✓ Dry-run 完成,未实际上传数据" )
659683 return
660684
0 commit comments