From 5a7582908fb7f0478bd8b048437fe2d7d2abb0f8 Mon Sep 17 00:00:00 2001 From: 180909 <734461790@qq.com> Date: Wed, 7 Jul 2021 12:04:38 +0800 Subject: [PATCH] add go test --- languages/golang.go | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/languages/golang.go b/languages/golang.go index 921d840..030ee0e 100644 --- a/languages/golang.go +++ b/languages/golang.go @@ -42,7 +42,7 @@ GOSSAFUNC // 生成SSA.html文件, go mod init // 初始化当前文件夹,创建go.mod文件 go mod download // 下载依赖的module到本地 go mod tidy // 增加缺少的module,删除无用的module -go mod vendor // 将依赖复制到vendor下 +go mod vendor // 将依赖的包复制到vendor文件夹下 文件go.mod // 依赖列表和版本约束 文件go.sum // 记录module文件hash值,用于安全校验 @@ -116,6 +116,42 @@ const d = 1 // 常量 +/******************************************************************************* + * 测试 + ******************************************************************************/ +// 要测试的接口 +package testdemo + +func Adder(a int, b int) int { + return a + b +} + +// 编写test +package testdemo + +import "testing" + +func TestPass(t *testing.T) { + expected := 3 + if observed := Adder(1, 2); observed != expected { + t.Fatalf("Adder(1, 2) = %v, want %v", observed, expected) + } +} + +func TestSkip(t *testing.T) { + t.Skip("Skipping test") +} + +func TestFail(t *testing.T) { + expected := 3 + if observed := Adder(2, 2); observed != expected { + t.Fatalf("Adder(2, 2) = %v, want %v", observed, expected) + } +} + +// 运行命令 +go test -v mod/{package_name} + /******************************************************************************* * 数据类型 @@ -271,3 +307,4 @@ https://github.com/avelino/awesome-go ******************************************************************************/ https://github.com/a8m/go-lang-cheat-sheet https://github.com/LeCoupa/awesome-cheatsheets +https://github.com/jonasbn/go-test-demo \ No newline at end of file