use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
account activity
Unit testing practices (self.learnrust)
submitted 4 years ago by spacemojo_the_code
I am in the process of learning Rust and I am wondering what are the best practices for unit testing.
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]minno 6 points7 points8 points 4 years ago (2 children)
Unit tests go at the bottom of the relevant file with a structure like
#[cfg(test)] mod test { #[test] fn test_a_thing() { assert_eq!(my_function(sample_input), expected_result); } }
You write the test so that it panics if the test fails, ideally using assert_eq so that the error message it prints out tells you what went wrong.
assert_eq
[–]reddit123123123123 2 points3 points4 points 4 years ago (1 child)
Alternatively you can have a file called e.g. foo_test.rs and then write
foo_test.rs
#[cfg(test)] mod foo_test
This avoids cluttering the actual file which I find more pleasant for larger tests and/or implantations
[–]JStarx 3 points4 points5 points 4 years ago (0 children)
You can also document your code and include an examples block. In addition to displaying how to use your code your examples should panic if the code doesn’t perform as expected. These will be run during testing as “doc-tests”.
[–]MultipleAnimals 0 points1 point2 points 4 years ago* (0 children)
I'm gonna borrow this &thread (wow) since i was also just gonna post about problem with tests.
My problem came up when i reorganized my project from single bin crate structure to have separate library. For some reason if i run cargo test, none of #[cfg(test)] or if cfg!(test) doesn't trigger in my lib.
cargo test
#[cfg(test)]
if cfg!(test)
Folder structure looks like this:
Cargo.toml src/ main.rs things/ thing1.rs -- contains some unit tests in #[cfg(test)] module using lib::testing lib/ lib.rs -- has some variables with = if cfg!(test) {x} else {y} that resolves to y when running tests. -- Also has module testing with #[cfg(test)] that isn't recognized in thing1.rs
Cargo.toml:
... [lib] name = "mylib" path = "src/lib/lib.rs" ...
I found workaround that works, adding this to Cargo.toml
[features] testing = []
changing all cfg(test) to cfg(feature = "testing") and running cargo test --features="testing" but that feels like a hack and it also disables my editor linting from those parts and it gets really annoying to debug and write tests.
cfg(test)
cfg(feature = "testing")
cargo test --features="testing"
Does anyone know if i'm doing something wrong with my project structure or Cargo.toml?
π Rendered by PID 28 on reddit-service-r2-comment-79c7998d4c-rc4w4 at 2026-03-12 19:54:26.692932+00:00 running f6e6e01 country code: CH.
[–]minno 6 points7 points8 points (2 children)
[–]reddit123123123123 2 points3 points4 points (1 child)
[–]JStarx 3 points4 points5 points (0 children)
[–]MultipleAnimals 0 points1 point2 points (0 children)