Files
feiqiu-ETL/etl_billiards/scripts/test_presets.py
2025-11-19 03:36:44 +08:00

167 lines
5.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# -*- coding: utf-8 -*-
"""测试命令“仓库”,集中维护 run_tests.py 的预置组合。
支持的参数键(可在 PRESETS 中自由组合):
1. suite
- 类型:列表
- 作用:引用 run_tests 中的预置套件,值可为 online / offline / integration 等。
- 用法:["online"] 仅跑在线模式;["online","offline"] 同时跑两套;["integration"] 跑数据库集成测试。
2. tests
- 类型:列表
- 作用:传入任意 pytest 目标路径,适合补充临时/自定义测试文件。
- 用法:["tests/unit/test_config.py","tests/unit/test_parsers.py"]。
3. mode
- 类型:字符串
- 取值ONLINE 或 OFFLINE。
- 作用:覆盖 TEST_MODEONLINE 走 API 全流程OFFLINE 读取 JSON 归档执行 T+L。
4. db_dsn
- 类型:字符串
- 作用:设置 TEST_DB_DSN指定真实 PostgreSQL 连接;缺省时测试引擎使用伪 DB仅记录写入不触库。
- 示例postgresql://user:pwd@localhost:5432/testdb。
5. json_archive / json_temp
- 类型:字符串
- 作用:离线模式的 JSON 归档目录 / 临时输出目录。
- 说明:不设置时沿用 .env 或默认值;仅在 OFFLINE 模式需要关注。
6. keyword
- 类型:字符串
- 作用:等价 pytest -k用于筛选测试名/节点。
- 示例:"ORDERS" 可只运行包含该关键字的测试函数。
7. pytest_args
- 类型:字符串
- 作用:附加 pytest 命令行参数。
- 示例:"-vv --maxfail=1 --disable-warnings"
8. env
- 类型:列表
- 作用:追加环境变量,形如 ["STORE_ID=123","API_TOKEN=xxx"],会在 run_tests 内透传给 os.environ。
9. preset_meta
- 类型:字符串
- 作用:纯注释信息,便于描述该预置组合的用途,不会传递给 run_tests。
运行方式建议直接 F5或 `python scripts/test_presets.py`),脚本将读取 AUTO_RUN_PRESETS 中的配置依次执行。
如需临时指定其它预置,可传入 `--preset xxx``--list` 用于查看所有参数说明和预置详情。
"""
from __future__ import annotations
import argparse
import os
import subprocess
import sys
from typing import List
RUN_TESTS_SCRIPT = os.path.join(os.path.dirname(__file__), "run_tests.py")
AUTO_RUN_PRESETS = ["online_orders"]
# PRESETS = {
# "online_orders": {
# "suite": ["online"],
# "mode": "ONLINE",
# "keyword": "ORDERS",
# "pytest_args": "-vv",
# "preset_meta": "在线模式,仅跑订单任务,输出更详细日志",
# },
# "offline_realdb": {
# "suite": ["offline"],
# "mode": "OFFLINE",
# "db_dsn": "postgresql://user:pwd@localhost:5432/testdb",
# "json_archive": "tests/testdata_json",
# "preset_meta": "离线模式 + 真实测试库,用预置 JSON 回放全量任务",
# },
# "integration_db": {
# "suite": ["integration"],
# "db_dsn": "postgresql://user:pwd@localhost:5432/testdb",
# "preset_meta": "仅跑数据库连接/操作相关的集成测试",
# },
# }
PRESETS = {
"offline_realdb": {
"suite": ["offline"],
"mode": "OFFLINE",
"db_dsn": "postgresql://local-Python:Neo-local-1991125@100.64.0.4:5432/LLZQ-test",
"json_archive": "tests/testdata_json",
"preset_meta": "离线模式 + 真实测试库,用预置 JSON 回放全量任务",
},
}
def print_parameter_help():
print("可用参数键说明:")
print(" suite -> 预置测试套件列表,如 ['online','offline']")
print(" tests -> 自定义测试文件路径列表")
print(" mode -> TEST_MODEONLINE / OFFLINE")
print(" db_dsn -> TEST_DB_DSN连接真实 PostgreSQL")
print(" json_archive -> TEST_JSON_ARCHIVE_DIR离线 JSON 目录")
print(" json_temp -> TEST_JSON_TEMP_DIR离线临时目录")
print(" keyword -> pytest -k 过滤关键字")
print(" pytest_args -> 额外 pytest 参数(单个字符串)")
print(" env -> 附加环境变量,形如 ['KEY=VALUE']")
print(" preset_meta -> 注释说明,不会传给 run_tests")
print()
def print_presets():
if not PRESETS:
print("当前没有定义任何预置命令,可自行在 PRESETS 中添加。")
return
for idx, (name, payload) in enumerate(PRESETS.items(), start=1):
comment = payload.get("preset_meta", "")
print(f"{idx}. {name}")
if comment:
print(f" 说明: {comment}")
for key, value in payload.items():
if key == "preset_meta":
continue
print(f" {key}: {value}")
print()
def run_presets(preset_names: List[str], dry_run: bool):
cmds = []
for name in preset_names:
cmd = [sys.executable, RUN_TESTS_SCRIPT, "--preset", name]
cmds.append(cmd)
for cmd in cmds:
printable = " ".join(cmd)
if dry_run:
print(f"[Dry-Run] {printable}")
else:
print(f"\n>>> 执行: {printable}")
subprocess.run(cmd, check=False)
def main():
parser = argparse.ArgumentParser(description="测试预置仓库(在此集中配置并运行测试组合)")
parser.add_argument("--preset", choices=sorted(PRESETS.keys()), nargs="+", help="直接指定要运行的预置命令")
parser.add_argument("--list", action="store_true", help="仅列出参数键和所有预置命令")
parser.add_argument("--dry-run", action="store_true", help="仅打印将要执行的命令,而不真正运行")
args = parser.parse_args()
if args.list:
print_parameter_help()
print_presets()
return
if args.preset:
target = args.preset
else:
target = AUTO_RUN_PRESETS or list(PRESETS.keys())
run_presets(target, dry_run=args.dry_run)
if __name__ == "__main__":
main()