From aff47ee6de393012d2a42da13c464429507fad91 Mon Sep 17 00:00:00 2001 From: hrz <1710360675@qq.com> Date: Sat, 24 Oct 2026 07:27:15 +0000 Subject: [PATCH] =?UTF-8?q?=E8=A6=81=E7=9C=9F=E5=87=AD=E6=8D=AE=E7=9A=84?= =?UTF-8?q?=E7=94=A8=E4=BE=8B=E6=89=93=E6=A0=87=E8=AE=B0=E7=BB=9F=E4=B8=80?= =?UTF-8?q?=E5=89=94=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Edge TTS 那条要连微软公网,runner 的出口 IP 会被风控挡掉。以前在 CI 命令行上用 -k 排除,看不出排掉了什么。现在给它打 live_api 标记,由 conftest 统一剔除,日志里列出剔的是哪几条。 --- .github/workflows/test.yml | 6 ++--- main/xiaozhi-server/pyproject.toml | 3 +++ main/xiaozhi-server/tests/conftest.py | 33 ++++++++++++++++++++++++--- main/xiaozhi-server/tests/test_tts.py | 4 ++++ 4 files changed, 40 insertions(+), 6 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index a0d515a2f..499403b7c 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -42,9 +42,9 @@ jobs: - name: Run tests working-directory: main/xiaozhi-server run: | - # Edge TTS 那条要连微软公网,runner 的出口 IP 会被风控挡掉,先排除。 - # 根治办法是给它打 live 标记,让 mock 模式下自动不收(另开一条改) - pytest tests/ -v -k "not test_tts_text_to_speak_writes_file" + # 要真凭据/真网络的用例由 tests/conftest.py 按 live_api 标记统一剔除, + # 剔掉的是哪几条会打在日志里 + pytest tests/ -v java: name: Java (manager-api) diff --git a/main/xiaozhi-server/pyproject.toml b/main/xiaozhi-server/pyproject.toml index 6f0998775..b0d3be70a 100644 --- a/main/xiaozhi-server/pyproject.toml +++ b/main/xiaozhi-server/pyproject.toml @@ -5,6 +5,9 @@ python_files = ["test_*.py"] python_classes = ["Test*"] python_functions = ["test_*"] addopts = "-ra" +markers = [ + "live_api: 需要真凭据或真网络的用例,mock 模式下由 conftest 剔除并列出", +] filterwarnings = [ "ignore::DeprecationWarning", ] \ No newline at end of file diff --git a/main/xiaozhi-server/tests/conftest.py b/main/xiaozhi-server/tests/conftest.py index c8e009f5e..cb3a0ccc1 100644 --- a/main/xiaozhi-server/tests/conftest.py +++ b/main/xiaozhi-server/tests/conftest.py @@ -454,8 +454,35 @@ import pytest # noqa: E402 (after conftest hooks so pytest is initialised) def pytest_collection_modifyitems(config, items): - """Live 模式下:``live_api`` 标记的测试必须配齐密钥才能跑(否则 skip)。""" + """Mock 模式下:``live_api`` 标记的用例要真凭据或真网络,不收,并列出是哪几条。 + + 以前是在 CI 命令行上用 ``-k`` 排除,看不出排掉了什么、为什么排。 + 改成打标记 + 这里统一剔除,日志里会一条条列出来,剔了什么一目了然。 + """ if LIVE_API_TESTS: return - # Mock 模式下不强行 skip:测试应该都能跑(用 shim)。 - return + + keep = [] + drop = [] + for item in items: + if "live_api" in item.keywords: + drop.append(item) + else: + keep.append(item) + + if not drop: + return + + print( + "\n[conftest] 下面这些用例要真凭据或真网络,mock 模式下不收:", + flush=True, + ) + for item in drop: + print(f"[conftest] - {item.nodeid}", flush=True) + print( + "[conftest] 真请求模式:export RUN_LIVE_API_TESTS=1 并配好凭据后再跑。\n", + flush=True, + ) + + config.hook.pytest_deselected(items=drop) + items[:] = keep diff --git a/main/xiaozhi-server/tests/test_tts.py b/main/xiaozhi-server/tests/test_tts.py index 3d4a37cde..0ad3f001a 100644 --- a/main/xiaozhi-server/tests/test_tts.py +++ b/main/xiaozhi-server/tests/test_tts.py @@ -81,9 +81,13 @@ def test_tts_provider_has_output_dir() -> None: assert provider.output_file, "output_file 不应为空" +@pytest.mark.live_api def test_tts_text_to_speak_writes_file() -> None: """Mock 模式:断言 file 存在即可。Live 模式:断言 size > 0。 + 这条要连微软公网,runner 的出口 IP 会被风控挡掉,所以打了 live_api 标记: + mock 模式下由 conftest 统一剔除(会列出剔的是哪几条),live 模式下才收。 + 参考 performance_tester_tts.py 的实现: - mock conn(sample_rate/audio_format/stop_event/client_abort/headers) 和 opus_encoder(SimpleNamespace 占位即可)