go mod 与单元测试
2024-10-09 07:15:39
go mod
为解决go模块间的相互引用,可以将go mod理解成j2ee中的pom文件。
创建mod
默认模块名
go mod init
指定模块名
go mod init <model-name>
引入其他模块
比如说需要引入 gin
模块。
- 首先需要进入模块所在目录,运行命令:
go get -u github.com/gin-gonic/gin
- 查看
mod
文件
可以看到会将相关的依赖拉取下来,这样就能在自己的模块中使用这些包了。
module org.golearn.basic
go 1.14
require (
github.com/gin-gonic/gin v1.6.3 // indirect
github.com/go-playground/validator/v10 v10.3.0 // indirect
github.com/golang/protobuf v1.4.2 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
golang.org/x/sys v0.0.0-20200602225109-6fdc65e7d980 // indirect
google.golang.org/protobuf v1.24.0 // indirect
gopkg.in/yaml.v2 v2.3.0 // indirect
)
go 单元测试
创建源文件和测试文件
├── test
│ ├── calc.go
│ └── calc_test.go
calc.go
package main
func Add(a, b int) int{
return a + b
}
func Sub(a, b int) int {
return a - b
}
func Mul(a, b int) int {
return a * b
}
calc_test.go
package main
import "testing"
func TestAdd(t *testing.T) {
t.Helper()
if ans := Add(1, 2); ans != 3 {
t.Errorf("1 + 2 expected be 3, but %d got", ans)
}
}
func TestMul(t *testing.T) {
//if ans := Mul(10, 20); ans != 100 {
// t.Errorf("10 * 20 expected be 600, but %d got", ans)
//}
// 子测试
// 正数
t.Run("正数相乘结果", func(t *testing.T) {
if ans := Mul(2, 3); ans != 6{
t.Errorf("10 * 20 expected be 6, but %d got", ans)
}
})
// 子测试-负数
t.Run("负数相乘结果", func(t *testing.T) {
if ans := Mul(-20, 3); ans != -60 {
t.Errorf("10 * 20 expected be -60, but %d got", ans)
}
})
}
func TestSub(t *testing.T) {
if ans := Sub(10, 1); ans != 9 {
t.Errorf("10 - 1 expected be 9, but %d got ", ans)
}
}
// 多个子测试场景
func TestMul2(t *testing.T) {
mulCases := []struct{
Name string
Num1, Num2, Expect int
}{
{"pos", 1, 2, 2},
{"neg", 1, -2, -2},
{"zero", 0, -2, 10},
}
for _, r := range mulCases {
t.Run(r.Name, func(t *testing.T) {
t.Helper()
if ans := Mul(r.Num1, r.Num2); ans != r.Expect {
t.Fatalf("%d * %d expected %d, but %d got",
r.Num1, r.Num2, r.Expect, ans)
}
})
}
}
//
/*
测试用例
1. 在相同的包路径中创建 xxx_test.go文件
2. 编写测试用例
3. 运行测试用例,查看测试用例中所有函数的运行结果 go test -v
*/
运行测试用例
go test -v
=== RUN TestAdd
--- PASS: TestAdd (0.00s)
=== RUN TestMul
=== RUN TestMul/正数相乘结果
=== RUN TestMul/负数相乘结果
--- PASS: TestMul (0.00s)
--- PASS: TestMul/正数相乘结果 (0.00s)
--- PASS: TestMul/负数相乘结果 (0.00s)
=== RUN TestSub
--- PASS: TestSub (0.00s)
=== RUN TestMul2
=== RUN TestMul2/pos
=== RUN TestMul2/neg
=== RUN TestMul2/zero
TestMul2/zero: calc_test.go:55: 0 * -2 expected 10, but 0 got
--- FAIL: TestMul2 (0.00s)
--- PASS: TestMul2/pos (0.00s)
--- PASS: TestMul2/neg (0.00s)
--- FAIL: TestMul2/zero (0.00s)
FAIL
exit status 1
FAIL org.golearn.basic/test 0.005s
最新文章
- 详解MVC设计模式
- bzoj3514Codechef MARCH14 GERALD07加强版
- linux下安装mongodb(php版本5.3)
- K650D安装黑苹果
- plist解析, 简易实现.
- Oracle EBS-SQL (PO-14):检查报价单与成本对比.sql
- c语言正则表达式
- tab栏切换,内容为不断实时刷新数据的vue实现方法
- vimgrep 搜索总结
- C# 默认访问修饰符
- 1231: ykc买零食
- 关于QT Graphics View开启OpenGL渲染后复选框、微调框等无法正常显示的问题
- PythonStudy——字符串扩展方法 String extension method
- 十七、springcloud(三)服务的注册与调用
- 2019.02.21 bzoj2300: [HAOI2011]防线修建(set+凸包)
- FDQuery多表更新生成sql语句的问题
- psutil库
- java多线程 -- volatile 关键字 内存 可见性
- No module named pip.req
- iOS 小技巧
热门文章
- Vue+Vuex实现自动登录 升级版
- axios请求拦截器(修改Data上的参数 ==>;把data上的参数转为FormData)
- spark机器学习从0到1机器学习工作流 (十一)
- AJAX二
- HTML新特性--canvas绘图-文本
- Maven整合JaCoCo和Sonar,看看你的测试写够了没
- 基于 abp vNext 和 .NET Core 开发博客项目 - 统一规范API,包装返回模型
- XXX_ProductCRUD的项目结构与配置文件
- Kivy中显示汉字的问题
- Kivy中ActionBar控件的使用