go 单元测试testify
testify介绍
testify
用go实现的一个assert风格的测试框架,这个包提供了我们需要的断言的功能,提供了非常丰富的断言方法。
提供了测试suite、断言、mock三种功能。
官方文档:https://godoc.org/github.com/stretchr/testify
安装:
go get -u -v github.com/stretchr/testify
testify断言
有两种断言方式,区别是require的断言失败会直接导致程序结束,而assert虽然也标记为此case失败,但程序不会退出,而是继续往下执行。
- assert
- require
import ( "github.com/stretchr/testify/assert" "testing" ) //单元测试函数 func TestAddNum(t *testing.T) { result := addNum(100) assert.Equal(t, 5050, result) }
功能代码如下:
1 2 3 4 5 6 | func addNum(n int) (result int) { for i := 0; i <= n; i++ { result = result + i } return result } |
更多断言类型:
https://godoc.org/github.com/stretchr/testify/assert
https://godoc.org/github.com/stretchr/testify/require
testify suite
package untest // Basic imports import ( "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/suite" ) type ExampleTestSuite struct { suite.Suite VariableThatShouldStartAtFive int } // 每个测试用例执行前都会调用 func (suite *ExampleTestSuite) SetupTest() { suite.VariableThatShouldStartAtFive = 5 } //一个测试用例 func (suite *ExampleTestSuite) TestExample() { assert.Equal(suite.T(), 5, suite.VariableThatShouldStartAtFive) suite.Equal(5, suite.VariableThatShouldStartAtFive) } // In order for 'go test' to run this suite, we need to create // a normal test function and pass our suite to suite.Run func TestExampleTestSuite(t *testing.T) { suite.Run(t, new(ExampleTestSuite)) } // 每个测试用例执行后都会调用 func (suite *ExampleTestSuite) TearDownTest() { }
Table Driven Test
通过构造结构体切片进行table driven test,这里传入常规参数的情况,代码实现如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | func TestSqrt(t *testing.T) { testcases := [] struct { desc string input float64 expect float64 }{ { desc: "zero" , input: 0, expect: 0, }, { desc: "one" , input: 1, expect: 1, }, { desc: "a very small rational number" , input: 0.00000000000000000000000001, expect: 0.0, }, { desc: "rational number result: 2.56" , input: 2.56, expect: 1.6, }, { desc: "irrational number result: 2" , input: 2, expect: 1.414213562, }, } for _, ts := range testcases { got := Sqrt(ts.input) erro := got - ts.expect require.True(t, erro < 0.000000001 && erro > -0.000000001, ts.desc) } } |
功能代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | // Sqrt calculate the square root of a non-negative float64 // number with max error of 10^-9. For simplicity, we don't // discard the part with is smaller than 10^-9. func Sqrt(x float64) float64 { if x < 0 { panic( "cannot be negative" ) } if x == 0 { return 0 } a := x / 2 b := (a + 2) / 2 erro := a - b for erro >= 0.000000001 || erro <= -0.000000001 { a = b b = (b + x/b) / 2 erro = a - b } return b } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?