Testing rust doc

I just learned about testing my code in the doc. But I can't make it work.

I have a lib.rs file and a lot of modules under it in other rust files. When I add a code block with assert_eq! in one of these files, let's call it foo.rs, I run the command cargo rustdoc --test foo.rs --open

However this will give me an error message no test target named 'foo.rs' found.

As I understand the docs this should work and also I should add every test to the command line. The command gets pretty big then.

Is there a way to tell rust to just test everything in all documentation?

cargo test --doc

This runs all doc tests for your crate.
cargo rustdoc can be used if you need more specific invocations, but cargo test should already do the majority of work for you without hassle.

1 Like

Also note that a plain cargo test (without any other options) will run the rustdoc tests, along with any unit tests and integration tests in your project. And it will compile (but not run) any programs it finds in the examples folder.

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.