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

# 使用 Bun 测试运行器生成代码覆盖率报告

Bun 的测试运行器内置了*代码覆盖率报告*。用它来查看代码库被测试覆盖了多少，以及差距在哪里。

***

将 `--coverage` 标志传递给 `bun test`，可在测试运行后打印覆盖率报告。

该报告列出了测试执行过的源文件、运行过的函数和行的百分比，以及从未运行过的行范围。

```sh terminal icon="terminal" theme={null}
bun test --coverage
```

```txt theme={null}

test.test.ts:
✓ math > add [0.71ms]
✓ math > multiply [0.03ms]
✓ random [0.13ms]
-------------|---------|---------|-------------------
文件         | 函数覆盖率 | 行覆盖率 | 未覆盖行号
-------------|---------|---------|-------------------
全部文件     |   66.67 |   77.78 |
 math.ts     |   50.00 |   66.67 |
 random.ts   |   50.00 |   66.67 |
-------------|---------|---------|-------------------

 3 通过
 0 失败
 3 个 expect() 调用
```

***

要默认启用覆盖率报告，请将以下内容添加到你的 `bunfig.toml` 中：

```toml bunfig.toml icon="settings" theme={null}
[test]
coverage = true # 始终启用覆盖率
```

***

参见 [代码覆盖率](/test/code-coverage)。
