Unable to get doc tests to run

Hi,

I've created a simple function with a doc test, but when I run cargo test I get the following output without any tests being run:

Finished dev [unoptimized + debuginfo] target(s) in 1.42s
     Running target\debug\deps\tool-a3915bdb06e437c0.exe

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

Here is what my doc looks like on the function:

/// Finds any files that are not in the second hashmap that are in the first.
///
/// # Arguments
///
/// * `hashemap1` - The first hashmap to compare against a second. The keys
/// of this hashmap will be compared to the keys of the second hashmap.  Any
/// keys from this hashmap not in the second will be returns in a vector.
///
/// * `hashmap2` - The second hashmap to compare against the first.
///
/// # Examples
///
/// ```
/// let hashmap1: HashMap<String, String> = HashMap::new();
/// hashmap1.insert("one", "one");
/// hashmap1.insert("two", "two");
///
/// let hashmap2: HashMap<String, String> = HashMap::new();
/// hashmap2.insert("two", "two");
/// hashmap2.insert("three", "three");
///
/// let entries_missing = find_missing_entries(&hashmap1, &hashmap2);
/// assert_eq!(entries_missing, vec!["one"]);
/// ```
fn find_missing_entries...

IKs it not running because I'm on Windows, or perhaps because I'm not compiling a lib? I'm not sure how to get this to run as a test. I don't see anywhere in the docs that lists requirements for this to work.

Hi,
I don't why you add it into doc. But your test can be something like:

use std::collections::HashMap;

fn main() {
    println!("Hello, world!");
}

pub fn find_missing_entries (h1 : &HashMap<String, String>,h2:&HashMap<String, String>) -> Vec<String> {
    // Do job
    vec!["one".to_string()]
}

#[cfg(test)]
mod tests {
    use std::collections::HashMap;
    use crate::find_missing_entries;


    #[test]
    fn it_works() {
        let mut hashmap1: HashMap<String, String> = HashMap::new();
        hashmap1.insert("one".to_string(), "one".to_string());
        hashmap1.insert("two".to_string(), "two".to_string());

        let mut hashmap2: HashMap<String, String> = HashMap::new();
        hashmap2.insert("two".to_string(), "two".to_string());
        hashmap2.insert("three".to_string(), "three".to_string());

        let entries_missing = find_missing_entries(&hashmap1, &hashmap2);
        assert_eq!(entries_missing, vec!["one".to_string()])
    }
}

cargo test

Finished dev [unoptimized + debuginfo] target(s) in 0.42s
     Running target/debug/deps/forum-e2535132f268635f

running 1 test
test tests::it_works ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out

I notice your function is not pub. Doctests only work on items that would be documented by rustdoc. Also, they run as though they were external programs using your crate, so they can't get to non public items.

You might consider factoring your code into a lib crate and a binary, and writing doctests for the lib.

2 Likes