#command #cli-command #command-line-utilities #cli #test

bin+lib test-exec

舒适地测试您的命令行应用程序

1 个不稳定版本

0.1.0 2019年5月6日

#684 in 测试

MIT 许可证

19KB
266 行代码(不含注释)

test-exec - 舒适地测试命令行应用程序

travis-badge appveyor-badge crates.io-badge license-badge

Cargo.toml

[dev-dependencies]
test-exec = "0.1.0

test-exec 是一个 Rust 测试库,用于帮助您测试命令行应用程序的输出。它旨在提供最大程度的舒适体验,并希望避免与 Command 交互。

主要功能是 exec 宏:它执行您的命令,验证输出,并且高度可定制。

一些预览,假设您有一个名为 my_bin 的二进制目标

  • 最小配置:exec!("my_bin");

  • (几乎)最大配置

let output = exec!{
    "my_bin",
    args: ["-p", "/"],
    cwd: "/tmp",
    env: {
        THREADS: "4"
    },
    stdin: b"show-hidden",
    timeout: 60000,
    log: true,

    code: 0,
    stdout: b"Started program...\nDone.\n",
    stderr: []
};

// `output` can be used here like a normal process::Output

如果程序以除 0 之外的其他代码退出,或者 stdoutstderr 不同,或者运行时间超过 60 秒,将发生 panic。

如您所注意到的,bin 目标会自动添加到 PATH 中。

有关更多信息,请参阅 文档

功能

  • 每行设置参数、当前工作目录、环境和 stdin
  • 退出代码、stdoutstderr 以及可选的终止信号比较,可直接通过宏进行
  • bin 目标自动可用
  • 程序的所有输出都返回以供进一步使用

安装和用法

由于 test-exec 是一个测试库,因此应该将其添加到 dev-dependencies 中

[dev-dependencies]
test-exec = "0.1.0

并且可以通过以下方式在代码中使用它

#[macro_use]
extern crate test_exec;

例如,在一个名为 my_pwd 的二进制集成测试中,该二进制打印当前工作目录

tests/bin.rs

#[macro_use]
extern crate test_exec;

#[test]
fn test_program_output() {
    exec!{
        "my_pwd",
        cwd: "/",
        log: true,
        
        code: 0,
        stdout: b"/\n",
        stderr: []
    };
}

依赖关系

~59KB