补充单元测试

This commit is contained in:
yumaojun03 2025-05-11 11:11:27 +08:00
parent 699b3b498b
commit a6dafaae71
9 changed files with 152 additions and 0 deletions

15
.vscode/launch.json vendored Normal file
View File

@ -0,0 +1,15 @@
{
// 使 IntelliSense
//
// 访: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}"
}
]
}

6
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,6 @@
{
"go.testEnvVars": {
"CONFIG_PATH": "application.yaml"
},
"go.testEnvFile": "${workspaceFolder}/etc/unit_test.env"
}

1
etc/unit_test.env Normal file
View File

@ -0,0 +1 @@
DB_HOST=127.0.0.1

3
go.mod
View File

@ -5,6 +5,7 @@ go 1.24.1
require (
github.com/caarlos0/env/v6 v6.10.1
github.com/gin-gonic/gin v1.10.0
github.com/stretchr/testify v1.9.0
gopkg.in/yaml.v3 v3.0.1
gorm.io/driver/mysql v1.5.7
gorm.io/gorm v1.26.0
@ -15,6 +16,7 @@ require (
github.com/bytedance/sonic/loader v0.1.1 // indirect
github.com/cloudwego/base64x v0.1.4 // indirect
github.com/cloudwego/iasm v0.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
@ -32,6 +34,7 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.12 // indirect
golang.org/x/arch v0.8.0 // indirect

View File

@ -0,0 +1,86 @@
# 单元测试
目的: 测试目标函数的功能是否正常
## 构建集成单元测试
目标函数: Add
站在使用者的角度进行单元测试: unittest.Add(1, 2) == 3
专门的包: 在同一个目录下允许有2个包: 一个包就是普通包 unittest, 和一个单元测试包 unittest_test
要求:
1. 单元测试包的文件名称: xxx_test.go, 必须以_test结尾才是一个合格的单元测试包
2. 必须是 xxx_test 包名
## 编写单元测试(run test)
编写单元测试代码: Add
1. 单元测试函数 必须以Test大头
```go
// 针对Add函数的单元测试
func TestAdd(t *testing.T) {
// 自己手动判断
// /usr/local/go/bin/go test -timeout 300s -run ^TestAdd$ 122.51.31.227/go-course/go18/skills/unit_test -v -count=1
// 如果没有打印日志, 配置vscode 打印单元测试的日志: -v -count=1
// 加上这串配置
// "go.testFlags": [
// "-v",
// "-count=1"
// ],
t.Log(unittest.Add(1, 2))
// 通过程序断言来判断
// if unittest.Add(1, 2) != 4 {
// t.Fatalf("1 + 2 != 4")
// }
// 专门的断言库
should := assert.New(t)
should.Equal(3, unittest.Add(1, 2))
}
```
## 单元测试调试(run debug)
1. 需要加断点, 必须加在有代码的位置
```sh
go/bin/dlv dap --listen=127.0.0.1:59905 --log-dest=3 from /Users/yumaojun/Projects/go-course/go18/skills/unit_test
DAP server listening at: 127.0.0.1:59905
Type 'dlv help' for list of commands.
```
2. 操作栏
![](./debug.png)
1. Continue: 继续,继续到下一个断点处
2. Step Over: 下一步,下一行
3. Step In: 进入到这行里面的 执行逻辑
4. Step Out: 从这行里面的执行路径出来
## 单元测试的配置
![](./debug_button.png)
单元测试如何读取外部配置, vscode 帮我们执行CLI
告诉vscode读取单元测试的配置 vscode 如何读取单元测试: 只有一个办法通过环境变量
1. 直接注入环境变量(Test Env Vars) : .vscode/settions.json
```json
{
"go.testEnvVars": {
"CONFIG_PATH": "application.yaml"
}
}
```
2. 将环境变量写入到一个文件中让vscdoe读取 (Test Env File)
```json
{
"go.testEnvFile": "${workspaceFolder}/etc/unit_test.env"
}
```

9
skills/unit_test/add.go Normal file
View File

@ -0,0 +1,9 @@
package unittest
import "fmt"
func Add(x, y int) int {
//
fmt.Println(y)
return x + x
}

View File

@ -0,0 +1,32 @@
package unittest_test
import (
"os"
"testing"
unittest "122.51.31.227/go-course/go18/skills/unit_test"
"github.com/stretchr/testify/assert"
)
// 针对Add函数的单元测试
func TestAdd(t *testing.T) {
t.Log(os.Getenv("CONFIG_PATH"))
t.Log(os.Getenv("DB_HOST"))
// 自己手动判断
// /usr/local/go/bin/go test -timeout 300s -run ^TestAdd$ 122.51.31.227/go-course/go18/skills/unit_test -v -count=1
// 如果没有打印日志, 配置vscode 打印单元测试的日志: -v -count=1
// 加上这串配置
// "go.testFlags": [
// "-v",
// "-count=1"
// ],
t.Log(unittest.Add(1, 2))
// 通过程序断言来判断
// if unittest.Add(1, 2) != 4 {
// t.Fatalf("1 + 2 != 4")
// }
// 专门的断言库
should := assert.New(t)
should.Equal(3, unittest.Add(1, 2))
}

BIN
skills/unit_test/debug.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB