You need to enable JavaScript to run this app. 如何使用imgui_bundle测试引擎测试自研Python ImGui应用?
优惠活动
大模型
产品
解决方案
定价
更多

如何使用imgui_bundle测试引擎测试自研Python ImGui应用?

环境信息

  • Python 3.12
  • imgui_bundle 1.91.7 WIP

更新补充

  • 自研应用启动命令:python -m app.gui
  • 应用基于hello_imgui开发
  • 当前先尝试测试独立文件中的简易GUI

问题描述

我有一个包含多个依赖文件的Python应用(主文件为app.py),想要用imgui_bundle测试引擎为其编写GUI单元测试。但官方仅提供测试内部Demo应用的示例,文档也未说明如何测试自研的app.py脚本。我已在谷歌、Stack Overflow、Reddit及YouTube等渠道搜索相关内容,均未找到有效结果。

请问是否有人使用过imgui_bundle测试引擎测试自研ImGui应用?

相关代码

testgui.py

##- testgui.py
## much of this code framework is from imgui's demo_testengine
## I've simplified it to have a simple GUI, now I want to
## not use the simple gui, but instead my python app file(s)
from imgui_bundle import imgui, hello_imgui
from imgui_bundle.imgui.test_engine_checks import CHECK
from typing import List

# Tests to perfrom
test_click_button: imgui.test_engine.Test
test_open_close_node: imgui.test_engine.Test

g_show_stack_tool_window = False

def simple_gui():
    imgui.begin("App")
    imgui.text("Neanderthal simple gui")
    if imgui.button("Throg punch here"):
        print("Ouch")
    if imgui.tree_node("Drop Rock"):
        imgui.text("Ouch")
        imgui.tree_pop()
    imgui.end()

def my_register_tests():
    global test_click_button, test_open_close_node
    engine = hello_imgui.get_imgui_test_engine()

    #Test 1 click button
    test_click_button = imgui.test_engine.register_test(engine, "throg", "Can Throg push button")
    def test_config_values_func(ctx: imgui.test_engine.TestContext) -> None:
        #point to the reference window name this is the imgui.begin("NAME")
        ctx.set_ref("App")
        ctx.item_click("**/Throg punch here")
    test_click_button.test_func = test_config_values_func

    #Test 2 open and close node
    test_open_close_node = imgui.test_engine.register_test(engine, "throg", "Can Throg play with trees")
    def test_config_values_func(ctx: imgui.test_engine.TestContext) -> None:
        #point to the reference window name this is the imgui.begin("NAME")
        ctx.set_ref("App")
        ctx.item_click("**/Drop Rock")
        ctx.item_click("**/Drop Rock")
    test_open_close_node.test_func = test_config_values_func

def my_gui():
    global g_show_stack_tool_window, test_click_button, test_open_close_node
    _, g_show_stack_tool_window = imgui.checkbox("Show ID Stack Tool Window", g_show_stack_tool_window)
    if imgui.is_item_hovered():
        imgui.set_tooltip("This tool window can help to identify the ID of the widgets (use \"Copy path to clipboard\")")
    if g_show_stack_tool_window:
        imgui.show_id_stack_tool_window()

    test_engine = hello_imgui.get_imgui_test_engine()
    if imgui.button('Run "Click Button"'):
        imgui.test_engine.queue_test(test_engine, test_click_button)
    if imgui.button('Run "Tree Node"'):
        imgui.test_engine.queue_test(test_engine, test_open_close_node)

    engine_io = imgui.test_engine.get_io(test_engine)
    engine_io.config_run_speed = imgui.test_engine.TestRunSpeed.normal

def apply_application_layout(runner_params: hello_imgui.RunnerParams) -> None:
    ...

def main() -> None:
    runner_params = hello_imgui.RunnerParams()
    apply_application_layout(runner_params)

    runner_params.use_imgui_test_engine = True
    runner_params.callbacks.register_tests = my_register_tests

    hello_imgui.run(runner_params)

def create_default_docking_splits() -> List[hello_imgui.DockingSplit]:
    # Define the application layout: splits the window in 3 spaces
    split_main_demo = hello_imgui.DockingSplit()
    split_main_demo.initial_dock = "MainDockSpace"
    split_main_demo.new_dock = "ImGuiDemoSpace"
    split_main_demo.direction = imgui.Dir.right
    split_main_demo.ratio = 0.5

    split_main_test = hello_imgui.DockingSplit()
    split_main_test.initial_dock = "MainDockSpace"
    split_main_test.new_dock = "TestEngineSpace"
    split_main_test.direction = imgui.Dir.down
    split_main_test.ratio = 0.7

    return [split_main_demo, split_main_test]


def create_dockable_windows() -> List[hello_imgui.DockableWindow]:
    # Define the app windows: my_gui, ImGui Demo Window, Dear ImGui Test Engine
    my_window = hello_imgui.DockableWindow()
    my_window.label = "Run Demos"
    my_window.dock_space_name = "MainDockSpace"
    my_window.gui_function = my_gui

    dear_imgui_demo_window = hello_imgui.DockableWindow()
    dear_imgui_demo_window.label = "Dear ImGui Demo"
    dear_imgui_demo_window.dock_space_name = "ImGuiDemoSpace"
    dear_imgui_demo_window.gui_function = simple_gui# imgui.show_demo_window  # type: ignore

    test_engine_window = hello_imgui.DockableWindow()
    test_engine_window.label = "Dear ImGui Test Engine"
    test_engine_window.dock_space_name = "TestEngineSpace"

    def show_test_engine_windows():
        imgui.test_engine.show_test_engine_windows(
            hello_imgui.get_imgui_test_engine(), True
        )

    test_engine_window.gui_function = show_test_engine_windows

    return [my_window, dear_imgui_demo_window, test_engine_window]


def apply_application_layout(runner_params: hello_imgui.RunnerParams) -> None:  # type: ignore # noqa: F811
    # Define the application layout and windows
    runner_params.app_window_params.window_title = "Demo ImGui Test Engine"
    runner_params.imgui_window_params.default_imgui_window_type = (
        hello_imgui.DefaultImGuiWindowType.provide_full_screen_dock_space
    )
    runner_params.docking_params.docking_splits = create_default_docking_splits()
    runner_params.docking_params.dockable_windows = create_dockable_windows()
    runner_params.docking_params.layout_condition = (
        hello_imgui.DockingLayoutCondition.application_start
    )


if __name__ == "__main__":
    main()

simplegui.py

##- simplegui.py
from imgui_bundle import imgui, immapp, hello_imgui

class SimpleGui:
    def __init__(self):
        imgui.create_context()

    def simp_gui(self):
        imgui.begin("App")
        imgui.text("Neanderthal simple gui")
        if imgui.button("Throg punch here"):
            print("Ouch")
        if imgui.tree_node("Drop Rock"):
            imgui.text("Ouch")
            imgui.tree_pop()
        imgui.end()

if __name__ == "__main__":
    app = SimpleGui()

    immapp.run(
        gui_function=app.simp_gui,  # The Gui function to run
        window_title="Hello!",  # the window title
        window_size_auto=False,  # Auto size the application window given its widgets
        # Uncomment the next line to restore window position and size from previous run
        # window_restore_previous_geometry==True
    )

内容的提问来源于stack exchange,提问作者Ingo McLean

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 19:20:11