Output assertion
The output assertion checks for stdout
and stderr
output from specific pieces of code:
assert_outputs!(
|| {
println!("hello, world");
},
on_stdout = |stdout| {
assert_eq!(stdout, "hello, world\n");
}
);
You can use on_stdout = <closure>
, on_stderr = <closure>
, or both to check the output streams.
Warning
To use this assertion you need to configure Cargo as described below.
Avoiding Cargo issues
To use this assertion, you must create or modify the .cargo/config.toml
file. Add this to it:
[env]
RUST_TEST_NOCAPTURE = "1"
RUST_TEST_THREADS = "1"
By default, cargo test
will capture output and also run tests in parallel. These both cause issues for assert_outputs!(...)
.
-
Cargo's output capturing means that
stdout
andstderr
will be empty when the code actually runs. This means thatassert_outputs!(...)
becomes useless. -
Although the assertion is thread safe, it cannot stop other threads from using
stdout
andstderr
. This means thatassert_outputs!(...)
becomes unreliable.