# Disable parallel testing for module

**URL:** <https://users.rust-lang.org/t/disable-parallel-testing-for-module/20207>\
**Category:** help\
**Created:** [September 6, 2018, 8:31pm UTC](https://users.rust-lang.org/t/disable-parallel-testing-for-module/20207 "2018-09-06T20:31:15Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![dastardlychimp](https://avatars.discourse-cdn.com/v4/letter/d/76d3ee/32.png) [@dastardlychimp](https://users.rust-lang.org/u/dastardlychimp)\
**Post date:** [September 6, 2018, 8:31pm UTC](https://users.rust-lang.org/t/disable-parallel-testing-for-module/20207/1 "2018-09-06T20:31:15Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![cuviper](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/cuviper/32/3156_2.png) [@cuviper](https://users.rust-lang.org/u/cuviper)\
**Post date:** [September 6, 2018, 8:37pm UTC](https://users.rust-lang.org/t/disable-parallel-testing-for-module/20207/2 "2018-09-06T20:37:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ryan](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/ryan/32/3732_2.png) [@ryan](https://users.rust-lang.org/u/ryan)\
**Post date:** [September 6, 2018, 8:45pm UTC](https://users.rust-lang.org/t/disable-parallel-testing-for-module/20207/3 "2018-09-06T20:45:16Z")

</div>

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](https://crates.io/crates/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?

---

<div class="post-metadata">

**Author:** ![kornel](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/kornel/32/4823_2.png) [@kornel](https://users.rust-lang.org/u/kornel)\
**Post date:** [September 7, 2018, 12:37am UTC](https://users.rust-lang.org/t/disable-parallel-testing-for-module/20207/4 "2018-09-07T00:37:33Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dastardlychimp](https://avatars.discourse-cdn.com/v4/letter/d/76d3ee/32.png) [@dastardlychimp](https://users.rust-lang.org/u/dastardlychimp)\
**Post date:** [September 7, 2018, 12:42pm UTC](https://users.rust-lang.org/t/disable-parallel-testing-for-module/20207/5 "2018-09-07T12:42:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/steffahn/32/47569_2.png) [@steffahn](https://users.rust-lang.org/u/steffahn)\
**Post date:** [January 12, 2023, 8:49am UTC](https://users.rust-lang.org/t/disable-parallel-testing-for-module/20207/6 "2023-01-12T08:49:31Z")

</div>

Closed on January 12, 2023, 8:49am UTC


