go install go.uber.org/mock/mockgen@latest
go mod init github.com/killlowkey/golang-testing
go get go.uber.org/mock/gomock
go get github.com/stretchr/testify
go get github.com/gin-gonic/gin
go get gorm.io/gorm
go get gorm.io/driver/mysql
go get github.com/DATA-DOG/go-sqlmock
- sqlmock:模拟数据库
- mock:mock 接口
- testify:断言框架
- mockery
mockgen -source ./store/user.go -destination ./store/user_mock.go -package store UserStore
mockgen -source ./biz/user.go -destination ./biz/user_mock.go -package biz UserService
- 运行所有测试:go test ./...
- 运行单个测试:go test ./biz -run TestUserService_GetUserById
- 运行指定包下测试:go test -v ./biz
- 运行指定测试文件(需要指定依赖):go test -v ./biz/user_test.go ./biz/user_mock.go ./biz/user.go
user_test.go 依赖了 user_mock.go 和 user.go,如果不指定依赖,会报错:undefined: UserServiceMock