Rust 中 CLI 和 TUI 应用的无头端端测试:真实的 PTY、渲染屏幕断言、快照和屏幕差异。
概览
termlens 会在真实的伪终端中生成你的真实二进制,通过 VT 模拟器将其输出渲染成内存中的屏幕网格,并让测试等待、对该网格进行断言和快照——用户看到的不是产生该网格的字节。 Ratatui 的快照测试方案包含一个 termlens 示例,用于通过 PTY 测试已编译的应用程序。与 TestBackend 快照一起使用,以涵盖交互和进程行为。 进程中的控件测试会将你的绘图函数渲染成缓冲区。它快速且精准,且无法看到绘制与用户眼睛之间的任何东西。termlens 是第二层,用于少数通过真实二进制的端到端流: - — 原始模式和切换界面进出,光标在Q或紧急情况下恢复,应用程序开启的模式以及是否关闭。- — 框绘图、颜色、宽字符、隐藏的密码字段而非透明打印,以及一个零散的println!
概述(AI 翻译)
终端程序的集成测试,就像测试网页一样 app. termlens 在真实的伪终端中生成你的真实二进制,渲染 其通过VT仿真器输出到内存中的屏幕网格,并且允许 测试、等待、断言并快照网格——用户会做的 看,不是生成它的字节。
Ratatui的快照测试配方
包含一个术语lens示例,用于通过PTY测试编译后的应用程序。
与TestBackend快照一起使用,以覆盖交互和处理过程
行为。
为什么
过程中的控件测试会把你的draw函数渲染成缓冲区。它确实如此
快速且精准,且无法看到draw与用户之间的任何东西
眼睛。项透镜是第二层,适用于少量端到端流动
通过真实二元对立:
- 终端状态 — 原始模式,切换到替代屏幕,进入和离开,
光标在
q或恐慌后恢复,应用程序切换的模式 以及是否关闭了它们。 - 实际绘制的内容 — 方框绘画、色彩、宽字符、a 密码字段被隐藏,而不是以透明打印,是个乱七八糟的
本页由 AI 翻译,技术细节请以原文为准。
README
以下为原文 README 节选,完整内容请查看原文。
termlens
Integration testing for terminal programs, the way you would test a web app. termlens spawns your real binary in a real pseudo-terminal, renders its output through a VT emulator into an in-memory screen grid, and lets a test wait on, assert against and snapshot that grid — what a user would see, not the bytes that produced it.
Ratatui’s snapshot-testing recipe
includes a termlens example for testing a compiled application through a PTY.
Use it alongside TestBackend snapshots to cover interactions and process
behaviour.
Why
An in-process widget test renders your draw function into a buffer. It is
fast and precise, and it cannot see anything between draw and the user’s
eyes. termlens is the second layer, for a small number of end-to-end flows
through the real binary:
- Terminal state — raw mode and the alternate screen entered and left,
the cursor restored after
qor a panic, the modes the application turned on and whether it turned them off. - What is actually drawn — box drawing, colours, wide characters, a
password field that is concealed rather than printed in clear, a stray
println!or panic message that never went through the framework. - Real input and real signals — keys, mouse and paste encoded exactly as
the terminal encodes them under the modes the application enabled; a
SIGWINCHthat the application must survive. - Behaviour that changes no cell — a repaint that drew nothing, a bell on a rejected key, an image that went out as pixels when it should have been text.
Every wait is deadline-bounded, and every failure carries the screen: a CI
log shows what the application was displaying, not assertion failed: false.
Quick start
cargo add termlens --dev
cargo add insta --dev # used by the snapshot assertions below
use std::time::Duration;
use termlens::{Key, Terminal};
#[test]
fn quits_from_the_main_screen() -> termlens::Result {
let mut t = Terminal::builder()
.size(80, 24)
.env_clear() // hermetic: no host env leaks in
.timeout(Duration::from_secs(5)) // every wait_* has this deadline
.spawn(env!("CARGO_BIN_EXE_myapp"))?;
// Wait for the app's ready marker, let the picture settle, then snapshot
// it with its styles — the three decisions every TUI snapshot needs.
termlens::assert_screen_snapshot!(t, after = |s| s.contains("Ready"));
推荐工具
换一个关键词,或者移除筛选条件。
安装
npx skillfish add vyncint/termlens