> ## Documentation Index
> Fetch the complete documentation index at: https://site.aspect.build/llms.txt
> Use this file to discover all available pages before exploring further.

# Coverage and Test Cases

> Learn how Bazel collects test coverage with bazel coverage, reports JUnit test cases, and captures extra outputs via the TEST_UNDECLARED_OUTPUTS_DIR variable.

## Test coverage

Bazel can collect test coverage with a dedicated `bazel coverage` command.

This re-builds source files with instrumentation that captures which lines are executed, then spawns the test runner with flags to collect coverage data for supported languages or test runners.

Finally Bazel combines coverage reports from all test runners, so you can report overall coverage in a multi-language project.

The quality of this support varies dramatically from ruleset to ruleset (i.e. across languages). Also note that the `coverage` command sets flags which cause the analysis cache to be discarded, which can cost minutes in Continuous Integration (CI).

In practice, it was probably a mistake to make `coverage` a separate command. Consider using the equivalent `bazel test --collect_code_coverage --instrumentation_filter=^//`

## Test Cases

Most test runners support the JUnit XML format for reporting results at the individual test case level. The implementation of the testing ruleset must have explicit support for this.

These are collected in the `bazel-testlogs` folder. Bazel can also report the individual test case results to its terminal output with the flag `--test_summary=testcase`.

### Other test outputs

You might want to grab screenshots or other output files that a test generates. Bazel doesn't allow tests to produce outputs the way build steps can, since tests are not build "Actions" but rather just some program being run.

You can read the environment variable `TEST_UNDECLARED_OUTPUTS_DIR` from your test, and write files into that folder. After Bazel finishes, you can collect the results as folders from the `bazel-testlogs` folder.
