golang终端测试覆盖率可视化工具插件go-carpet的使用
golang终端测试覆盖率可视化工具插件go-carpet的使用
go-carpet - 显示Go源文件的测试覆盖率
go-carpet是一个在终端显示Go代码测试覆盖率的工具。
主要特性
- 直接在终端查看测试覆盖率
- 可以在GOPATH目录外工作
- 支持递归处理多个包
- 使用
-256colors
选项时,会用不同深浅的绿色表示覆盖率等级 - 默认跳过vendor目录(Godeps,vendor),也可使用
-include-vendor
选项包含 - 使用
-mincov
选项可以设置显示文件的覆盖率阈值
使用方法
usage: go-carpet [options] [paths]
-256colors
use more colors on 256-color terminal (indicate the level of coverage)
-args string
pass additional arguments for go test
-file string
comma-separated list of files to test (default: all)
-func string
comma-separated functions list (default: all functions)
-include-vendor
include vendor directories for show coverage (Godeps, vendor)
-mincov float
coverage threshold of the file to be displayed (in percent) (default 100)
-summary
only show summary for each file
-version
get version
要在less中查看覆盖率,使用-R
选项:
go-carpet | less -R
安装方法
从源码安装:
go install github.com/msoap/go-carpet@latest
从Homebrew安装(OS X):
brew tap msoap/tools
brew install go-carpet
# 更新:
brew upgrade go-carpet
示例截图
完整示例
下面是一个完整的示例,展示如何使用go-carpet查看测试覆盖率:
- 首先创建一个简单的Go项目:
// main.go
package main
import "fmt"
func main() {
fmt.Println(greet("World"))
}
func greet(name string) string {
return "Hello, " + name
}
- 创建测试文件:
// main_test.go
package main
import "testing"
func TestGreet(t *testing.T) {
tests := []struct {
name string
want string
}{
{"World", "Hello, World"},
{"Gopher", "Hello, Gopher"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := greet(tt.name); got != tt.want {
t.Errorf("greet() = %v, want %v", got, tt.want)
}
})
}
}
- 运行go-carpet查看覆盖率:
go-carpet -256colors .
输出会显示代码的测试覆盖率情况,不同深浅的绿色表示不同的覆盖率等级。
- 如果只想查看覆盖率低于100%的文件:
go-carpet -mincov 99 .
- 如果只想查看特定文件的覆盖率:
go-carpet -file main.go
- 如果只想查看特定函数的覆盖率:
go-carpet -func greet
更多关于golang终端测试覆盖率可视化工具插件go-carpet的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html
更多关于golang终端测试覆盖率可视化工具插件go-carpet的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
Go-Carpet:Golang终端测试覆盖率可视化工具
Go-Carpet 是一个轻量级的终端测试覆盖率可视化工具,它可以直接在终端中显示代码的测试覆盖率情况,非常适合开发者在开发过程中快速查看测试覆盖情况。
安装
go install github.com/msoap/go-carpet@latest
安装完成后,你可以直接使用 go-carpet
命令。
基本使用
查看当前目录的测试覆盖率
go-carpet
这会运行当前目录下的所有测试,并在终端中显示覆盖率情况。
查看指定包的测试覆盖率
go-carpet ./pkg/...
显示详细覆盖率信息
go-carpet -256colors -summary ./...
-256colors
参数会启用更丰富的颜色显示,-summary
会显示覆盖率摘要。
高级功能
排除某些文件
go-carpet -exclude "_test.go,vendor/" ./...
设置覆盖率阈值
go-carpet -min-coverage 80 ./...
如果覆盖率低于80%,命令会返回非零退出码。
作为Go测试工具使用
你可以在 go test
命令中使用 -exec
参数来集成 go-carpet:
go test -coverprofile=cover.out ./... && go-carpet -file cover.out
示例代码
下面是一个完整的示例,展示如何在Go项目中集成go-carpet:
// main.go
package main
import "fmt"
func Add(a, b int) int {
return a + b
}
func Subtract(a, b int) int {
return a - b
}
func main() {
fmt.Println("Calculator")
}
// main_test.go
package main
import "testing"
func TestAdd(t *testing.T) {
result := Add(2, 3)
if result != 5 {
t.Errorf("Expected 5, got %d", result)
}
}
// 注意:这里故意没有写Subtract的测试用例
运行测试并查看覆盖率:
go-carpet
输出结果会在终端中显示,未覆盖的代码行会以红色显示(Subtract函数)。
与其他工具集成
与Makefile集成
test-coverage:
@go-carpet -256colors -summary ./...
与CI集成
可以在CI脚本中添加:
go-carpet -min-coverage 80 ./... || exit 1
优缺点分析
优点:
- 直接在终端显示结果,无需打开浏览器
- 轻量级,安装简单
- 支持多种颜色主题
- 可以设置最小覆盖率阈值
缺点:
- 不如HTML报告直观
- 对于大型项目,终端显示可能不够清晰
替代方案
如果你需要更详细的覆盖率报告,可以考虑:
go tool cover -html=cover.out
生成HTML报告gocov
和gocov-html
组合go-acc
用于更精确的覆盖率计算
Go-Carpet 最适合在开发过程中快速检查覆盖率情况,而对于正式的覆盖率报告,建议使用更全面的工具。