How to run #[test] unit tests inside doc tests?

I'm writing a unit test utility, a proc-macro whose transform output is multiple #[test] functions, and I want to test the utility within doc tests.

If I run cargo test when it runs doc tests, it does not invoke the standard unit test runner for those snippets. How can I accomplish this?

Here's a minimal example (which doesn't involve proc-macros like my project):

$ cargo new --lib foo
     Created library `foo` package

$ cd foo/

$ cat > ./src/lib.rs 
/// ```
/// #[test]
/// fn it_fails() {
///   panic!("The test was executed and failed.");
/// }
/// ```
pub fn foo() {}

$ cargo test
   Compiling foo v0.1.0 (/tmp/tempdir-shell.7tWvPXJMkxYD/foo)
    Finished test [unoptimized + debuginfo] target(s) in 0.48s
     Running unittests src/lib.rs (target/debug/deps/foo-c5853500830f24d9)

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

   Doc-tests foo

running 1 test
test src/lib.rs - foo (line 1) ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.16s

I believe it is "running 1 test" which does nothing, since the doc code snippet is not executing the unit test.

This is an integration test which relies only on the public crate (proc-macro) interface, and I don't think cfg(doctest) doesn't work as expected · Issue #67295 · rust-lang/rust · GitHub is relevant here.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.