Skip to content

Testing

rafageist edited this page Dec 2, 2026 · 3 revisions

Testing

TurboFrame gives your tests two layers: Pilot tests in your test process, and tests in a real terminal.

Use it for testing your application headless (Pilot) and in a real terminal
Import from turboframe.testing import dialog_ready, wait_for; pytest_plugins = ["turboframe.testing"]; ["turboframe.testing.terminal"]
Needs pytest and pytest-asyncio; the terminal extra for the real-terminal plugin
Extend the fixtures in your conftest.py; turboframe_acceptance_prefix

With Textual's Pilot

app.run_test() runs your application headless, and its Pilot presses keys and clicks. turboframe.testing adds what such tests keep needing:

from turboframe.testing import dialog_ready, wait_for


async def test_find_remembers(isolated_config):
    app = MyApp()
    async with app.run_test(size=(100, 30)) as pilot:
        await pilot.press("ctrl+f")
        await wait_for(pilot, lambda: dialog_ready(app), what="the Find dialog")
        await pilot.press(*"total", "enter")
        await wait_for(pilot, lambda: "found" in app.status_line._hint_text, what="the match")
  • wait_for(pilot, predicate, timeout=10.0, what="") lets the application run until the predicate holds, and fails after timeout seconds, saying what it waited for. The timeout is a bound, not a pause: never sleep a fixed time in a test.
  • dialog_ready(app) says a dialog is up and takes keys. Keys pressed before its focused control is mounted are lost on a slow machine.
  • isolated_config, a fixture, points the user's configuration and cache folders (XDG_CONFIG_HOME, XDG_CACHE_HOME, and APPDATA, LOCALAPPDATA on Windows) at temporary ones, so a test never reads or writes the user's settings. Load it in your conftest.py:
pytest_plugins = ["turboframe.testing"]

With pytest-asyncio in auto mode, async def tests need no decorator. TurboFrame's own pyproject.toml shows the settings.

To give a component a store without touching any file, use turboframe.ui.history.MemoryStore (see Settings and Recent Files). Your catalogues have checks of their own, in turboframe.testing.i18n (see Translating Your Application), and check_glyphs keeps your strings to one cell each (see Glyphs).

In a real terminal

Pilot cannot send what a terminal sends, such as Alt+F as Esc and f, and cannot see what a terminal shows, or which processes are left behind when the application exits. turboframe.testing.terminal is a pytest plugin for that, built on Microsoft's tui-test. It needs the terminal extra (see Installation).

acceptance/conftest.py:

pytest_plugins = ["turboframe.testing.terminal"]

pyproject.toml:

[tool.pytest.ini_options]
turboframe_acceptance_prefix = "MYAPP"   # its variables: MYAPP_ACCEPTANCE_RUN, MYAPP_ACCEPTANCE_ARTIFACTS...

A test:

import sys

from turboframe.testing import terminal as term


async def test_exit_leaves_nothing(box, evidence, checkpoint, no_process_left, terminal):
    await term.start(terminal, [sys.executable, "-m", "myapp"], box)
    await terminal.get_by_text("Alt+X Exit").expect()
    await term.alt(terminal, "f")                      # Alt+F as a terminal sends it
    await terminal.get_by_text("Open...").expect()
    await checkpoint.terminal(terminal, "file-menu", "Alt+F opened the File menu")
    await term.alt(terminal, "x")
    await terminal.wait_exit(timeout=15_000)
    assert await term.exit_code(terminal) == 0
Fixture
evidence a folder for what explains a failure: the screen, the terminal's trace, the processes left. It is copied to $MYAPP_ACCEPTANCE_ARTIFACTS/<test>/ when the test fails.
checkpoint the scenario's screens at its meaningful moments, as SVG and text, kept in $MYAPP_ACCEPTANCE_CHECKPOINTS/<test>/
box a Sandbox: a project folder and settings for this test only, and a mark (run_id) that every process the test starts inherits
no_process_left fails the test when a process with its mark outlives it, describes it in the evidence and kills it. Ask for it before terminal.
terminal the tui-test terminal

need(ok, reason) skips a test whose tool is missing on this machine, and fails it in CI (MYAPP_ACCEPTANCE_STRICT=1), so a missing tool never passes as a skipped test.

TurboFrame's own acceptance/terminal tests use exactly this, on Linux and Windows in CI.

API: turboframe.testing, turboframe.testing.terminal.

Clone this wiki locally