Skip to content

Transforms complex documents like PDFs and Office docs into LLM-ready markdown/JSON for your Agentic workflows.

test: initialize regression environment in pytest configure

myhloli4204d49b0c51
diff --git a/tests/conftest.py b/tests/conftest.py
index 5746a8c8..76cc48e0 100644
--- a/tests/conftest.py
+++ b/tests/conftest.py
@@ -7,17 +7,21 @@ import os
import pytest
-# ONNX Runtime 1.30 的 1DS 遥测线程可能在解释器析构时崩溃,普通回归在导入前默认关闭遥测。
-os.environ.setdefault("ORT_DISABLE_TELEMETRY", "1")
-# Typer 的 help 文案在 import 期求值,必须在任何 mineru 模块导入前固定 CLI 语言,
-# 否则中文 locale 机器上既有的英文输出断言会失败。i18n 专项测试自行 monkeypatch 覆盖。
-os.environ["MINERU_LANG"] = "en"
[email protected](tryfirst=True)
+def pytest_configure() -> None:
+ """在收集测试模块前初始化环境,避免单独导入 conftest 时产生配置副作用。"""
+ # ONNX Runtime 1.30 的 1DS 遥测线程可能在解释器析构时崩溃,普通回归在导入前默认关闭遥测。
+ os.environ.setdefault("ORT_DISABLE_TELEMETRY", "1")
-# GitHub Actions 设置 GITHUB_ACTIONS=true 会使 typer/rich 强制彩色输出,
-# OptionHighlighter 会把 "--option" 拆成多段 ANSI 样式,破坏测试的子串断言;
-# 在导入 typer 前统一关闭终端强制模式,保证跨环境输出确定。
-os.environ.setdefault("_TYPER_FORCE_DISABLE_TERMINAL", "1")
+ # Typer 的 help 文案在 import 期求值,必须在任何 mineru 模块导入前固定 CLI 语言,
+ # 否则中文 locale 机器上既有的英文输出断言会失败。i18n 专项测试自行 monkeypatch 覆盖。
+ os.environ["MINERU_LANG"] = "en"
+
+ # GitHub Actions 设置 GITHUB_ACTIONS=true 会使 typer/rich 强制彩色输出,
+ # OptionHighlighter 会把 "--option" 拆成多段 ANSI 样式,破坏测试的子串断言;
+ # 在导入 typer 前统一关闭终端强制模式,保证跨环境输出确定。
+ os.environ.setdefault("_TYPER_FORCE_DISABLE_TERMINAL", "1")
@pytest.fixture(autouse=True)
diff --git a/tests/unittest/test_pytest_environment.py b/tests/unittest/test_pytest_environment.py
new file mode 100644
index 00000000..0b810fb6
--- /dev/null
+++ b/tests/unittest/test_pytest_environment.py
@@ -0,0 +1,68 @@
+"""验证测试环境初始化的导入边界与 pytest 生命周期顺序。"""
+
+from __future__ import annotations
+
+import os
+import runpy
+import shutil
+import subprocess
+import sys
+from pathlib import Path
+
+import pytest
+
+
[email protected]("configured_value", [None, "0", ""])
+def test_environment_initialization_waits_for_pytest_configure(
+ monkeypatch: pytest.MonkeyPatch, configured_value: str | None
+) -> None:
+ """单独导入配置不修改环境,执行钩子后保留显式配置并固定测试语言。"""
+ names = ("ORT_DISABLE_TELEMETRY", "MINERU_LANG", "_TYPER_FORCE_DISABLE_TERMINAL")
+ for name in names:
+ if configured_value is None:
+ monkeypatch.delenv(name, raising=False)
+ else:
+ monkeypatch.setenv(name, configured_value)
+ environment = dict(os.environ)
+
+ configuration = runpy.run_path(str(Path(__file__).resolve().parents[1] / "conftest.py"))
+
+ assert dict(os.environ) == environment
+ configuration["pytest_configure"]()
+ assert os.environ["MINERU_LANG"] == "en"
+ expected_default = "1" if configured_value is None else configured_value
+ assert os.environ["ORT_DISABLE_TELEMETRY"] == expected_default
+ assert os.environ["_TYPER_FORCE_DISABLE_TERMINAL"] == expected_default
+
+
+def test_pytest_configures_environment_before_test_module_import(tmp_path: Path) -> None:
+ """通过真实 pytest 收集入口确认配置先于测试模块导入生效。"""
+ shutil.copyfile(Path(__file__).resolve().parents[1] / "conftest.py", tmp_path / "conftest.py")
+ (tmp_path / "test_environment.py").write_text(
+ '''import os
+
+assert os.environ["ORT_DISABLE_TELEMETRY"] == "1"
+assert os.environ["MINERU_LANG"] == "en"
+assert os.environ["_TYPER_FORCE_DISABLE_TERMINAL"] == "1"
+
+def test_environment() -> None:
+ """提供一个可收集的测试,环境断言已在模块导入时执行。"""
+''',
+ encoding="utf-8",
+ )
+ environment = dict(os.environ)
+ environment.pop("ORT_DISABLE_TELEMETRY", None)
+ environment.pop("_TYPER_FORCE_DISABLE_TERMINAL", None)
+ environment.update(MINERU_LANG="zh", PYTEST_ADDOPTS="", PYTEST_DISABLE_PLUGIN_AUTOLOAD="1", GITHUB_ACTIONS="true")
+
+ result = subprocess.run(
+ [sys.executable, "-m", "pytest", "--collect-only", "-q", "-o", "addopts="],
+ cwd=tmp_path,
+ env=environment,
+ capture_output=True,
+ text=True,
+ timeout=30,
+ )
+
+ assert result.returncode == 0, result.stdout + result.stderr
+ assert "1 test collected" in result.stdout