golang将Go测试结果转换为可读语句的测试报告插件gotestdox的使用

Golang将Go测试结果转换为可读语句的测试报告插件gotestdox的使用

gotestdox是一个命令行工具,用于将Go测试结果格式化为可读的文档,如《The Power of Go: Tests》一书中所推荐的那样。

安装方法

go install github.com/bitfield/gotestdox/cmd/gotestdox@latest

Writing gopher logo

基本使用

在任何Go项目中运行:

gotestdox ./...

功能说明

gotestdox会运行你的测试并报告结果,但它以一种特殊的方式格式化测试名称。它将驼峰式命名的测试名称转换为普通句子。

例如,假设我们有以下测试名称:

TestValidIsTrueForValidInputs
TestValidIsFalseForInvalidInputs

gotestdox会将它们转换为:

 ✔ Valid is true for valid inputs (0.00s)
 ✔ Valid is false for invalid inputs (0.00s)

完整示例

下面是一个完整的示例展示如何使用gotestdox:

  1. 首先创建一个简单的测试文件 example_test.go
package example

import "testing"

// 测试函数1
func TestValidIsTrueForValidInputs(t *testing.T) {
    // 测试逻辑
}

// 测试函数2
func TestHandleInput_ClosesInputAfterReading(t *testing.T) {
    // 测试逻辑
}
  1. 运行gotestdox:
gotestdox ./...

输出将类似于:

example:
 ✔ Valid is true for valid inputs (0.00s)
 ✔ HandleInput closes input after reading (0.00s)

高级用法

多单词函数名

对于函数名包含多个单词的情况,可以使用下划线作为分隔:

TestHandleInput_ClosesInputAfterReading

这将转换为:

 ✔ HandleInput closes input after reading

过滤标准输入

你可以自己运行 go test -json 并将输出通过管道传递给 gotestdox:

go test -json | gotestdox

测试标志和参数

任何传递给 gotestdox 的参数都将传递给 go test。例如:

gotestdox -run ParseJSON

这相当于运行:

go test -json -run ParseJSON

多个包

要测试当前树中的所有包:

gotestdox ./...

输出将显示每个包的测试结果:

github.com/octocat/mymodule/api:
 ✔ NewServer errors on invalid config options (0.00s)
 ✔ NewServer returns a correctly configured server (0.00s)

github.com/octocat/mymodule/util:
 x LeftPad adds the correct number of leading spaces (0.00s)
    util_test.go:133: want "  dummy", got " dummy"

为什么使用gotestdox?

gotestdox的主要价值在于它鼓励开发者编写具有信息性的测试名称,这些名称能够清晰地描述单元的行为。当通过gotestdox格式化时,这些测试名称会形成易于理解的句子。

测试名称应该是句子,这个简单的想法有着强大的效果。将gotestdox的输出展示给用户、客户或业务人员,看看是否对他们有意义,可能会引发一些有趣的对话。


更多关于golang将Go测试结果转换为可读语句的测试报告插件gotestdox的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html

1 回复

更多关于golang将Go测试结果转换为可读语句的测试报告插件gotestdox的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


使用gotestdox将Go测试结果转换为可读报告

gotestdox是一个实用的Go测试工具,它可以将标准的go test输出转换为更易读的句子格式,让测试结果更加清晰直观。下面我将详细介绍如何使用gotestdox。

安装gotestdox

首先需要安装gotestdox:

go install github.com/bitfield/gotestdox@latest

基本使用

最简单的使用方式是直接运行:

go test -v | gotestdox

这会运行所有测试并将结果转换为可读格式。

示例

假设我们有以下测试文件calculator_test.go

package main

import "testing"

func TestAdd(t *testing.T) {
    if Add(1, 2) != 3 {
        t.Error("Expected 1 + 2 to equal 3")
    }
}

func TestSubtract(t *testing.T) {
    if Subtract(5, 3) != 2 {
        t.Error("Expected 5 - 3 to equal 2")
    }
}

func TestMultiply(t *testing.T) {
    if Multiply(2, 3) != 6 {
        t.Error("Expected 2 * 3 to equal 6")
    }
}

func TestDivide(t *testing.T) {
    if Divide(6, 3) != 2 {
        t.Error("Expected 6 / 3 to equal 2")
    }
}

使用gotestdox运行测试:

go test -v | gotestdox

输出可能如下:

✔ Add adds two numbers (0.00s)
✔ Subtract subtracts one number from another (0.00s)
✔ Multiply multiplies two numbers (0.00s)
✔ Divide divides one number by another (0.00s)

高级用法

  1. 只运行特定包的测试
go test ./pkg/... -v | gotestdox
  1. 生成JSON格式输出
go test -json | gotestdox
  1. 过滤通过的测试
go test -v | gotestdox | grep -v "✔"
  1. 与benchmark结合
go test -bench . -v | gotestdox

集成到CI/CD流程

gotestdox可以很好地集成到CI/CD流程中,例如在GitHub Actions中:

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - uses: actions/setup-go@v2
      with:
        go-version: '1.20'
    - run: go install github.com/bitfield/gotestdox@latest
    - run: go test -v ./... | gotestdox

自定义输出格式

gotestdox支持一些自定义选项:

  • -format:指定输出格式
  • -no-color:禁用彩色输出
  • -hide-passed:隐藏通过的测试

例如:

go test -v | gotestdox -format "Test result: {{.Status}} {{.Name}}"

总结

gotestdox是一个简单但强大的工具,它通过将测试名称转换为自然语言句子,使测试结果更易于理解。特别适合在团队协作环境中使用,让非技术人员也能理解测试结果。

相比原始的go test输出,gotestdox提供了:

  • 更清晰的测试描述
  • 更好的可读性
  • 直观的通过/失败标识
  • 可定制的输出格式

对于大型项目或需要清晰测试报告的场景,gotestdox是一个值得考虑的工具。

回到顶部