Go测试文件应与被测代码同包且以_test.go结尾,支持单元测试、基准测试和表驱动测试,可通过接口模拟依赖,使用t.Error报告错误,Benchmark函数评估性能,结合build tags管理测试类型。

Go语言的
testing
编写Go测试用例的关键在于使用
testing
t.Error
t.Errorf
t.Fatal
t.Fatalf
_test.go
package mypackage
import "testing"
func Add(a, b int) int {
return a + b
}
func TestAdd(t *testing.T) {
sum := Add(2, 3)
if sum != 5 {
t.Errorf("Add(2, 3) returned %d, expected 5", sum)
}
}这段代码展示了一个简单的加法函数及其对应的测试用例。
TestAdd
Add
t.Errorf
组织良好的测试文件对于项目的可维护性和可测试性至关重要。一种常见的做法是将测试文件放在与被测试代码相同的包中,并使用
_test.go
mypackage
mypackage_test.go
立即学习“go语言免费学习笔记(深入)”;
此外,复杂的项目可能需要更细粒度的测试组织。你可以创建子目录来存放特定模块的测试,或者使用build tags来控制哪些测试在不同的环境下运行。
// +build integration
package mypackage
import "testing"
func TestIntegration(t *testing.T) {
// 仅在执行集成测试时运行
t.Log("Running integration test")
}在这个例子中,
TestIntegration
go test -tags=integration
testing
Benchmark
*testing.B
package mypackage
import "testing"
func fibonacci(n int) int {
if n <= 1 {
return n
}
return fibonacci(n-1) + fibonacci(n-2)
}
func BenchmarkFibonacci(b *testing.B) {
for i := 0; i < b.N; i++ {
fibonacci(20) // 测试计算斐波那契数列第20项的性能
}
}要运行基准测试,可以使用
go test -bench=.
表驱动测试是一种常用的测试模式,它允许你使用一组输入和预期输出的表格来测试一个函数。这种方法可以减少代码重复,并使测试更加清晰和易于维护。
package mypackage
import "testing"
func TestAbs(t *testing.T) {
testCases := []struct {
input int
expected int
}{
{input: -1, expected: 1},
{input: 0, expected: 0},
{input: 1, expected: 1},
{input: -99, expected: 99},
}
for _, tc := range testCases {
actual := abs(tc.input)
if actual != tc.expected {
t.Errorf("abs(%d) = %d, expected %d", tc.input, actual, tc.expected)
}
}
}
func abs(n int) int {
if n < 0 {
return -n
}
return n
}在这个例子中,
testCases
在单元测试中,经常需要模拟外部依赖项,例如数据库连接、网络请求等。Go语言提供了多种模拟依赖项的方法,包括使用接口、函数变量和第三方mocking库。
使用接口是一种常见的做法。你可以定义一个接口来描述依赖项的行为,然后在测试中使用一个实现了该接口的mock对象。
package mypackage
import "testing"
type DataStore interface {
Get(key string) (string, error)
}
type MyService struct {
db DataStore
}
func (s *MyService) GetData(key string) (string, error) {
return s.db.Get(key)
}
type MockDataStore struct {
data map[string]string
}
func (m *MockDataStore) Get(key string) (string, error) {
if val, ok := m.data[key]; ok {
return val, nil
}
return "", nil
}
func TestGetData(t *testing.T) {
mockDB := &MockDataStore{data: map[string]string{"testkey": "testvalue"}}
service := &MyService{db: mockDB}
val, err := service.GetData("testkey")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if val != "testvalue" {
t.Errorf("expected 'testvalue', got '%s'", val)
}
}在这个例子中,
DataStore
MockDataStore
MyService
以上就是Golang使用testing包编写测试用例示例的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号