Are the built-in integration tests run concurrently or sequentially?

An alternative to an env var is the --test-threads flag. Set it to a single thread to run your tests sequentially.

cargo test -- --test-threads 1

The built-in testing framework runs concurrently by default. It is designed to offer useful but simple support for testing, that covers many needs, and a lot of functionality can/should be tested with each test independent of the others. (Being independent means they can be run in parallel.)

That said, it does listen to the RUST_TEST_THREADS environment variable, e.g. RUST_TEST_THREADS=1 cargo test will run tests on a single thread. However, if you want this functionality for your tests always, you may be interested in not using #[test], or, at least, not directly.

The most flexible way is via cargo's support for tests that completely define their own framework, via something like the following in your Cargo.toml:

[[test]]
name = "foo"
harness = false

With that, cargo test will compile and run tests/foo.rs as a binary. This can then ensure that operations are sequenced/reset appropriately.

Alternatively, maybe a framework like stainless has the functionality you need. (I've not used it so I'm not sure.)

Tags:

Rust