How to execute the cargo test concurrently?
What I want is that the files in the
tests directory can be executed concurrently, that is,
b_test.rs run at the same time.
The jobs and threads configurations don't seem to be what I want.
cargo test -- --test-threads=5
cargo test --jobs 50
Cargo test already runs concurrently. What you meant is to run them in parallel.
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:
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;"
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:
main.rs // containing "mod a_test; mod b_test;"
Cargo-nextest is a very good choice. Will it eventually merge into the cargo and replace the original test?
This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.