tui-test
作者: microsoft
透過 tui-test 命令列介面,從命令列驅動、檢查、斷言、記錄並觀看真實終端機。適用於執行 shell(bash、zsh、fish、PowerShell、…)時使用。
npx skills add https://github.com/microsoft/tui-test --skill tui-testtui-test
Use tui-test to control and test a real terminal.
Pick an API
| Context | Use | Reference |
|---|---|---|
| Agent or shell workflow | CLI | CLI |
| Python code | tui_test | Python |
| JavaScript or TypeScript | @microsoft/tui-test | JavaScript |
| Rust code | tui-test-rs | Rust |
| Common tasks | Current project language | Recipes |
Use the library that matches the project. Use the CLI when the terminal must persist across separate commands or be watched with monitor.
CLI and library sessions do not share state.
CLI
tui-test run my-app
tui-test expect text "Ready"
tui-test click text "Continue"
tui-test expect text "Done"
tui-test close
Run tui-test agent-context for exact command and option names.
Python
from tui_test import TuiTest
async with TuiTest.ephemeral() as terminal:
await terminal.run("my-app")
await terminal.get_by_text("Ready").expect()
await terminal.get_by_text("Continue").click()
JavaScript
import { TuiTest } from "@microsoft/tui-test";
const terminal = TuiTest.ephemeral();
try {
await terminal.run("my-app");
await terminal.getByText("Ready").expect();
await terminal.getByText("Continue").click();
} finally {
await terminal.closeQuiet();
}
Locators
Use text and style locators for visible terminal state.
save = (
terminal
.get_by_text("Settings")
.get_by_text("Save", direction="after")
.unique()
)
await save.click()
- Use
first(),last(),nth(), orunique()before an action when text repeats. - Use
get_by_style()orgetByStyle()for colors and attributes. - Use
direction="within","after", or"before"for relative matches. - Use
locations()orcount()for immediate reads. - Use
wait(),expect(), orclick()when the app may still be changing.
Wait for state
| Need | Use |
|---|---|
| Visible or hidden text | Locator wait() or expect() |
| Submitted command finished | wait command, wait_command(), waitCommand() |
| Program exited | wait exit, wait_exit(), waitExit() |
| Shell prompt ready | wait ready, wait_ready(), waitReady() |
| Screen stopped changing | wait idle, wait_idle(), waitIdle() |
| Clipboard changed or matched | wait clipboard, wait_clipboard(), waitClipboard() |
| Bell fired | wait bell, wait_bell(), waitBell() |
Do not use a fixed sleep when a wait can describe the state.
Keep tests stable
- Give parallel tests unique sessions.
- Use
openfor shell commands andrunfor an app. - Assert the exit code separately from visible text.
- Keep the shell, terminal size, cwd, environment, and timeouts explicit when they affect output.
- Use semantic mouse buttons:
left,middle, orright. - Close every session. Prefer the Python and JavaScript test helpers.
Capture output
Use screenshot for text or SVG. Use record for APNG, GIF, MP4, or asciinema. Use recording mode on-failure for test artifacts.