-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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 #45599
Comments
I may be mistaken here, but I believe doctests are treated as integration tests (i.e., not unit tests), and consider the crate under test external. This is supported by the fact that you must manually add Now, I do believe there is a bug here, and that is that That all said, I have also wanted the ability to have the crate under test behave differently in some way when it is run in "test mode", even when used as an |
Maybe the answer is to have a specific With that, unit tests could remain unit tests, and we could do more specialized behavior in doctests when we need to. Adding an attribute like that isn't a breaking change either so that's great too. Note: if we add this, we should probably have something for tests that aren't unit tests or doctests. The tests in the separate tests directory could probably benefit from their own cfg flag. |
It would be great if a |
+1 for a |
I am facing similar situation for the tests folder. I have code that provide me with helpers to the tests folder. I would like this code to only compile under #[cfg(test)] as it's only meant for test. |
Doctests are integration tests so any solution for this problem should work for integration tests too. I agree that we need a way to enable features that are available (only) to integration tests. |
Agreed. I am writing a crate that manages some devices via libusb. I would prefer if it wasn't necessary for me to connect a device to be able to perform doctests; the same actually also holds for the integration tests. (If somebody known solutions for these kinds of issues and common ways to mitigate them, please let me know in private. Thanks in advance!) |
My problem is quite similar. I maintain https://github.com/asomers/mockall , which is normally only used as a dev-dependency. So some of my examples won't really do anything unless |
cfg(test) is no longer set during doctests See: rust-lang/rust#45599
I would also point out that this causes the most prominent use of the doc-comment crate to seemingly not work for users. Each of the top 5 most popular crates (lazy_static, regex, aho-corasick, byteorder, num_cpus) are all using doc-comment behind a #[cfg(test)] conditional. Meaning that they intend to check their README.md files, but in fact are not. These crates are maintained by some of the best members of the rust community indicating me me that the current behavior is not what even experienced rust engineers expect. I only discovered this issue after trying to use doc_comment to test my README.md file, trying to figure out why it wasn't working. Comparing my code to the examples in those crates above, failing to see what I was doing wrong. Eventually compiling those crates only to discover they also are not testing their README.md files, bisecting older versions of the rust compiler and doc_comment crate to discovery that this seemingly never worked. |
|
Ah actually it turns out I misunderstood what was added. The This does not resolve this issue then. We would need |
Thank you! I agree. I was initially very confused about the behaviour. |
I just would like to point out this thread from official user forum since it's settled that this is an issue affecting integration tests as well. Just in case. |
... at least until rust-lang/rust#45599 is resolved.
The bindings module does not play nicely with cargo test. Fortunatley, it's functionality is tested with pytest, and the module is not required when testing the core module. The test suite can now be ran with: `cargo test --features "test"` Workaround for: PyO3/pyo3#340 which allows for doctests. Source of workaround: rust-lang/rust#45599
Closing in favor of #67295, this is covered by cfg(doctest). If you need doctest to behave the same as test, you can use |
In 'cargo test' environments, the SvsmAllocator must not be made the global_allocator. However, the conditional setting of this attribute depends on the 'test' configuration predicate being set in 'cargo test' environments. This seems to not be working properly for documentation tests ([1]), and it's similar for the alternative 'doctest' predicate ([2]). As documentation tests currently don't play an important role for the project, just disable them alltogether until the cargo community has settled on a suitable alternative. [1] rust-lang/rust#45599 [2] rust-lang/rust#67295 Signed-off-by: Nicolai Stange <[email protected]>
Our custom bare-metal allocator (`SvsmAllocator`) does not work during tests, as they run as regular userspace binaries. Thus, we must not set it as the global Rust allocator. To do this, we need a compile-time directive that lets us know we are building doctests and not the regular binary (`cfg(doctests)`), but sadly it is not properly set during test compilation(see rust-lang/rust#45599, rust-lang/rust#67295 and coconut-svsm#93). In order to bypass this, set the compile time flag ourselves via the RUSTFLAGS environment variable so that tests work. Also add the new invocation to the `test` target in the Makefile. Fixes: coconut-svsm#93 Fixes: 8cdea8e ("Cargo manifest: disable doctests") Signed-off-by: Carlos López <[email protected]>
Our custom bare-metal allocator (`SvsmAllocator`) does not work during tests, as they run as regular userspace binaries. Thus, we must not set it as the global Rust allocator. To do this, we need a compile-time directive that lets us know we are building doctests and not the regular binary (`cfg(doctests)`), but sadly it is not properly set during test compilation(see rust-lang/rust#45599, rust-lang/rust#67295 and coconut-svsm#93). In order to bypass this, set the compile time flag ourselves via the RUSTFLAGS environment variable so that tests work. Also add the new invocation to the `test` target in the Makefile. Fixes: coconut-svsm#93 Fixes: 8cdea8e ("Cargo manifest: disable doctests") Signed-off-by: Carlos López <[email protected]>
Our custom bare-metal allocator (`SvsmAllocator`) does not work during tests, as they run as regular userspace binaries. Thus, we must not set it as the global Rust allocator. To do this, we need a compile-time directive that lets us know we are building doctests and not the regular binary (`cfg(doctests)`), but sadly it is not properly set during test compilation (see rust-lang/rust#45599, rust-lang/rust#67295 and coconut-svsm#93). In order to bypass this, set the compile time flag ourselves via the RUSTFLAGS environment variable so that tests work. Also add the new invocation to the `test` target in the Makefile. Fixes: coconut-svsm#93 Fixes: 8cdea8e ("Cargo manifest: disable doctests") Signed-off-by: Carlos López <[email protected]>
From @sunjay on October 27, 2017 21:23 (Moved from rust-lang/cargo)
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")]
Copied from original issue: rust-lang/cargo#4669
The text was updated successfully, but these errors were encountered: