golang BDD测试框架与实时刷新Web UI插件GoConvey的使用

Golang BDD测试框架与实时刷新Web UI插件GoConvey的使用

GoConvey简介

GoConvey是一个强大的Go测试工具,支持BDD(行为驱动开发)风格的测试编写方式。它直接与go test集成,提供浏览器实时刷新UI界面和彩色终端输出。

主要特性:

  • 直接与go test集成
  • 全自动Web UI界面(也支持原生Go测试)
  • 大量回归测试套件
  • 显示测试覆盖率
  • 可读性强的彩色控制台输出
  • 测试代码生成器
  • 桌面通知(可选)
  • 立即在Sublime Text中打开问题行

安装

$ go install github.com/smartystreets/goconvey

快速开始

编写测试示例

package package_name

import (
    "testing"
    . "github.com/smartystreets/goconvey/convey"
)

func TestSpec(t *testing.T) {
    // 只在顶层Convey调用中传入t
    Convey("Given some integer with a starting value", t, func() {
        x := 1

        Convey("When the integer is incremented", func() {
            x++

            Convey("The value should be greater by one", func() {
                So(x, ShouldEqual, 2)
            })
        })
    })
}

在浏览器中使用

  1. 在项目路径下启动GoConvey web服务器:
$ $GOPATH/bin/goconvey
  1. 在浏览器中访问:
http://localhost:8080

如果浏览器没有自动打开,可以手动点击上述链接。

GoConvey运行期间,测试结果会自动在浏览器窗口中更新。Web UI设计是响应式的,可以调整浏览器大小以适应你的编码环境。

在终端中使用

执行常规测试:

$ go test

或者包含详细输出:

$ go test -v

文档

更多详细使用文档请参考:

  • GoConvey wiki
  • GoDoc文档
  • 项目中的*_test.go测试文件

贡献者

GoConvey由SmartyStreets和多位贡献者共同开发维护。


更多关于golang BDD测试框架与实时刷新Web UI插件GoConvey的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html

1 回复

更多关于golang BDD测试框架与实时刷新Web UI插件GoConvey的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html


Golang BDD测试框架与GoConvey实时刷新Web UI插件

BDD测试框架与GoConvey简介

BDD(行为驱动开发)是一种敏捷软件开发方法,GoConvey是Go语言中一个流行的BDD风格测试框架,它提供了以下特性:

  1. 简洁的BDD语法
  2. 实时刷新的Web UI
  3. 自动测试执行
  4. 丰富的断言库
  5. 与标准testing包兼容

安装GoConvey

go get github.com/smartystreets/goconvey

基本使用示例

下面是一个简单的GoConvey测试示例:

package calculator

import (
	"testing"
	. "github.com/smartystreets/goconvey/convey"
)

func Add(a, b int) int {
	return a + b
}

func TestAdd(t *testing.T) {
	Convey("Given two integers", t, func() {
		a := 1
		b := 2

		Convey("When adding them together", func() {
			result := Add(a, b)

			Convey("Then the result should be the sum", func() {
				So(result, ShouldEqual, 3)
			})
		})
	})
}

GoConvey的Web UI

GoConvey最强大的功能之一是其实时Web界面:

  1. 启动Web UI:
goconvey
  1. 默认访问地址:http://localhost:8080

Web UI提供以下功能:

  • 实时测试结果展示
  • 代码覆盖率报告
  • 测试结果历史
  • 自动重新测试

常用断言方法

GoConvey提供了丰富的断言方法:

Convey("Assertion examples", t, func() {
    // 相等断言
    So(1, ShouldEqual, 1)
    So(1, ShouldNotEqual, 2)
    
    // 类型断言
    So(1, ShouldHaveSameTypeAs, 0)
    So("hello", ShouldNotHaveSameTypeAs, 0)
    
    // nil断言
    var p *int
    So(p, ShouldBeNil)
    So(&p, ShouldNotBeNil)
    
    // 布尔断言
    So(true, ShouldBeTrue)
    So(false, ShouldBeFalse)
    
    // 字符串断言
    So("hello", ShouldContainSubstring, "ell")
    So("hello", ShouldNotContainSubstring, "world")
    
    // 集合断言
    So([]int{1, 2, 3}, ShouldContain, 2)
    So([]int{1, 2, 3}, ShouldNotContain, 4)
    
    // 长度断言
    So([]int{1, 2, 3}, ShouldHaveLength, 3)
    So("abc", ShouldHaveLength, 3)
    
    // 近似相等(浮点数)
    So(1.0/3.0, ShouldAlmostEqual, 0.333, 0.001)
})

跳过测试与聚焦测试

// 跳过某个测试
Convey("This test will be skipped", t, Skip(func() {
    // 测试代码
}))

// 聚焦某个测试(只运行这个测试)
FocusConvey("Only this test will run", t, func() {
    // 测试代码
})

测试前后钩子

func TestWithHooks(t *testing.T) {
    Convey("Setup and teardown example", t, func() {
        // 测试前的设置
        db := setupDatabase()
        
        // 测试后的清理
        Reset(func() {
            teardownDatabase(db)
        })
        
        Convey("Test something with db", func() {
            // 测试代码
        })
    })
}

自定义断言

你可以创建自定义断言:

func ShouldBeDivisibleBy(actual interface{}, expected ...interface{}) string {
    if len(expected) != 1 {
        return "ShouldBeDivisibleBy requires exactly 1 expected value"
    }
    
    divisor, ok := expected[0].(int)
    if !ok {
        return "Divisor must be an integer"
    }
    
    value, ok := actual.(int)
    if !ok {
        return "Actual value must be an integer"
    }
    
    if value%divisor != 0 {
        return fmt.Sprintf("Expected %d to be divisible by %d", value, divisor)
    }
    
    return "" // 空字符串表示断言成功
}

// 使用自定义断言
Convey("Custom assertion example", t, func() {
    So(10, ShouldBeDivisibleBy, 2)
    So(10, ShouldBeDivisibleBy, 3) // 这会失败
})

与标准testing包的集成

GoConvey完全兼容标准testing包,你可以在同一个项目中混合使用:

func TestStandardTesting(t *testing.T) {
    result := Add(2, 3)
    if result != 5 {
        t.Errorf("Expected 5, got %d", result)
    }
}

func TestGoConveyTesting(t *testing.T) {
    Convey("When adding 2 and 3", t, func() {
        result := Add(2, 3)
        Convey("The result should be 5", func() {
            So(result, ShouldEqual, 5)
        })
    })
}

最佳实践

  1. 使用描述性的Convey语句,使其读起来像自然语言
  2. 保持测试层次结构扁平,避免过多嵌套
  3. 每个Convey块应该只测试一个行为
  4. 利用Web UI进行快速反馈
  5. 将复杂的断言逻辑提取为自定义断言

GoConvey通过其简洁的BDD语法和强大的Web UI,极大地提升了Go语言测试的体验和效率。

回到顶部