golang人性化格式化测试输出插件库gotestfmt的使用
Golang人性化格式化测试输出插件库gotestfmt的使用
gotestfmt是一个用于格式化Golang测试输出的工具,它能将原始的go test输出转换为更易读、结构化的格式。
安装gotestfmt
手动安装
你可以从发布页面手动下载二进制文件,这些文件没有依赖,可以在列出的任何操作系统上运行。
使用go install
go install github.com/gotesttools/gotestfmt/v2/cmd/gotestfmt@latest
使用容器
go test -json ./... | docker run ghcr.io/gotesttools/gotestfmt:latest
基本使用
set -euo pipefail
go test -json -v ./... 2>&1 | tee /tmp/gotest.log | gotestfmt
这个命令会:
- 运行go test并输出JSON格式的结果
- 同时将原始日志保存到/tmp/gotest.log
- 通过管道将输出传递给gotestfmt进行格式化
CI系统集成
GitHub Actions示例
jobs:
build:
name: Test
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.16
- name: Set up gotestfmt
uses: gotesttools/gotestfmt-action@v2
with:
token: ${{ secrets.GITHUB_TOKEN }}
- name: Run tests
run: |
set -euo pipefail
go test -json -v ./... 2>&1 | tee /tmp/gotest.log | gotestfmt
- name: Upload test log
uses: actions/upload-artifact@v2
if: always()
with:
name: test-log
path: /tmp/gotest.log
if-no-files-found: error
自定义输出
你可以通过创建.gotestfmt
文件夹并添加以下模板文件来自定义输出:
downloads.tpl
- 控制包下载的输出package.tpl
- 控制单个包测试结果的输出
常见问题
如何减少输出冗余?
使用-hide
参数可以隐藏某些输出:
gotestfmt -hide "successful-tests,successful-downloads"
如何格式化测试中的日志行?
从v2.2.0开始支持外部格式化器:
go test -json -v ./... 2>&1 | gotestfmt -formatter "/path/to/your/formatter"
为什么gotestfmt会以非零状态退出?
从v2.3.0开始,当测试失败时gotestfmt会返回非零退出状态。可以使用-nofail
参数禁用此行为。
示例项目结构
project/
├── .github/
│ └── workflows/
│ └── test.yml # GitHub Actions配置文件
├── .gotestfmt/ # 自定义模板目录(可选)
│ ├── downloads.tpl
│ └── package.tpl
├── go.mod
├── go.sum
└── ... # 你的项目代码
gotestfmt通过解析go test -json
的输出并将其重新格式化为更易读的形式,特别适合在CI系统中使用,能显著提高测试结果的可读性。
更多关于golang人性化格式化测试输出插件库gotestfmt的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html
更多关于golang人性化格式化测试输出插件库gotestfmt的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
gotestfmt:人性化格式化Go测试输出
gotestfmt是一个用于美化Go测试输出的工具,它可以将标准go test
的输出转换为更清晰、易读的格式,特别适合在CI/CD管道或开发过程中使用。
安装gotestfmt
go install github.com/gotesttools/gotestfmt/v2/cmd/gotestfmt@latest
基本使用
1. 直接运行测试并格式化输出
go test -v ./... | gotestfmt
2. 保存到文件
go test -json ./... | gotestfmt > test-report.txt
3. 在CI/CD中使用
go test -json ./... | gotestfmt -w
-w
参数会在测试失败时返回非零退出码,适合CI环境。
主要特性
- 彩色输出:区分通过/失败的测试
- 清晰的测试结构:显示包和测试的层次关系
- 时间统计:显示每个测试的运行时间
- 失败摘要:在输出底部汇总所有失败的测试
- 覆盖率整合:可以显示测试覆盖率信息
配置选项
gotestfmt支持多种配置选项:
# 禁用颜色输出
go test -json ./... | gotestfmt --nocolor
# 显示覆盖率信息
go test -coverprofile=coverage.out -json ./... | gotestfmt --coverprofile coverage.out
# 只显示失败的测试
go test -json ./... | gotestfmt --hide-successful
与标准输出的对比
标准go test -v
输出:
=== RUN TestAdd
--- PASS: TestAdd (0.00s)
=== RUN TestSubtract
--- FAIL: TestSubtract (0.00s)
calc_test.go:20: Expected 3, got 1
gotestfmt输出:
✓ pkg/calc TestAdd (0.00s)
✗ pkg/calc TestSubtract (0.00s)
calc_test.go:20: Expected 3, got 1
=== FAILURES ===
pkg/calc TestSubtract
集成到Makefile示例
test:
@go test -json ./... | gotestfmt
高级用法:自定义主题
gotestfmt支持自定义输出主题。创建一个JSON配置文件:
{
"colors": {
"success": "#00ff00",
"failure": "#ff0000",
"skipped": "#ffff00"
},
"symbols": {
"success": "✓",
"failure": "✗",
"skipped": "∅"
}
}
然后使用:
go test -json ./... | gotestfmt --theme custom-theme.json
性能考虑
gotestfmt处理JSON输出会有轻微的性能开销,但对于大多数项目来说可以忽略不计。如果遇到性能问题,可以考虑:
- 只在本地开发时使用gotestfmt
- 在CI中只对失败时使用gotestfmt格式化输出
gotestfmt是改善Go测试可读性的优秀工具,特别适合大型项目或测试输出复杂的场景。