I'm writing a Rust library and I want to provide examples in my documentation that
cargo test
Is this possible?
I'm writing a database client library, and the examples make use of a hypothetical, non-existing database server. As such, the examples always fail when run, but it's important that the examples be valid syntactically. Hence my requirements above.
If there's no way to do what I want, then how does one opt out of having cargo test
run a specific doc test? I.e., have cargo run
compile-and-run some doc tests but completely ignore some others?
Right click on a blank space in the python code, and there is a menu option to run all the Doctests found in the file, not just the tests for one function.
There are several common ways to use doctest: To check that a module's docstrings are up-to-date by verifying that all interactive examples still work as documented. To perform regression testing by verifying that interactive examples from a test file or a test object work as expected.
To run the tests, use doctest as the main program via the -m option to the interpreter. Usually no output is produced while the tests are running, so the example below includes the -v option to make the output more verbose.
doctest is a module included in the Python programming language's standard library that allows the easy generation of tests based on output from the standard Python interpreter shell, cut and pasted into docstrings.
This is documented in The rustdoc book, specifically the chapter about attributes.
Your opening codeblock delimiter should look like:
/// ```no_run
From the book:
/// ```no_run /// loop { /// println!("Hello, world"); /// } /// ```
The
no_run
attribute will compile your code, but not run it. This is important for examples such as "Here's how to retrieve a web page," which you would want to ensure compiles, but might be run in a test environment that has no network access.
To omit build completely use ignore
instead of no_run
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With