25

I have tests which share a common resource and can't be executed concurrently. These tests fail with cargo test, but work with RUST_TEST_THREADS=1 cargo test.

I can modify the tests to wait on a global mutex, but I don't want to clutter them if there is any simpler way to force cargo set this environment variable for me.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
kriomant
  • 2,216
  • 1
  • 16
  • 21
  • As much as I love duplicates, this doesn't seem to be a duplicate of "Is there any way to tell Cargo to run its tests on the main thread?". This question specifically is asking if there's a way to set the number of threads somewhere in the filesystem so that neither the environment variable or command line option need to be passed. For that question, even setting the number of threads to 1 doesn't work because it's still a separate thread from main. – Shepmaster Jul 06 '17 at 13:42
  • 1
    There's an [open PR](https://github.com/rust-lang/rust/pull/42684) that proposes to add an attribute like `#[serial]` to the test runner which would cause the marked tests to not run in parallel, but it's anyones guess if this will be accepted. – Shepmaster Jul 06 '17 at 13:45

1 Answers1

21

As of Rust 1.18, there is no such thing. In fact, there is not even a simpler option to disable parallel testing.
Source

However, what might help you is cargo test -- --test-threads=1, which is the recommended way of doing what you are doing over the RUST_TEST_THREADS envvar. Keep in mind that this only sets the number of threads used for testing in addition to the main thread.

Jan Hohenheim
  • 3,552
  • 2
  • 17
  • 42