Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cargo test does not run documentation tests on binary projects #2009

Closed
bltavares opened this issue Sep 29, 2015 · 8 comments
Closed

cargo test does not run documentation tests on binary projects #2009

bltavares opened this issue Sep 29, 2015 · 8 comments
Labels
A-documenting-cargo-itself Area: Cargo's documentation

Comments

@bltavares
Copy link

Hi there folks,

I was preparing an introduction to cargo to share with some coworkers and I wanted to show how the documentation testing works.

So I created a binary project to show a 'Hello world' and added a documentation code that should fail to exemplify the documentation testing, but it didn't run with cargo test.

Here are examples to reproduce:

cargo new --bin binary
cd binary
echo '///```
///assert_eq!(1, 2);
///```
fn main() {
    println!("Hello, world!");
}
' > src/main.rs
cargo test
# Expected: 1 doc-test failed, found 0 failures
cargo new library
cd library
echo '///```
///assert_eq!(1, 2);
///```
fn lib() {
    println!("Hello, world!");
}
' > src/lib.rs
cargo test
# Expected: 1 doc-test failed, found 1 failure

Thanks and keep the good work folks! (:

@bltavares bltavares changed the title cargo test does not run documentation on binary projects cargo test does not run documentation test on binary projects Sep 29, 2015
@bltavares bltavares changed the title cargo test does not run documentation test on binary projects cargo test does not run documentation tests on binary projects Sep 29, 2015
@steveklabnik
Copy link
Member

This is actually a rustdoc 'bug', though I can't seem to find it. Basically, Rustdoc, in its current form, needs to link to your library, and so this is just an inherent limitation of it right now.

/cc @alexcrichton do you remember where that bug is?

@alexcrichton
Copy link
Member

Yeah @steveklabnik hit this right on the money, in the case of a binary there's no library to link against (because it's a binary), so this is an inherent limitation of rustdoc itself. If you end up writing lots of functions and documenting heavily in a binary, moving them to a library in the same repo should do the trick!

@bltavares
Copy link
Author

Thanks for the explanation folks. Would it make sense to add this information on the documentation about documentation? I think it would be interesting to explain why there is such limitation currently and suggest to move the logic into a library on the same like @alexcrichton said.

I could try to do that later if it makes sense.

Cheers.

@alexcrichton
Copy link
Member

Oh dear, this should definitely be in the docs! I'll leave this open to ensure a mention can be added there.

@alexcrichton alexcrichton reopened this Sep 29, 2015
@alexcrichton alexcrichton added the A-documenting-cargo-itself Area: Cargo's documentation label Sep 29, 2015
@steveklabnik
Copy link
Member

steveklabnik commented Sep 29, 2015 via email

@bltavares
Copy link
Author

@steveklabnik: now that you mentioned, I found it already documented on Rust. I was about to send a PR on documentation Rust.

I think this could be closed. Sorry about that.

@steveklabnik
Copy link
Member

steveklabnik commented Sep 29, 2015 via email

@Rudxain
Copy link

Rudxain commented Oct 24, 2022

Is this related to modules? I have doc-tests in a module file, but rust-doc can't find them.
Terminal:

cargo test
   Compiling xorsum v4.0.0 (/home/rudxain/Documents/GitHub/xorsum)
    Finished test [unoptimized + debuginfo] target(s) in 1.08s
     Running unittests src/lib.rs (target/debug/deps/xorsum-be73721f08e2bcae)

running 0 tests

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

     Running unittests src/main.rs (target/debug/deps/xorsum-46e9ecbb076492d7)

running 2 tests
test module::tests::hex_cmp ... ok
test module::test_hasher ... ok

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

   Doc-tests xorsum

running 0 tests

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

The weird thing is that rust-analyzer detects doc-tests, and shows a "Run Doctest" button in VSCode, but when I press it, it says "0 tests run"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-documenting-cargo-itself Area: Cargo's documentation
Projects
None yet
Development

No branches or pull requests

4 participants