How to execute the cargo test concurrently?

How to execute the cargo test concurrently?
What I want is that the files in the tests directory can be executed concurrently, that is, and 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

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.

1 Like

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:

src/         // or; in either case, containing "#[cfg(test)] mod tests;"
    tests/         // this is NOT the top-level tests directory     // 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:

    ab_test/     // 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.