A code coverage tool for Rust projects
Tarpaulin is a code coverage reporting tool for the Cargo build system, named for a waterproof cloth used to cover cargo on a ship.
Currently, Tarpaulin provides working line coverage, and while fairly reliable, may still contain minor inaccuracies in the results. A lot of work has been done to get it working on a wide range of projects, but unique combinations of packages and build features can cause issues, so please report anything you find that's wrong. Also, check out our roadmap for planned features.
On Linux, Tarpaulin's default tracing backend is still Ptrace and will only work
on x86_64 processors. This can be changed to the llvm coverage instrumentation
with --engine llvm. For Mac and Windows, this is the default collection
method.
It can also be run in Docker, which is useful for when you don't use Linux but want to run it locally, e.g. during development. See below for how to do that.
Below is the help-text for a thorough explanation of the flags and features available:
…If your tests or application make use of unix signals they may not work with
ptrace instrumentation in Tarpaulin. This is because Tarpaulin relies on the
sigtrap signal to catch when the instrumentation points are hit. The
--forward option results in forwarding the signals from process stops not
caused by SIGSTOP, SIGSEGV or SIGILL to the test binary.
Despite generally being far more accurate there are some nuances with the LLVM coverage instrumentation.
In these cases coverage results may differ a lot between ptrace and llvm and llvm
coverage may be a worse choice. Things like doc tests with the should_panic
attribute or --no-fail-fast won't report any coverage because of non-zero
exit codes and if you use these and want coverage data from them you should
avoid the llvm coverage backend.
Below is a list of features currently implemented. As Tarpaulin loads binary files into memory and parses the debugging information, different setups could lead to coverage not working. In this instance, please raise an issue detailing your setup and an example project and I'll attempt to fix it (please link us to a repo and the commit containing your project and paste the verbose output).
Config file section for details)Issues, feature requests and pull requests are always welcome! For a guide on how to approach bugs found in Tarpaulin and add features please check CONTRIBUTING. If you're having any troubles also look to our TROUBLESHOOTING
Rust 1.23 introduced a regression in the compiler affecting Tarpaulin's accuracy. If you see missing lines or files, check your compiler version.
Tarpaulin is a command-line program, you install it into your development environment with cargo install. A locked install is recommended to avoid being impacted by breaking changes in dependencies:
cargo install --locked cargo-tarpaulinWhen using the Nix package manager, the nixpkgs.cargo-tarpaulin package can be used.
This ensures that Tarpaulin will be built with the same rust version as the rest of your packages.
You can also use cargo-binstall:
cargo binstall cargo-tarpaulinWhen Tarpaulin runs your tests it strives to run them in the same environment as if they were run via cargo test. To achieve this it sets the following environment variables when executing the test binaries:
1 when --verbose flag is used unless it is already setFor Tarpaulin to construct the Cargo environment correctly, Tarpaulin needs to find Cargo.toml by either:
If Cargo does not find any Cargo.toml from using either of the above methods the run will error "cargo metadata" and exit.
Several RFCs are open in rust-lang to expose more of these directly in order to avoid the issues arising out of this.
To get detailed help on available arguments when running Tarpaulin call:
cargo tarpaulin --helpCurrently, no options are required, if no root directory is defined Tarpaulin will run in the current working directory.
Below is a Tarpaulin run utilising one of our example projects. This is a relatively simple project to test and if you check the test, you can see the output correctly reports the lines the test hits.
…Tarpaulin can also report the change in coverage for each file between runs. If the tests were updated in the previous example to cover all the lines we would expect the following output.
…Hint: if using coveralls.io with travis-ci run with the options
--ciserver travis-ci --coveralls $TRAVIS_JOB_ID. The coveralls.io repo-token
is mainly designed for private repos and it won't generate a badge for the
coverage results submitted (although you can still see them on the coveralls
web interface). For an example of a project using Tarpaulin, you can check out
my crate keygraph-rs.
See the wasm-bindgen guide on how to get coverage for the wasm32-unknown-unknown target.
Tarpaulin allows you to ignore modules or functions using attributes. Below is an example of ignoring the main function in a project:
#[cfg(not(tarpaulin_include))]
fn main() {
println!("I won't be included in results");
}
// Also supports the nightly rustc `coverage(off)` attribute.
#[coverage(off)]
fn not_included() {
}Unfortunately, due to the unexpected cfg warnings cargo now emits you will
likely want to add the recommended lints to your Cargo.toml, or utilise any
existing build scripts. If you're using a nightly compiler then making use of
unstable coverage attributes may be preferable.
However, the skip attribute only allows you to exclude code from coverage
it doesn't change the code present in the binaries or what tests are run.
Because of this, --cfg=tarpaulin is used when building your project for
Tarpaulin allowing you to also conditionally include/exclude code from
compilation entirely. For example to have a test that isn't included in
the test binaries when built with Tarpaulin and cannot be run just do:
#[test]
#[cfg(not(tarpaulin))]
fn big_test_not_for_tarpaulin() {
// Something that would be very slow in tarpaulin or not work
}If you still want the test included in the binary and ignored by default you can use:
#[test]
#[cfg_attr(tarpaulin, ignore)]
fn ignored_by_tarpaulin() {
}There is also nightly support for using tool attributes with Tarpaulin for skip. For example:
#![feature(register_tool)]
#![register_tool(tarpaulin)]
#[tarpaulin::skip]
fn main() {
println!("I won't be in coverage stats");
}As Tarpaulin changes the RUSTFLAGS when building tests sometimes rebuilds of
test binaries can't be avoided. There is also a --force-clean and --skip-clean
argument, the default has been changed at times to avoid issues with incremental
compilation when changing RUSTFLAGS. If you aim to reduce the amount of
unnecessary recompilation attempting to add the --skip-clean flag should be
the first step. After that you can either:
cargo tarpaulin --print-rust-flags and use those flags for dev and coverage--target-dir when running Tarpaulin and have a coverage build and dev buildTarpaulin aims to be easy to add to your CI workflow. With well-tested support for Travis-CI it also supports sending CI specific meta-data to coveralls.io for Circle, Semaphore, Jenkins and Codeship (though only Jenkins has been tested).
You can also use Tarpaulin on Azure, check out crate-ci/azure-pipelines for an example config.
The expected most common use case is launching coverage via a CI service to upload to a site like codecov or coveralls. Given the built-in support and ubiquity of travis-ci it seems prudent to document the required steps here for new users. To follow these steps you'll first need a travis-ci and a project setup for your coverage reporting site of choice.
We recommend taking the minimal rust .travis.yml, installing the libssl-dev
dependency Tarpaulin has and then running Tarpaulin with the version of
rustc you require. Tarpaulin is installed in before_cache to allow it to be cached
and prevent having to reinstall every Travis run. You can also replace cargo test
with a verbose run of Tarpaulin to see the test results as well as coverage output.
Tarpaulin is run after success as there are still some unstable features which could
cause coverage runs to fail. If you don't rely on any of these features you can
alternatively replace cargo test with a call to cargo tarpaulin.
For codecov.io you'll need to export CODECOV_TOKEN there are instructions on this in
the settings of your codecov project.
…If you rely on certain nightly features you may need to change the before_script to
before_cache to force Tarpaulin to reinstall each time. However, if it can be avoided it
will speed up your CI runs.
Alternatively, there are the prebuilt docker images or you can use cargo-binstall.
The GNU Linux prebuilt binaries are built against glibc 2.31, matching Debian bullseye
and Ubuntu 20.04. If you need to run on an older distribution, use the musl release
artifact with cargo-binstall --target x86_64-unknown-linux-musl or install from
source with cargo install.
File .github/workflows/coverage.yml
Example how to run coverage within docker with seccomp in GitHub Actions and push the result
to <codecov.io>.
…To run Tarpaulin on CircleCI you need to run Tarpaulin in docker and set the machine flag to true as shown below:
jobs:
coverage:
machine: true
steps:
- checkout
- run:
name: Coverage with docker
command: docker run --rm --security-opt seccomp=unconfined -v "${PWD}:/volume" xd009642/tarpaulinTo get the coverage results showing up in your Gitlab pipelines add the following regex to the Test coverage section in the gitlab job definition in .gitlab-ci.yml:
job: ...
coverage: '/^\d+.\d+% coverage/'Gitlab can [show coverage information] in the diff of a merge request. For that, use
job: ...No open issues yet, or sync has not completed.