63 lines
1.6 KiB
Python
63 lines
1.6 KiB
Python
import os
|
|
import shutil
|
|
import subprocess
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
|
|
CURRENT_FILE_PATH = Path(__file__).resolve()
|
|
PROJECT_ROOT = CURRENT_FILE_PATH.parent.parent
|
|
ALLURE_RESULTS_DIR = PROJECT_ROOT / "allure-results"
|
|
ALLURE_REPORT_DIR = PROJECT_ROOT / "allure-report"
|
|
LOCAL_ALLURE_PATH = PROJECT_ROOT / "allure" / "allure-2.28.0" / "bin" / "allure.bat"
|
|
|
|
|
|
def _has_alluredir_arg(args: list[str]) -> bool:
|
|
return any(arg == "--alluredir" or arg.startswith("--alluredir=") for arg in args)
|
|
|
|
|
|
def _allure_command() -> str | None:
|
|
env_allure_path = os.environ.get("ALLURE_PATH")
|
|
if env_allure_path:
|
|
return env_allure_path
|
|
|
|
if LOCAL_ALLURE_PATH.exists():
|
|
return str(LOCAL_ALLURE_PATH)
|
|
|
|
return shutil.which("allure")
|
|
|
|
|
|
def _generate_allure_report() -> None:
|
|
allure = _allure_command()
|
|
if not allure:
|
|
print("未找到 allure 命令,跳过 HTML 报告生成。")
|
|
return
|
|
|
|
command = [
|
|
allure,
|
|
"generate",
|
|
str(ALLURE_RESULTS_DIR),
|
|
"-o",
|
|
str(ALLURE_REPORT_DIR),
|
|
"--clean",
|
|
]
|
|
subprocess.run(command, cwd=PROJECT_ROOT, check=False)
|
|
|
|
|
|
def main() -> int:
|
|
tests_dir = CURRENT_FILE_PATH.parent
|
|
pytest_args = sys.argv[1:]
|
|
|
|
command = [sys.executable, "-m", "pytest", str(tests_dir)]
|
|
if not _has_alluredir_arg(pytest_args):
|
|
command.append(f"--alluredir={ALLURE_RESULTS_DIR}")
|
|
command.extend(pytest_args)
|
|
|
|
completed = subprocess.run(command, cwd=PROJECT_ROOT)
|
|
_generate_allure_report()
|
|
return completed.returncode
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|