-
Notifications
You must be signed in to change notification settings - Fork 2.6k
cargo test: option to run all tests under the same binary #13450
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
Note that with #5609, we plan to run test binaries in parallel. We are actively working towards this with the T-testing-devex team. The first step is json output for libtest for cargo to read and process. Reducing link times would be great though. Ways to opt-in
My main question is how to setup cargo/rustc to enable this, both with and without |
Hi :) The link you shared is for a multicrate testing though, instead of single crate? Or are you planning on running binaries in the same crate in parallel too? Anyway, I think it would be nice if running everything in the same binary per thread would be facilitated, as single process multithreading (for tests in a crate) solves all 4 listed issues. |
I see that Issue being about running all selected test targets in parallel, whether they belong to the same package or not. There is another issue about running the available test binaries in parallel to compilation. My plan is to not run a process per test unlike nextest because that runs into shared state issues.
Correct, this does not help with reducing link time.
My assumption is that we'll use jobserver to limit the number of threads across all processes |
Had some more thoughts on reducing the link time. A very rough sketch:
(names to be bikesheded)
This still leaves out custom test harnesses. Our working approach to testing is that libtest is effectively frozen and we need to make custom test harnesses a first class experience to allow evolution outside of libtest. One approach is if we move forward with a target field to specify If we do not have a way to delegate We likely will need to defer the custom test harness discussion to have a better idea of what other pieces may be available (e.g. delegated main) Benefits
In future editions we could migrate Questions for what the long term
Open questions
Prior discussions and related links |
Asked T-compiler for input on |
Uh oh!
There was an error while loading. Please reload this page.
Problem
Currently if we have multiple test files (containing multiple test functions each) under the
tests
folder in a crate directory,cargo test
generates a different binary for each of test files. For example below:cargo test
will generate a binary fortest_file1.rs
and another one fortest_file2.rs
.This article reports some of the issues with this approach, especially for larger projects/projects with some slow tests:
rustc
will need to link the library for each of these binaries, potentially increasing compilation times.This can also have some other complications:
3.1) Tests cannot share expensive initialization steps: If many tests, even if they are split across different files, require a common expensive initialization step, this is easy to handle if everything is in the same binary/process: we can execute the computationally hard step, store it with something like
lazy_static
and then load it in the tests. This way you only calculate the expensive step once, and the use it many times. Of course, this is only possible if all the tests are in the same binary. But if they are split across different binaries, you have to run the computationally expensive step once per binary, wasting time (considering that binaries are run sequentially).3.2) Systems resources have to be shared among parallel test. This is not an issue in
cargo test
but is, for example, incargo nextest
, since it does not use the threading model. If some tests require not a computationally expensive step, but one that uses a large fraction of the ram (loading a very big matrix for example), this will limit parallelization, since we will need to load this large amount of data in every binary, limiting the number of binaries that can be run concurrently.Proposed Solution
One way to currently solve this is to structure the tests according to the following:
However, I suggest that
cargo test
could allow compiling all the tests to the same binary even if we use the original organization:Hypothetical solutions:
a) Change the behaviour of
cargo test
so that everything is run under the same binary by default. I would imagine however, that this would have backward compatibility issues, especially with multithreaded apps usingunsafe
code?b) Simply add a flag that would enable this.
Note:
cargo nextest
, with its paralellization model using jobs, is able to solve issue (2). However, using processes instead of threads have the disadvantage of potentially making issues (3.1) and (3.2) worse.Notes
No response
The text was updated successfully, but these errors were encountered: