Disable parallel testing for module

The number of threads can be specified when you run a test. Running tests in parallel can be disabled by running:

cargo test -- --test-threads=1

This disables it for all tests.

Is there a way to specify a certain test module, or integration test, must be ran in a single thread but the rest can be ran in parallel?

There might be a way using lazy_static and a mutex, but is there an easier way?

1 Like

If you put it in a standalone file in tests/, it will run on its own.

The only time I've seen a need for this is when accessing the file system during tests. If many tests use the same file name it causes problems when they run in parallel. I've fixed this by using the tempdir crate so that every test has it's own workspace when running.

I'm not sure if that will help with your case though...maybe you are sharing some other resources between tests?

1 Like

Isolating tests to avoid this problem is the best. Global mutex sounds like an OK option too.

Rust on principle assumes everything is thread-safe, so it's a good practice to keep code under test thread-safe too. --test-threads=1 is useful for discovering which test causes crash of the test suite (because otherwise test names are printed only after the test completes), but IMHO shouldn't be a workaround for other test issues.

I agree that having the tests being isolated is ideal. I'm working on a wrapper for a project created by another team. I am already using temporary files where possible (which enables most tests to be run in parallel). The problem is that they have a global config variable that can be set. Test A can set this variable, and before it finishes, Test B has changed the variable causing Test A to fail.