unit-testingtestingrustrust-cargo

How to execute tests with Cargo in random order?


To be sure that my tests are isolated, I would like to randomize the order of execution.

Is there an option to select when running tests with Cargo? Or a plugin to install, like pytest-random-order for pytest in Python?


Solution

  • In Rust, using Cargo for testing, you can execute your tests in random order using the --test-threads flag in combination with environment variables.

    Cargo runs tests in parallel by default, but you can explicitly set the number of threads to encourage more randomness in execution timing.

    cargo test --test-threads=4