title | timestamp | author | published | description | tags | todo | |||||
---|---|---|---|---|---|---|---|---|---|---|---|
Show standard output and standard error in tests in Rust |
2023-12-30 10:30:01 -0800 |
szabgab |
true |
Sometimes we would like to include print statements in the tests in Rust. How can we see them? |
|
|
Sometimes our code prints to the screen, either to the standard output channel or the standard error channel. There are cases when we might want to verify the content of what was printed in other case we just would like to see.
By default when we run cargo test
, it will capture and hide both channels. Using the --show-output
we can convince
cargo
to print the captured output.
In this example we'll see this working both inline tests that are usually used for unit-testing and external tests that, in this case, run our code as an external program as we can also see in the article on how to test command line application.
{% include file="examples/show-output-in-tests/src/main.rs" %}
Running the program as a command line application we would probably check that the text printed to the STDOUT and STDERR by the program is what we expect. Still we have some text printed by the test that we would like to see.
{% include file="examples/show-output-in-tests/tests/tests.rs" %}
$ cargo test -q
running 1 test
.
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
running 1 test
.
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.04s
The upper part shows the inline test
$ cargo test -q -- --show-output
running 1 test
.
successes:
---- test_function stdout ----
STDOUT In test_function
STDERR In test_function
STDOUT in code
STDERR in code
successes:
test_function
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
running 1 test
.
successes:
---- test_cli stdout ----
STDOUT In test_cli
STDERR In test_cli
successes:
test_cli
test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.04s
The -q
is an option of cargo
, but --show-outputis a parameter of the test runner. So we need the extra
--` in order to pass this argument to the actual test runner.