Why is doctests so slow compared to unit tests and integration tests?

When I write only unit tests (functions in the main crate with #[test] attribute) and integration tests (*.rs files in the /tests folder), a re-run of cargo test finishes under 1 second, no matter how many small tests I wrote. Doctests (Rust code snippets inside documentation), OTOH, is a different story, it seems that Rust re-compile and re-run every single code snippet sequentially.

My questions are:

  • Is my hypothesis true? That doctests re-compile and re-run sequentially?
  • Why is it this way? What makes doctests so difficult to optimize?
  • How to improve the speed of doctesting?
1 Like

I'm sure it's not sequential, but each doctest is compiled to it's own binary to be ran. Here's an issue about the topic; it's short and worth a read to answer your questions.

I can't be bothered to pull up a source, but they are compiled (and I believe run) in parallel. @quinedot is correct that they are compiled individually.