- Kinds of tests
- Further reading
The Rust project runs a wide variety of different tests, orchestrated by
the build system (
This section gives a brief overview of the different testing tools.
Subsequent chapters dive into running tests and adding new tests.
There are several kinds of tests to exercise things in the Rust distribution.
Almost all of them are driven by
./x test, with some exceptions noted below.
The main test harness for testing the compiler itself is a tool called compiletest.
It supports running different styles of tests, called test suites.
The tests are all located in the
The Compiletest chapter goes into detail on how to use this tool.
./x test tests/ui
The standard library and many of the compiler packages include typical Rust
unit tests, integration tests, and documentation tests.
You can pass a path to
x.py to almost any package in the
x will essentially run
cargo test on that package.
|Runs tests on |
|Runs tests on |
|Runs tests on |
The standard library relies very heavily on documentation tests to cover its functionality. However, unit tests and integration tests can also be used as needed. Almost all of the compiler packages have doctests disabled.
All standard library and compiler unit tests are placed in separate
(which is enforced in tidy).
This ensures that when the test file is changed, the crate does not need to be recompiled.
#[cfg(test)] mod tests;
If it wasn't done this way,
and you were working on something like
that would require recompiling the entire standard library, and the entirety of
./x test includes some CLI options for controlling the behavior with these tests:
--doc— Only runs documentation tests in the package.
--no-doc— Run all tests except documentation tests.
Tidy is a custom tool used for validating source code style and formatting conventions, such as rejecting long lines. There is more information in the section on coding conventions.
./x test tidy
Rustfmt is integrated with the build system to enforce uniform style across the compiler. The formatting check is automatically run by the Tidy tool mentioned above.
|Checks formatting and exits with an error if formatting is needed.|
|Runs rustfmt across the entire codebase.|
|First runs rustfmt to format the codebase, then runs tidy checks.|
All of the books that are published have their own tests,
primarily for validating that the Rust code examples pass.
Under the hood, these are essentially using
rustdoc --test on the markdown files.
The tests can be run by passing a path to a book to
./x test src/doc/book
Links across all documentation is validated with a link checker tool.
./x test src/tools/linkchecker
./x test linkchecker
This requires building all of the documentation, which might take a while.
distcheck verifies that the source distribution tarball created by the build system
will unpack, build, and run all tests.
./x test distcheck
Packages that are included with Rust have all of their tests run as well. This includes things such as cargo, clippy, rustfmt, miri, bootstrap (testing the Rust build system itself), etc.
Most of the tools are located in the
To run the tool's tests, just pass its path to
./x test src/tools/cargo
Usually these tools involve running
cargo test within the tool's directory.
cargotest is a small tool which runs
cargo test on a few sample projects
This ensures there aren't any significant regressions.
./x test src/tools/cargotest
Crater is a tool which runs tests on many thousands of public projects. This tool has its own separate infrastructure for running. See the Crater chapter for more details.
A separate infrastructure is used for testing and tracking performance of the compiler. See the Performance testing chapter for more details.
The following blog posts may also be of interest:
- brson's classic "How Rust is tested"