Skip to content

Commit 5ada328

Browse files
Rollup merge of rust-lang#38823 - Freyskeyd:doc-missingInformationCfgTest, r=steveklabnik
Improve doc cfg(test) and tests directory Hi, I was facing a problem with my code organisation. I was using a tests directory and i defined some `#[cfg(test)]` in my `src/`. But i was not able to use it in my `tests` folder. ```bash . ├── Cargo.lock ├── Cargo.toml ├── src │   ├── lib.rs │   └── test.rs └── tests └── x.rs ``` > src/lib.rs ```rust pub mod test; fn tesst() { assert!(test::t()); } ``` > src/test.rs ```rust pub fn t() -> bool { true } ``` > test/x.rs ```rust extern crate testt; use testt::test; fn tesst() { assert!(test::t()); } ``` I was unable to compile using `cargo test`: ```bash error[E0432]: unresolved import `testt::test` --> tests/x.rs:3:5 | 3 | use testt::test; | ^^^^^^^^^^^ no `test` in `testt` ``` If i remove the `tests` directory everything works fine. To use an utils module in your `tests` directory, you need to create a module in the directory (like `tests/utils.rs`). My `tests/x.rs` look like this now: ```rust extern crate testt; mod utils; fn tesst() { assert!(utils::t()); } ``` And my tree: ```bash . ├── Cargo.lock ├── Cargo.toml ├── src │   └── lib.rs └── tests ├── utils.rs └── x.rs ``` I think that thing must be documented in the book. Ping: - @badboy : Because he's the one who showed me the path - @shahn : Because he helped me too to find the solution Signed-off-by: Freyskeyd <[email protected]>
2 parents a47a6ea + b996a7e commit 5ada328

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

src/doc/book/testing.md

+4
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,10 @@ be imported in every test with `mod common;`
499499
That's all there is to the `tests` directory. The `tests` module isn't needed
500500
here, since the whole thing is focused on tests.
501501

502+
Note, when building integration tests, cargo will not pass the `test` attribute
503+
to the compiler. It means that all parts in `cfg(test)` won't be included in
504+
the build used in your integration tests.
505+
502506
Let's finally check out that third section: documentation tests.
503507

504508
# Documentation tests

0 commit comments

Comments
 (0)