go mod

为解决go模块间的相互引用,可以将go mod理解成j2ee中的pom文件。

创建mod

默认模块名

go mod init

指定模块名

go mod init <model-name>

引入其他模块

比如说需要引入 gin 模块。

  1. 首先需要进入模块所在目录,运行命令:
 go get -u github.com/gin-gonic/gin
  1. 查看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

go mod 与单元测试的更多相关文章

  1. [易学易懂系列|rustlang语言|零基础|快速入门|(15)|Unit Testing单元测试]

    [易学易懂系列|rustlang语言|零基础|快速入门|(15)] 实用知识 Unit Testing单元测试 我们知道,在现代软件开发的过程中,单元测试对软件的质量极及重要. 今天我们来看看Rust ...

  2. 单元测试及框架简介 --junit、jmock、mockito、powermock的简单使用

    转 单元测试及框架简介 --junit.jmock.mockito.powermock的简单使用 2013年08月28日 14:33:06 luvinahlc 阅读数:6413 标签: 测试工具单元测 ...

  3. 为代码编写稳定的单元测试 [Go]

    为代码编写稳定的单元测试 本文档配套代码仓库地址: https://github.com/liweiforeveryoung/curd_demo 配合 git checkout 出指定 commit ...

  4. golang单元测试一(简单函数测试)

    0.1.索引 https://blog.waterflow.link/articles/1663688140724 1.简介 单元测试是测试代码.组件和模块的单元函数.单元测试的目的是清除代码中的错误 ...

  5. Intellij idea添加单元测试工具

    1.idea 版本是14.0.0 ,默认带有Junit,但是不能自动生成单元测试,需要下载JunitGererator2.0插件 2.Settings -Plugins,下载 JunitGenerat ...

  6. Python的单元测试(二)

    title: Python的单元测试(二) date: 2015-03-04 19:08:20 categories: Python tags: [Python,单元测试] --- 在Python的单 ...

  7. Python的单元测试(一)

    title: Python的单元测试(一) author: 青南 date: 2015-02-27 22:50:47 categories: Python tags: [Python,单元测试] -- ...

  8. javascript单元测试框架mochajs详解

    关于单元测试的想法 对于一些比较重要的项目,每次更新代码之后总是要自己测好久,担心一旦上线出了问题影响的服务太多,此时就希望能有一个比较规范的测试流程.在github上看到牛逼的javascript开 ...

  9. 使用NUnit为游戏项目编写高质量单元测试的思考

    0x00 单元测试Pro & Con 最近尝试在我参与的游戏项目中引入TDD(测试驱动开发)的开发模式,因此单元测试便变得十分必要.这篇博客就来聊一聊这段时间的感悟和想法.由于游戏开发和传统软 ...

随机推荐

  1. [Unity3D]编辑器扩展之数组或List显示

    效果如下: 源码如下: using System.Collections.Generic; using UnityEditor; using UnityEngine; namespace XM.Edi ...

  2. 复习MintUI

    一.表单----复选框列表 1.<mt-checklist title="标题" options="['a','b','c']" #选项列表 v-mode ...

  3. mysql设置文档快捷写

    select distinct column_name,column_comment,column_type from information_schema.columns where table_n ...

  4. redis学习——day01_redis简介与安装

    一.Redis 简介 1.1 Redis是什么 REmote DIctionary Server(Redis) 是一个由Salvatore Sanfilippo写的key-value存储系统.Redi ...

  5. 单词数(hdu2072)

    这道题用到了(STL初步)集合:Set 的知识点.同时,也用到了stringstream 的知识点,还用到了getline(cin,line)的知识点. #include<iostream> ...

  6. COLA的扩展性使用和源码研究

    cola扩展点使用和设计初探 封装变化,可灵活应对程序的需求变化. 扩展点使用 步骤: 定义扩展点接口,类型可以是校验器,转换器,实体: 必须以ExtPt结尾,表示一个扩展点. 比如,我定义一个云枢的 ...

  7. SQL SERVER修改为sa登陆权限报错,233,18456接连出现【抓狂ing】

    [记录生活] 今天做作业需要修改sa权限,本人电脑没错误. 同样教程发给朋友,错误百出.... 话不多说,百度很多解决方法,但是都没有解决,贴出解决方法. 0.用Windows身份验证登录,执行SQL ...

  8. 用非常硬核的JAVA序列化手段实现对象流的持久化保存

    目录 背景 对象流的概念 对象流实例 引入一张组织结构图 定义组织架构图的类 类的完整结构 用对象流保存组织架构的对象信息 核心代码 用对象流读取文件并输出 核心代码 总结 背景 在OOP(面向对象编 ...

  9. 洛谷 P1352 没有上司的舞会 树形DP板子

    luogu传送门 题目描述: 某大学有n个职员,编号为1~n. 他们之间有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的直接上司. 现在有个周年庆宴会,宴会每邀请来一个职员都会 ...

  10. [PHP动态]0001.关于 PHP 7 你必须知道的五件事

    1.今年的计划表已出.PHP 7 时间表 RFC 投票一直通过, PHP 7 将在2015年10月发布.尽管有些延迟,但我们还是很高兴它在今年内发布.PHP 7 详细时间表由此查看. 2.PHP 要上 ...