Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
chenjing
/
api-test
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
Commit
472d5418
...
472d54189c29df51eadc4f27d24d90f7311c60f9
authored
2026-01-21 15:50:45 +0800
by
chenjing
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
更新代码
1 parent
7a09b152
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
40 additions
and
14 deletions
api_test.py
api_test.py
View file @
472d541
...
...
@@ -80,7 +80,7 @@ def run_api_test(cases):
# 新增:根据env_scope和当前环境判断是否执行
env_scope
=
str
(
case
.
get
(
'env_scope'
,
''
))
.
strip
()
if
not
should_run_in_env
(
env_scope
,
current_env
):
print
(
f
"
⏭️ 跳过用例 case_id: {case.get('case_id')},原因:不在{current_env}环境执行范围内
(env_scope: {env_scope})"
)
print
(
f
"
[SKIP] case_id: {case.get('case_id')} - not in {current_env} env
(env_scope: {env_scope})"
)
continue
# 变量替换,优先用ip_host+path拼接url
...
...
@@ -476,27 +476,53 @@ def read_cases_from_excel(file_path, sheet_name='Sheet1'):
if
__name__
==
"__main__"
:
import
sys
import
glob
# 删除旧的测试报告
for
f
in
os
.
listdir
(
'.'
):
if
(
f
.
startswith
(
'api_test_report_'
)
and
f
.
endswith
(
'.xlsx'
))
or
(
f
.
startswith
(
'api_test_report_'
)
and
f
.
endswith
(
'.html'
)):
# 创建按环境分类的报告目录并清理旧报告
deploy_env
=
get_deploy_env
()
reports_base_dir
=
'reports'
env_reports_dir
=
os
.
path
.
join
(
reports_base_dir
,
deploy_env
)
# 确保目录存在
os
.
makedirs
(
env_reports_dir
,
exist_ok
=
True
)
# 清理超过5个的旧报告(保留最近5个)
def
cleanup_old_reports
(
env_dir
,
keep_count
=
5
):
"""保留最近N个报告,删除更旧的"""
try
:
os
.
remove
(
f
)
except
Exception
:
pass
# 获取所有报告文件(只统计xlsx,因为html和xlsx是成对的)
xlsx_files
=
sorted
(
glob
.
glob
(
os
.
path
.
join
(
env_dir
,
'*.xlsx'
)))
if
len
(
xlsx_files
)
>
keep_count
:
# 删除最旧的报告(成对删除html和xlsx)
files_to_delete
=
xlsx_files
[:
-
keep_count
]
for
xlsx_file
in
files_to_delete
:
html_file
=
xlsx_file
.
replace
(
'.xlsx'
,
'.html'
)
try
:
if
os
.
path
.
exists
(
xlsx_file
):
os
.
remove
(
xlsx_file
)
if
os
.
path
.
exists
(
html_file
):
os
.
remove
(
html_file
)
print
(
f
"[CLEANUP] Deleted old report: {os.path.basename(xlsx_file)}"
)
except
Exception
as
e
:
print
(
f
"[CLEANUP ERROR] Failed to delete {xlsx_file}: {e}"
)
except
Exception
as
e
:
print
(
f
"[CLEANUP ERROR] {e}"
)
cleanup_old_reports
(
env_reports_dir
,
keep_count
=
5
)
# 支持多个Excel文件
excel_files
=
[
f
for
f
in
os
.
listdir
(
'.'
)
if
f
.
startswith
(
'api_cases'
)
and
f
.
endswith
(
'.xlsx'
)]
all_passed
=
True
# 标记所有用例是否都通过
for
excel_path
in
excel_files
:
deploy_env
=
get_deploy_env
()
report_path
=
f
"api_test_report_{excel_path.replace('.xlsx', '')}_{deploy_env}_{datetime.now().strftime('
%
Y
%
m
%
d_
%
H
%
M
%
S')}.xlsx"
report_filename
=
f
"api_test_report_{excel_path.replace('.xlsx', '')}_{deploy_env}_{datetime.now().strftime('
%
Y
%
m
%
d_
%
H
%
M
%
S')}"
report_path
=
os
.
path
.
join
(
env_reports_dir
,
f
"{report_filename}.xlsx"
)
html_path
=
report_path
.
replace
(
'.xlsx'
,
'.html'
)
cases
=
read_cases_from_excel
(
excel_path
)
print
(
f
'
读取到的用例({excel_path}):'
,
cases
)
print
(
f
'
[INFO] Loaded test cases from {excel_path}'
)
results
=
run_api_test
(
cases
)
print
(
f
'
最终results({excel_path}):'
,
results
)
print
(
f
'
[INFO] Test execution completed for {excel_path}'
)
write_report_to_excel
(
results
,
report_path
)
write_report_to_html
(
results
,
html_path
)
...
...
@@ -504,12 +530,12 @@ if __name__ == "__main__":
failed_count
=
sum
(
1
for
r
in
results
if
r
.
get
(
'test_result'
)
in
[
'失败'
,
'异常'
])
if
failed_count
>
0
:
all_passed
=
False
print
(
f
"
\n
⚠️ {excel_path}: 有 {failed_count} 个用例执行失败
"
)
print
(
f
"
\n
[WARNING] {excel_path}: {failed_count} test cases failed
"
)
# 根据结果返回退出码
if
all_passed
:
print
(
"
\n
✅ 所有测试用例运行成功!
"
)
print
(
"
\n
[SUCCESS] All test cases passed!
"
)
sys
.
exit
(
0
)
# Jenkins: 执行成功
else
:
print
(
"
\n
❌ 有测试用例执行失败,请查看报告详情!
"
)
print
(
"
\n
[FAILED] Some test cases failed, check the report for details!
"
)
sys
.
exit
(
1
)
# Jenkins: 执行失败
...
...
Please
register
or
sign in
to post a comment