132 lines
4.3 KiB
Python
132 lines
4.3 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""
|
|
@Module: tests.run_all_tests
|
|
@FileName: run_all_tests.py
|
|
@Author: 小布丁
|
|
@CreateTime: 2026-08-06
|
|
@Description: 统一测试运行器
|
|
用法:
|
|
python tests/run_all_tests.py # 仅运行离线测试
|
|
python tests/run_all_tests.py --online # 运行全部测试(含在线)
|
|
python tests/run_all_tests.py --list # 列出所有测试
|
|
python tests/run_all_tests.py test_05 # 运行指定测试
|
|
"""
|
|
|
|
import argparse
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
|
|
# 测试文件列表(按依赖顺序排列)
|
|
ALL_TESTS = [
|
|
# 离线测试(无需登录,可独立运行)
|
|
("test_04_parsers", "离线", "解析函数(排班/打卡/加班表单)"),
|
|
("test_05_leave_validator", "离线", "请假校验模块"),
|
|
|
|
# 在线测试(需登录态,有依赖顺序)
|
|
("test_01_login_session", "在线", "登录 + 会话 + 待办列表"),
|
|
("test_02_form_detail", "在线", "SSO + WfActivity + WorkformEngine"),
|
|
("test_03_attendance", "在线", "排班 + 打卡明细接口"),
|
|
("test_06_leave_online", "在线", "请假记录 + 全流程"),
|
|
]
|
|
|
|
# 在线测试需要在离线测试之后运行
|
|
ONLINE_TESTS = [t for t in ALL_TESTS if t[1] == "在线"]
|
|
OFFLINE_TESTS = [t for t in ALL_TESTS if t[1] == "离线"]
|
|
|
|
|
|
def print_header(text: str):
|
|
"""打印分隔标题"""
|
|
width = 60
|
|
print(f"\n{'=' * width}")
|
|
print(f" {text}")
|
|
print(f"{'=' * width}")
|
|
|
|
|
|
def run_test(test_name: str) -> bool:
|
|
"""运行单个测试文件,返回是否成功"""
|
|
test_file = os.path.join(os.path.dirname(__file__), "hrbp", f"{test_name}.py")
|
|
if not os.path.exists(test_file):
|
|
print(f" [错误] 测试文件不存在: {test_file}")
|
|
return False
|
|
|
|
print(f"\n ▶ 运行: {test_name}")
|
|
result = subprocess.run(
|
|
[sys.executable, test_file],
|
|
capture_output=False,
|
|
cwd=os.path.join(os.path.dirname(__file__), ".."),
|
|
)
|
|
return result.returncode == 0
|
|
|
|
|
|
def list_tests():
|
|
"""列出所有测试"""
|
|
print_header("可用测试列表")
|
|
for name, kind, desc in ALL_TESTS:
|
|
print(f" [{kind}] {name:30s} {desc}")
|
|
print(f"\n 离线测试: {len(OFFLINE_TESTS)} 个")
|
|
print(f" 在线测试: {len(ONLINE_TESTS)} 个")
|
|
print(f" 共计: {len(ALL_TESTS)} 个")
|
|
|
|
|
|
def run_selected(name: str) -> bool:
|
|
"""运行指定测试(支持模糊匹配)"""
|
|
matched = [t for t in ALL_TESTS if name in t[0]]
|
|
if not matched:
|
|
print(f"未找到匹配 '{name}' 的测试")
|
|
return False
|
|
|
|
success = True
|
|
for test_name, kind, desc in matched:
|
|
print_header(f"运行: {test_name} ({kind} - {desc})")
|
|
if not run_test(test_name):
|
|
success = False
|
|
return success
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description="Process-Automation 统一测试运行器")
|
|
parser.add_argument("--online", action="store_true", help="同时运行在线测试(需登录态)")
|
|
parser.add_argument("--list", action="store_true", help="列出所有测试")
|
|
parser.add_argument("test", nargs="?", help="运行指定测试(支持模糊匹配)")
|
|
args = parser.parse_args()
|
|
|
|
if args.list:
|
|
list_tests()
|
|
return
|
|
|
|
if args.test:
|
|
run_selected(args.test)
|
|
return
|
|
|
|
# 默认:运行所有离线测试
|
|
all_pass = True
|
|
print_header("Process-Automation 测试套件")
|
|
print(f" 离线测试: {len(OFFLINE_TESTS)} 个")
|
|
if args.online:
|
|
print(f" 在线测试: {len(ONLINE_TESTS)} 个")
|
|
print()
|
|
|
|
for test_name, kind, desc in OFFLINE_TESTS:
|
|
print(f"\n [{kind}] {desc}")
|
|
if not run_test(test_name):
|
|
all_pass = False
|
|
|
|
if args.online:
|
|
print(f"\n{'─' * 60}")
|
|
print(" 开始在线测试...")
|
|
for test_name, kind, desc in ONLINE_TESTS:
|
|
print(f"\n [{kind}] {desc}")
|
|
if not run_test(test_name):
|
|
all_pass = False
|
|
|
|
print_header("测试结果汇总")
|
|
if all_pass:
|
|
print(" ✅ 全部通过")
|
|
else:
|
|
print(" ❌ 存在失败项")
|
|
print(f"{'=' * 60}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main() |