Skip to content

cfg(test) is not set during doctests #45599

Closed
@sunjay

Description

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 and cfg!(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)

#[test]
fn cfg_test() {
    // This will pass, as it should
    assert!(cfg!(test))
}

/// ```rust
/// # fn main() {
/// // This will fail, even though we *are* in a test
/// assert!(cfg!(test), "cfg!(test) was false");
/// # }
/// ```
pub fn cfg_test2() {}

/// ```rust
/// # fn main() {
/// #[cfg(test)]
/// fn foo() {}
/// #[cfg(not(test))]
/// fn foo() {panic!("cfg(test) was not set")}
/// foo();
/// # }
/// ```
pub fn cfg_test3() {}

This produces the following output:

$ cargo test
    Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs
     Running target/debug/deps/foo-3890a4e1c4133892

running 1 test
test cfg_test ... ok

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

   Doc-tests foo

running 2 tests
test src/lib.rs - cfg_test3 (line 15) ... FAILED
test src/lib.rs - cfg_test2 (line 7) ... FAILED

failures:

---- src/lib.rs - cfg_test3 (line 15) stdout ----
	thread 'rustc' panicked at 'test executable failed:

thread 'main' panicked at 'cfg(test) was not set', src/lib.rs:6:10
note: Run with `RUST_BACKTRACE=1` for a backtrace.

', /checkout/src/librustdoc/test.rs:315:16
note: Run with `RUST_BACKTRACE=1` for a backtrace.

---- src/lib.rs - cfg_test2 (line 7) stdout ----
	thread 'rustc' panicked at 'test executable failed:

thread 'main' panicked at 'cfg!(test) was false', src/lib.rs:3:0
note: Run with `RUST_BACKTRACE=1` for a backtrace.

', /checkout/src/librustdoc/test.rs:315:16


failures:
    src/lib.rs - cfg_test2 (line 7)
    src/lib.rs - cfg_test3 (line 15)

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

error: test failed, to rerun pass '--doc'

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:

[features]
# The reason we do this is because doctests don't get cfg(test)
# See: https://github.com/rust-lang/cargo/issues/4669
test = []

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    A-doctestsArea: Documentation tests, run by rustdocC-bugCategory: This is a bug.T-rustdocRelevant to the rustdoc team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions