本地跑:tools/run_tests.py + README「本地怎么跑」(239 测试)

This commit is contained in:
2026-11-22 20:33:31 +08:00
parent ef201ffacc
commit ab05221105
6 changed files with 80 additions and 2 deletions
+33
View File
@@ -0,0 +1,33 @@
"""一条命令跑全部单测:python tools/run_tests.py
为什么不用 `unittest discover`:`tests/` 不是包(没有 `__init__.py`,故意的,
省得 import 时踩相对导入),`discover -s tests -t .` 会直接报
`Start directory is not importable`。这里按文件名显式列出,稳。
"""
from __future__ import annotations
import glob
import os
import sys
import unittest
from pathlib import Path
ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT))
os.chdir(ROOT)
def main() -> int:
mods = ["tests." + os.path.basename(p)[:-3]
for p in sorted(glob.glob("tests/test_*.py"))]
if not mods:
print("没找到 tests/test_*.py")
return 1
suite = unittest.defaultTestLoader.loadTestsFromNames(mods)
res = unittest.TextTestRunner(verbosity=1).run(suite)
return 0 if res.wasSuccessful() else 1
if __name__ == "__main__":
raise SystemExit(main())