# Cargo

Your Cargo suite gets a record from [Variance Authority](https://variance-authority.dev/docs/index.md) of which
functions of your code each test ran. `variance cargo` runs your tests with
rustc's coverage counters, with no `llvm-cov` installed, and writes it. Then
`variance select` names the test targets a change did not reach, and
`variance covering` names the tests that ran a function.

## Try it

The CLI is a Node package, so you need Node 22.15 or newer. Install it at the
repository root, beside your `Cargo.toml`, and declare the suite in
`variance.config.json` there:

```bash
npm install --save-dev @variance-authority/cli
```

```json
{ "suites": { "unit": { "kind": "unit" } } }
```

Record once, from the crate or the workspace root. What follows `--` goes to
`cargo test`, and a test filter there is refused, since every test the build
lists runs:

```bash
variance cargo --suite unit -- --workspace --lib --locked
```

It exits `0` when every test passed and `101` when a test or the build failed.
Doctests are not run, so run `cargo test --doc` beside it.

## What you get

### Leave out the test targets a change did not reach

Cargo does not leave targets out itself. After you change a function, ask:

```bash
variance select --suite unit --since origin/main
```

It prints the test files the change does not reach, one per line, and each is
one target: `src/lib.rs` is the crate's `--lib`, and `tests/negates.rs` is
`--test negates`. Run `cargo test` with the targets that were not printed. A
change to `Cargo.toml`, `Cargo.lock` or a build script selects every target.

### Ask which tests ran a function

```bash
variance covering --file src/lib.rs --function add
```

```text
unit (unit):
  2 named tests covered function add of src/lib.rs:
    src/lib.rs — 2/3
      tests::adds
      tests::muls
```

A method is named by the `impl` around it, such as `Calc/twice`. [What a row
holds](https://variance-authority.dev/docs/rust-reference/index.md#what-a-row-holds) says how each function is named.

## Not yet

- The runner leaving targets out itself: `variance select` prints them, for the
  script that runs `cargo test` to leave out.
- Skipping tests inside a selected target: every test in it runs.
- Shard placement by recorded time.
- [Test stories](https://variance-authority.dev/docs/test-stories/index.md) and visual review.

## Go further

- [The Cargo reference](https://variance-authority.dev/docs/rust-reference/index.md): what `variance cargo` builds and runs, and what it costs.
- [How it differs from `cargo test`](https://variance-authority.dev/docs/rust-reference/index.md#record-which-tests-ran-each-line), each test in its own process.
- [When a left-out Cargo test could still have failed](https://variance-authority.dev/docs/rust-reference/index.md#when-a-left-out-test-could-still-have-failed), from build scripts to `include_str!`.
- [Supported frameworks](https://variance-authority.dev/docs/supported-frameworks/index.md) puts Cargo beside every other runner.
