Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to opt out of running a doc test?

I'm writing a Rust library and I want to provide examples in my documentation that

  1. compile as part of running cargo test
  2. do not run.

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?

like image 443
Craig M. Brandenburg Avatar asked Sep 06 '15 23:09

Craig M. Brandenburg


People also ask

What is the correct way to run all the Doctests?

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.

Can doctest be used to test docstrings?

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.

How do I run a doctest from command line?

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.

What is doctest used for?

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.


1 Answers

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.

like image 70
Jorge Israel Peña Avatar answered Oct 16 '22 13:10

Jorge Israel Peña