"""一条命令跑全部单测: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())