当前位置: 首页 > news >正文

《使用Gin框架构建分布式应用》阅读笔记:p272-p306

《用Gin框架构建分布式应用》学习第15天,p272-p306总结,总35页。

一、技术总结

1.TDD(test-driven development)

虽然经常看到TDD这个属于,从本人的工作经历看,实际开发中用得相对较少。

2.unitest(单元测试)

go语言开发中,使用testify进行单元测试开发。

(1)创建测试文件

测试文件以xxx_test.go命名,与xxx.go在同一目录下。示例:main.go和mian_test.go在同一目录下。

(2)编写测试函数

测试函数必须Test作为前缀,后面跟被测试函数名,示例:被测试函数名称为IndexHandler,测试函数名称为TestIndexHandler。

main.go:

// main.go
package mainimport ("github.com/gin-gonic/gin""net/http"
)func IndexHandler(c *gin.Context) {c.JSON(http.StatusOK, gin.H{"message": "hello world"})
}func SetupServer() *gin.Engine {router := gin.Default()router.GET("/", IndexHandler)return router
}
func main() {err := SetupServer().Run()if err != nil {return}
}

main_test.go:

package mainimport ("github.com/stretchr/testify/assert""io""net/http""net/http/httptest""testing"
)func TestIndexHandler(t *testing.T) {// 不适用 testify// 注意,因为`{"message":"hello world"}`是字符串,所以冒号后面如果有空格,在判断相等的时候也会有影响// mockUserResp := `{"message":"hello world"}`//// ts := httptest.NewServer(SetupServer())// defer ts.Close()//// res, err := http.Get(ts.URL + "/")// if err != nil {// 	t.Fatalf("Expected no error: got %v", err)// }//// defer res.Body.Close()//// if res.StatusCode != http.StatusOK {// 	t.Fatalf("Expected status code 200: got %v", res.StatusCode)// }//// // ioutil.ReadAll 已不推荐使用// // responseData, err := ioutil.ReadAll(res.Body)// responseData, err := io.ReadAll(res.Body)// if string(responseData) != mockUserResp {// 	t.Fatalf("Expected hello world message: got %v", string(responseData))// }// 使用 testifymockUserResp := `{"message": "hello world"}`ts := httptest.NewServer(SetupServer())defer ts.Close()res, err := http.Get(ts.URL + "/")defer res.Body.Close()assert.Nil(t, err)assert.Equal(t, http.StatusOK, res.StatusCode)responseData, err := io.ReadAll(res.Body)assert.Equal(t, mockUserResp, string(responseData))
}

(3)执行测试

go test

3.coverage(测试覆盖率)

p282, Test coverage describes how much of a package’s code is exercised by running the package’s tests.

4.integration test(集成测试)

integration test就是多个功能一起测试。

5.security test(安全测试)

go语言开发中,使用gosec进行安全测试。

6.postman

书上介绍了postman的collection, environment, scripts的使用,基本属于工作中常用到的操作。当然,postman本身也不复杂。

go语言开发中,使用

7.吐槽系列

// chapter 01router := gin.Default()// chapter 07r := gin.Default()

作者在chapter 01用的名称是router, 那么在chapter 07也应该用这个,而不是r,保持字段名称的一致性!想起本人在实际工作中遇到的一个项目,其中表示“设备”的名称就用了三个:eqp, equip, equipment,但其实都是指同一个东西,这无形中会导致一些问题:(1)阅读代码的人会有疑问,这三个表示的是同一个东西吗?(2)写代码的时候得思考,用的是哪个名称。

二、英语总结

无。

三、其它

今天没有什么想说的。

四、参考资料

1. 编程

(1) Mohamed Labouardy,《Building Distributed Applications in Gin》:https://book.douban.com/subject/35610349

2. 英语

(1) Etymology Dictionary:https://www.etymonline.com

(2) Cambridge Dictionary:https://dictionary.cambridge.org

在这里插入图片描述

欢迎搜索及关注:编程人(a_codists)

http://www.lryc.cn/news/474021.html

相关文章:

  • 【搜索引擎】俄罗斯搜索引擎yandex
  • 加密源代码|html代码如何加密保护?3分钟学会4种源代码加密妙招,代码人必看
  • Jetson Orin NX平台自研载板 IMX477相机掉线问题调试记录
  • spring-boot(整合mybatisplus、及常见注解)
  • 深度学习:yolov3的使用--建立模型
  • 关于我、重生到500年前凭借C语言改变世界科技vlog.13——深入理解指针(3)
  • 每日算法一练:剑指offer——数组篇(6)
  • 【环境搭建】Apache ZooKeeper 3.8.4 Stable
  • 算法练习——双指针
  • vue中el-table显示文本过长提示
  • JS 字符串拼接并去重
  • opencv 图像预处理
  • SAP B1 功能模块字段介绍 - 价格清单(下)
  • 传智杯 第六届-复赛-D
  • Java - 数组实现大顶堆
  • ifuse挂载后,在python代码中访问iOS沙盒目录获取app日志
  • Windows WSL环境下安装 pytorch +ROCM 支持AMD显卡
  • uniapp中skymap.html(8100端口)提示未登录的排查与解决方法
  • 训练模型时梯度出现NAN或者INF(禁用amp的不同level)
  • Maven核心概念
  • Sonatype Nexus 部署手册
  • TLV320AIC3104IRHBR 数据手册 一款低功耗立体声音频编解码器 立体声耳机放大器芯片麦克风
  • (8)结构体、共用体和枚举类型数据
  • Jedis操作和springboot整合redis
  • 基于AI大模型的复杂扫描件PDF信息提取与规整
  • 为什么https先非对称加密,然后对称加密?
  • 【Coroutines】Full Understanding of Kotlinx.Corutines Framework
  • Python面向对象,实现图片处理案例,支持:高斯模糊、Canny边缘检测、反转边缘图像、生成手绘效果、调亮度......等等
  • SOLID - 依赖倒置原则(Dependency Inversion Principle)
  • 【.NET 8 实战--孢子记账--从单体到微服务】--需求拆分与规划