-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
cfg(test) is not set during doctests #4669
Comments
Thanks for the report! I think that this may be an issue for rust-lang/rust, though, where rustdoc is defined? |
Yes that may be a good idea! I wasn't sure and so I assumed that cargo passes in the config flags. Thanks! |
This issue was moved to rust-lang/rust#45599 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It might be that I'm just not understanding how something works, so please let me know if that is the case!
Since doctests are tests, I expect that
#[cfg(test)]
would work andcfg!(test)
would return true. This works during unit tests, but during doctests, both assumptions are wrong.The reason I need this is because I have some code (not in doctests) that manages windows (like GUI windows) that I need to not run during any tests. I think this is a cargo problem because the attributes work consistently, I believe it's just cargo that doesn't set the attribute. If there is a workaround I can use, I would really appreciate it. :) (EDIT: My workaround is at the bottom.)
Here's some code that reproduces the problem: (Run with
cargo test
)This produces the following output:
In both cases, since
cfg(test)
is not set, the test fails.This is a temporary workaround you can use if you are in need of this functionality:
In Cargo.toml, add the following:
Then run with
cargo test --features "test"
.This adds a feature called "test" to tests when they run. This works for both unit tests and doctests.
Then, to test for it, use the following:
#[cfg(any(feature = "test", test))]
You could also just go with the following, since the feature is set for all kinds of tests.
#[cfg(feature = "test")]
The text was updated successfully, but these errors were encountered: