补充包管理
This commit is contained in:
parent
8e24e19003
commit
f3cb6b013d
16
day06/userapp/.vscode/launch.json
vendored
Normal file
16
day06/userapp/.vscode/launch.json
vendored
Normal file
@ -0,0 +1,16 @@
|
||||
{
|
||||
// 使用 IntelliSense 了解相关属性。
|
||||
// 悬停以查看现有属性的描述。
|
||||
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"name": "User App",
|
||||
"type": "go",
|
||||
"request": "launch",
|
||||
"mode": "auto",
|
||||
// workspaceFolder 是 VS Code 中的一个变量,表示当前打开的工作区的根目录。它会根据你打开的文件夹自动解析为相应的路径。
|
||||
"program": "${workspaceFolder}/main.go"
|
||||
}
|
||||
]
|
||||
}
|
||||
@ -1,3 +1,6 @@
|
||||
// run package tests: 运行当前包下的所有测试函数 (*_test.go)
|
||||
// /usr/local/go/bin/go test -test.fullpath=true -timeout 300s -coverprofile=/var/folders/51/dnfr1hzd53x03k3fxnnyd4qc0000gn/T/vscode-golzp26T/go-code-cover userapp/models -v -count=1
|
||||
// run file tests: 运行当前文件下的所有测试函数(user_test)
|
||||
package models_test
|
||||
|
||||
// models 内部测试(可以测试私有方法)
|
||||
@ -17,6 +20,34 @@ func TestUserCheckPasswordHash(t *testing.T) {
|
||||
user := models.NewUser("张三", "zhangsan@example.com", 25, "password123")
|
||||
user.ID = 1
|
||||
|
||||
user.SetPasswordHash("password123")
|
||||
|
||||
// userapp/models/user_test.go:15: true
|
||||
// 想要看到单元测试的日志, 需要在运行测试时加上 -v 参数,例如 go test -v
|
||||
// /usr/local/go/bin/go test -test.fullpath=true -timeout 300s -run ^TestUserCheckPasswordHash$ userapp/models -v -count=1
|
||||
// -v: 显示详细的测试输出,包括测试函数的日志信息。
|
||||
// -count=1: 禁止测试结果的缓存,确保每次运行测试时都会执行测试函数,而不是使用之前的测试结果。
|
||||
// -v -count=1 你们是没有的,vscode 设置(go.testFlags):
|
||||
// "go.testFlags": [
|
||||
// "-v",
|
||||
// "-count=1"
|
||||
// ],
|
||||
|
||||
if !user.CheckPasswordHash("password1231") {
|
||||
// t.Error()
|
||||
// t.Fatal() 会立即停止测试函数的执行,并标记测试为失败,而 t.Error() 只是记录错误信息,但继续执行测试函数。
|
||||
t.Fatal("密码验证失败")
|
||||
}
|
||||
// fmt.Println()
|
||||
t.Log("验证成功")
|
||||
}
|
||||
|
||||
func TestUserCheckPasswordHash2(t *testing.T) {
|
||||
user := models.NewUser("张三", "zhangsan@example.com", 25, "password123")
|
||||
user.ID = 1
|
||||
|
||||
user.SetPasswordHash("password123")
|
||||
|
||||
// userapp/models/user_test.go:15: true
|
||||
// 想要看到单元测试的日志, 需要在运行测试时加上 -v 参数,例如 go test -v
|
||||
// /usr/local/go/bin/go test -test.fullpath=true -timeout 300s -run ^TestUserCheckPasswordHash$ userapp/models -v -count=1
|
||||
|
||||
5
day06/userapp/utils/add.go
Normal file
5
day06/userapp/utils/add.go
Normal file
@ -0,0 +1,5 @@
|
||||
package utils
|
||||
|
||||
func Add(a, b int) int {
|
||||
return a + b
|
||||
}
|
||||
48
day06/userapp/utils/add_test.go
Normal file
48
day06/userapp/utils/add_test.go
Normal file
@ -0,0 +1,48 @@
|
||||
// Copyright 2024 The Cockroach Authors.
|
||||
//
|
||||
// Use of this software is governed by the Business Source License
|
||||
// included in the file licenses/BSL.txt.
|
||||
//
|
||||
// As of the Change Date specified in that file, in accordance with
|
||||
// the Business Source License, use of this software will be governed
|
||||
// by the Apache License, Version 2.0, included in the file
|
||||
// licenses/APL.txt.
|
||||
//
|
||||
// Package utils contains utility functions.
|
||||
|
||||
package utils_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"userapp/utils"
|
||||
)
|
||||
|
||||
// ✅ 表驱动测试:简洁清晰
|
||||
func TestAdd(t *testing.T) {
|
||||
// 定义测试用例表
|
||||
// 单测 一次使用,使用匿名结构体定义测试用例,适合简单的测试场景
|
||||
tests := []struct {
|
||||
name string
|
||||
a int
|
||||
b int
|
||||
want int
|
||||
}{
|
||||
{"正数相加", 1, 2, 3},
|
||||
{"负数相加", -1, 1, 0},
|
||||
{"零值相加", 0, 0, 0},
|
||||
{"大数相加", 100, 200, 300},
|
||||
}
|
||||
|
||||
// 测试初始化和回收
|
||||
|
||||
// 遍历执行测试, 并发执行的
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := utils.Add(tt.a, tt.b)
|
||||
if got != tt.want {
|
||||
t.Errorf("Add(%d, %d) = %d; want %d",
|
||||
tt.a, tt.b, got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user