#Unit and integration tests folder structure

3 messages · Page 1 of 1 (latest)

prisma rain
#

Hi everyone, I have a REST API. Axum and SQLx are the most used crates in the project (ask for more information if interested).

I want to add unit and integration tests. I read in https://doc.rust-lang.org/book/ch11-03-test-organization.html that it is common to have unit tests defined inside the file of the function that they test and have a folder in the src directory named ‘tests’ for integration tests.

Is it also possible to put the unit tests in the src/tests folder like in the example in some way to keep the tests separate from the actual logic they are testing?

I added a screenshot of my desired folder structure in regard to the tests.

haughty viper
# prisma rain Hi everyone, I have a REST API. Axum and SQLx are the most used crates in the pr...

The main difference between "unit" and "integration" tests is that unit tests live inside your crate, which means they can access private APIs (e.g. private functions you want to test), whereas integration tests take a dependency on your crate, and therefore can only access the public API. Which one you use to test what is pretty much up to you, but if you need access to internals, you have to put the tests next to the thing they're testing. That's just how Rust's visibility/privacy rules work.

ocean sleet
#

If you want to test in a separate create, you could gate your test functions/methods behind a feature, such as "testing" and then enable that feature in the testing crate