3

Suppose I have a Rust program with some blackbox tests written in sh or Python (for example). Is there any easy way to get Cargo test to run them?

(I realize this is a bit against the grain of Cargo, since it's likely to introduce untracked dependencies on OS tools. But it'd be really useful, since I have some existing tests I want to reuse.)

4

1 回答 1

2

对于快速而简单的测试,您可以通过带有std::process::Command的 shell 命令运行外部可执行文件。只需将其粘贴到测试目录中,如下所示:

#[test]
fn it_works() {
    use std::process::Command;

    let output = Command::new("python.exe")
        .arg("test.py")
        .output()
        .unwrap_or_else(|e| { panic!("failed to execute process: {}", e) });

    let s = match String::from_utf8(output.stdout) {
        Ok(v) => v,
        Err(e) => panic!("Invalid UTF-8 sequence: {}", e),
    };

    println!("result: {}", s); //must run "cargo test -- --nocapture" to see output
}

对于比这更复杂的事情,您将不得不使用特定于外部语言的 FFI。

于 2015-08-01T09:29:59.390 回答