I have a collection of tests. There are a few tests that need to access a shared resource (external library/API/hardware device). If any of these tests run in parallel, they
You can always provide your own test harness. You can do that by adding a [[test]]
entry to Cargo.toml
:
[[test]]
name = "my_test"
# If your test file is not `tests/my_test.rs`, add this key:
#path = "path/to/my_test.rs"
harness = false
In that case, cargo test
will compile my_test.rs
as a normal executable file. That means you have to provide a main
function and add all the "run tests" logic yourself. Yes, this is some work, but at least you can decide everything about running tests yourself.
You can also create two test files:
tests/
- sequential.rs
- parallel.rs
You then would need to run cargo test --test sequential -- --test-threads=1
and cargo test --test parallel
. So it doesn't work with a single cargo test
, but you don't need to write your own test harness logic.