Output from test

Does a println! statement not work from a test? Seems not.

    #[test]
    fn test_this() {
        println!("TESTING THIS");
    }
running 1 test
test tests::test_this ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
1 Like

By default, output from passing tests is not shown. Use cargo test -- --nocapture to override this behavior.

3 Likes

Thank you.

This was a very noob question. I had a hunt for documentation first, but could not find ant thorough documentation for writing tests (and libraries) from the POV of doing it for the first time.

I view that as my failure of searching! Next step: Ask. Where is this documented for the early part of the learning curve?

The book has a chapter on testing. It is pretty long, but it is covered in this subsection. Their suggestion is to use

cargo test -- --show-output

The difference being, --show-output will still capture the output, but it will show it after the testing and not as the tests run (in parallel). So it's nicer to read, but --nocapture may be more appropriate for tests with large amounts of output.


Side note, I didn't happen to know about --show-output myself, and it took a surprising amount of time for me to track it down. It's in the rustc docs, but not in the cargo docs.

7 Likes

Pedantically:
cargo test -- --show-output

1 Like

Whoops, thanks. Corrected.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.