-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Should unit tests spawn new processes instead of threads? #47506
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I'd really appreciate this at some point. I'm using Rust to functionally verify my companies C code base. Our code is automotive grade(runs on ECU's) so it uses lots of global's and massive functions with significant internal state (the code is auto generated from a model). Being able to spawn tests as processes would allow tests to run in parallel which would be a huge win. |
rusty-fork can be used to work around this issue |
cc #32512 |
Poking this old issue because having an option to force this, even if it isn't the default, would be very beneficial for running a test suite under miri, which tracks all memory allocated over the lifetime of a program. |
It would be nice to be able to denote unit tests to have this as their behavior. |
I looked at rusty-fork just now and it seems incompatible with rstest which provides functionality to parameterize tests more easily. |
Running tests in separate processes would also allow for |
it seems rusty-fork isn't compatible with tests that use tokio (i.e. #[tokio-test] instead of #[test]). Would be great if this is done. |
An even more obvious use-case are libraries like MPI, since even |
I am tasked with writing tests against a code-base that uses global static variables with iterior mutability. I would like to ensure that one test can not influence another. While I would normally push for refactoring the code base for testability, this is not currently an option and a long term effort that requires planning which is out of my control. Are there any known work-arounds that let me running one test in one process reliably? I had a look at rusty-fork but it seems like that could cause problems of its own. I would like to be able to debug my tests as well. Would it be possible to for someone to write an alternative test running framework that natively supports spawning a new process for each test? |
My program is based on dpdk, which is heavily depend on global variables, when I run multiple test case and each test initialize and deinitialize the dpdk runtime, cargo test failed:
|
Since it isn't mentioned here, another workaround is using nextest which runs each test in a separate process. |
We have the same issue, use a lazy_static or singleton for connection strings and other global options. |
Imagine a (contrived) piece of code like this:
This is broken by default because cargo test will by default spawn multiple threads for the tests so the global state of Q. The user would have to know to pass --test-threads=1 or the test author would have to create a global mutex to synchronize each test on. This seems like a lot of unnecessary boilerplate, not to mention an unnecessary pain point for new developers to discover when they can't figure out why tests are producing weird results.
There also doesn't seem to be a solution at the moment that would enable you to achieve the benefits of running tests in parallel at all for shared state between tests. The global mutex approach effectively reduces the test code to implicitly running with --test-threads=1. Using separate processes by default (or at least providing a decorator to do it easily) would solve the vast majority of such problems without requiring authors to significantly restructure their code.
FWIW, other test frameworks go with the process sharding approach instead of threads.
The text was updated successfully, but these errors were encountered: