poolie
poolie

Reputation: 9526

How to run external blackbox tests from Cargo?

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.)

Upvotes: 4

Views: 754

Answers (1)

Zarathustra30
Zarathustra30

Reputation: 56

For a quick-and-dirty tests, you can run external executables by way of a shell command with std::process::Command. Simply stick it into the tests directory, as so:

#[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
}

For anything more complicated than that, you will have to use a FFI specific to the external language.

Upvotes: 2

Related Questions