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

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

Bun 的测试运行器支持内置的 *代码覆盖率报告*。使用它来查看你的代码库有多少由测试覆盖，并找到当前尚未得到很好测试的区域。

***

只需向 `bun test` 传递 `--coverage` 参数即可启用此功能。测试运行结束后将打印覆盖率报告。

覆盖率报告列出了测试运行期间执行的源文件、执行的函数和代码行的百分比，以及运行时未执行的代码行范围。

```sh terminal icon="terminal" theme={"theme":{"light":"github-light","dark":"dracula"}}
bun test --coverage
```

```txt theme={"theme":{"light":"github-light","dark":"dracula"}}

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={"theme":{"light":"github-light","dark":"dracula"}}
[test]
coverage = true # 始终启用覆盖率
```

***

完整的 Bun 代码覆盖率报告文档请参见 [文档 > 测试运行器 > 覆盖率](/test/code-coverage)。
