You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The conventional answer to this in Rust is to put our unit tests inside a module (`mod tests`, let's say, though the name is up to us), and then protect that module with a `cfg(test)` attribute:
`cfg` turns compilation on or off for the item it's attached to, based on the value of some expression. `cfg(test)` means the `tests` module will be compiled only if we're running in `cargo test` mode. Otherwise it will be ignored entirely, saving time, energy, and the planet.
12
-
13
-
### Anatomy of a test module
14
-
15
-
So here's what our test looks like once we've moved it into its own module:
A module can have its own `use` declarations, which is handy since we often want to `use` things in tests that we don't need in the library itself (`std::io` in this example).
0 commit comments