How to execute the cargo test concurrently?
What I want is that the files in the tests directory can be executed concurrently, that is, a_test.rs and b_test.rs run at the same time.
tests
a_test.rs
b_test.rs
The jobs and threads configurations don't seem to be what I want.
cargo test -- --test-threads=5
cargo test --jobs 50
The cargo test execution model: each test binary is run serially, and binaries are responsible for running individual tests in parallel.
The nextest model: in the run phase, cargo-nextest then executes each individual test in a separate process, in parallel. It then collects, displays and aggregates results for each individual test.
The way Cargo works is that test targets (these tests in the tests/ directory, also known as “integration tests”) are compiled to separate binaries, and run serially.
In order to get parallel execution of tests (without using nextest), you need to put the tests in the same binary. If you don't need the test-target functionality (e.g. being able to execute binaries from the project from within tests), then you can put your tests in the main target:
src/
lib.rs // or main.rs; in either case, containing "#[cfg(test)] mod tests;"
tests/ // this is NOT the top-level tests directory
mod.rs // containing "mod a_test; mod b_test;"
a_test.rs
b_test.rs
If you do need them to be in a test target, then you can still have multiple test modules within that target by using the multi-file target layout: