Documention tests does not work


#1

According to the Docs something like this should panic because of assertion failed
/// Do somework /// # Example /// /// assert_eq!(9, 21); /// pub foo() { ... }

but nothing happen and in cargo test output i get thing message running 0 tests why is that ?


#2

Are you sure the test is marked using triple backticks as shown in the docs? Rustc has to be able to distinguish code from prose.


#3

Yes it’s but i just can’t write it here


#4

Is this for a library crate or a binary crate? Cargo will only run doctests for library crates. (If you are developing an executable, you might want to split testable code into a separate library.)


#5

What do you mean? It should look like this:

/// Do somework
///
/// # Example
///
/// ```
/// assert_eq!(9, 21);
/// ```
pub foo() {
...
}

This is demonstrated in the documentation tests section of your link: https://doc.rust-lang.org/book/testing.html#documentation-tests


#6

it look like this i just can’t write the mark above assert_eq! here


#7

yes it’s a library crate


#8

What does this mean? Why can’t you? What error do you get? Can you share what you’re writing?


#9

I might be wrong, but I my guess is that @hackereg35 complains that Discourse eats triple quote marks (```), while they are present in code.

Another possible explanation for the original issue is that the module is not properly connected to the crate. That is, there is foo.rs, but no corresponding mod foo; in lib.rs. Are usual tests function (#[test] fn foo() {}) work in this file?