背景:d759921 抽 _open_realtime_viewer_impl 时误删 16 行搜索工具栏定义, gui_app 启动时报 NameError,但 pytest 1421 passed 没抓住——def main() 里的 NameError 只有真正调用 main() 才触发,单纯 import 不会执行函数体。 新测试通过 patch tk.Misc.mainloop 让 GUI 创建完所有控件后立刻 quit, 覆盖 def main() 内部所有顶层语句,能抓住 NameError / AttributeError / ImportError 这类启动期失败。 已验证:临时 revertca6c2e4后该测试 FAIL(精确指向 L3994 NameError), 恢复ca6c2e4后 PASS。 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
45 lines
1.6 KiB
Python
45 lines
1.6 KiB
Python
"""gui/gui_app.py 启动冒烟测试。
|
|
|
|
背景:d759921 抽 _open_realtime_viewer_impl 时误删 16 行搜索工具栏定义,
|
|
导致 gui_app 启动时 NameError。但 pytest 跑 1421 passed 也没抓住——因为
|
|
def main() 里的 NameError 只有真正调用 main() 才触发,单纯 import 函数体
|
|
不执行。
|
|
|
|
这个测试通过 patch tk.Misc.mainloop 让 GUI 创建完所有控件后立刻 quit,
|
|
2 秒内验证 main() 能跑到 mainloop 不抛任何异常。
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import tkinter as tk
|
|
|
|
import pytest
|
|
|
|
|
|
def test_gui_app_main_runs_without_error(monkeypatch):
|
|
"""完整调用 gui.gui_app.main(),验证所有控件创建到 mainloop 不抛异常。
|
|
|
|
覆盖 def main() 内部所有顶层语句——能抓住 NameError / AttributeError /
|
|
ImportError 这类启动期失败。
|
|
"""
|
|
# patch mainloop:进入主循环就立刻 quit,让 main() 优雅返回
|
|
orig_mainloop = tk.Misc.mainloop
|
|
|
|
def patched_mainloop(self, *args, **kwargs):
|
|
# after 0 立即触发,但要等 mainloop 真正在跑后才会执行
|
|
self.after(0, self.quit)
|
|
return orig_mainloop(self, *args, **kwargs)
|
|
|
|
monkeypatch.setattr(tk.Misc, "mainloop", patched_mainloop)
|
|
|
|
# tkinter 在无显示环境会抛 TclError,CI 上跳过
|
|
try:
|
|
import gui.gui_app as gui_app
|
|
except ImportError as e:
|
|
pytest.skip(f"gui.gui_app 导入失败: {e}")
|
|
|
|
try:
|
|
gui_app.main()
|
|
except tk.TclError as e:
|
|
# 无显示环境 / DISPLAY 未设置等
|
|
pytest.skip(f"无法创建 Tk 窗口: {e}")
|