How to separate tests into two groups: unit and integration

I have an application with main.rs and I have a bunch of folders where I wrote parsers with unit-tests. But now I have several integration tests for exec part and I'd like to avoid to run them with cargo test command.

What I've tried:

  • Move integration tests outside of src/ folder into tests/ one but to make it run I needed to create lib.rs with all pub mod imports.
  • I tried features flags like cargo test --features exec-tests but it doesn't work and still run all tests
  • One thing that really works is that I mark integration tests as ignore and then run only ignored ones

I don't have any other ideas how I can separate running unit tests and integration ones.

Have you already read cargo test target docs?


As you've found, what rust considers integration tests, in the tests/ dir, only have access to the public API of your library (and cfg(test) is not set in your main library). This is normally what you want for them, but there's nothing really stopping you from using in-source #[test] functions as integration tests.

Probably the closest to what you're looking for is just filtering by name: cargo test integration will run only tests including integration in their path for example. Unfortunately I don't think there's a way to invert that selection, which might be a problem if you're not very careful.

2 Likes