Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

84%
+9 −0
Q&A What is the purpose of grouping the tests in a `tests` module and is it possible to split them?

Grouping related items into modules is of course generally good practice, but it serves a practical purpose as well. The important part here is the #[cfg(test)] annotation. The #[cfg(test)] anno...

posted 2y ago by Moshi‭  ·  edited 1y ago by Moshi‭

Answer
#2: Post edited by user avatar Moshi‭ · 2023-08-06T02:10:23Z (about 1 year ago)
  • The important part here is the [`#[cfg(test)]` annotation](https://doc.rust-lang.org/book/ch11-03-test-organization.html#the-tests-module-and-cfgtest).
  • > The `#[cfg(test)]` annotation on the tests module tells Rust to compile and run the test code only when you run `cargo test`, not when you run `cargo build`. This saves compile time when you only want to build the library and saves space in the resulting compiled artifact because the tests are not included. You’ll see that because integration tests go in a different directory, they don’t need the `#[cfg(test)]` annotation. However, because unit tests go in the same files as the code, you’ll use `#[cfg(test)]` to specify that they shouldn’t be included in the compiled result.
  • As it says, it is just a way to separate tests from the library itself; the other way is to use a separate directory.
  • Creating tests outside of such a module actually does work, though from the wording I would assume doing so would have the test end up in the resulting binary, which may be undesirable.
  • Grouping related items into modules is of course generally good practice, but it serves a practical purpose as well. The important part here is the [`#[cfg(test)]` annotation](https://doc.rust-lang.org/book/ch11-03-test-organization.html#the-tests-module-and-cfgtest).
  • > The `#[cfg(test)]` annotation on the tests module tells Rust to compile and run the test code only when you run `cargo test`, not when you run `cargo build`. This saves compile time when you only want to build the library and saves space in the resulting compiled artifact because the tests are not included. You’ll see that because integration tests go in a different directory, they don’t need the `#[cfg(test)]` annotation. However, because unit tests go in the same files as the code, you’ll use `#[cfg(test)]` to specify that they shouldn’t be included in the compiled result.
  • As it says, it is just a way to separate tests from the library itself; the other way is to use a separate directory.
  • Creating tests outside of such a module actually does work, and as [cafce25‭'s answer](https://software.codidact.com/posts/287585/288279#answer-288279) mentions, `#[test]` already implies `#[cfg(test)]`. However, you might also have helper functions or mocks for your tests that you also don't want to include in your final non-test binary. By putting them all into one module, you not only group them semantically, but this also allows you to `#[cfg(test)]` all of them in one place rather than adding it to each of them individually.
#1: Initial revision by user avatar Moshi‭ · 2022-12-18T23:55:39Z (almost 2 years ago)
The important part here is the [`#[cfg(test)]` annotation](https://doc.rust-lang.org/book/ch11-03-test-organization.html#the-tests-module-and-cfgtest).

> The `#[cfg(test)]` annotation on the tests module tells Rust to compile and run the test code only when you run `cargo test`, not when you run `cargo build`. This saves compile time when you only want to build the library and saves space in the resulting compiled artifact because the tests are not included. You’ll see that because integration tests go in a different directory, they don’t need the `#[cfg(test)]` annotation. However, because unit tests go in the same files as the code, you’ll use `#[cfg(test)]` to specify that they shouldn’t be included in the compiled result.

As it says, it is just a way to separate tests from the library itself; the other way is to use a separate directory.

Creating tests outside of such a module actually does work, though from the wording I would assume doing so would have the test end up in the resulting binary, which may be undesirable.