GO——单元测试(test)

go test用来做什么

做单元测试,测试函数是否符合预期

go test在哪个包

testing

如何使用

参考: https://geektutu.com/post/quick-go-test.html

my_func.go中的Add方法为例

  • 在同一个文件夹下添加my_func_test.go文件
    • 测试文件以_test.go为结尾
    • 里面的测试方法以Test开头,但是不一定是要跟方法名,随意
package main

import (
	"fmt"
	"testing"
)

func TestAdd(t *testing.T) {
	fmt.Println(Add(1,3))
	if res := Add(1, 2); res != 3 {
		t.Errorf("1 + 2 应该等于 3, 但是结果是%d", res)
	}
}

func TestAdd2(t *testing.T) {
	fmt.Println(add(11,3))
}

func TestAdd3(t *testing.T) {
	fmt.Println(Add(121,3))
}
函数首字母大写

函数首字母大写表示可以在另一个包中访问

  • 参考:https://www.cnblogs.com/rickiyang/p/11074174.html

相关推荐

  1. GO——单元测试test

    2024-01-16 21:48:04       37 阅读
  2. 自动化单元测试 Automatic Test Generation

    2024-01-16 21:48:04       23 阅读
  3. 软件测试:C++ Google Test单元测试框架GTest

    2024-01-16 21:48:04       22 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-16 21:48:04       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-16 21:48:04       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-16 21:48:04       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-16 21:48:04       20 阅读

热门阅读

  1. SpringCloud openFeign 之 获取被调用服务名

    2024-01-16 21:48:04       33 阅读
  2. 算法通关村第十一关—理解位运算的规则(青铜)

    2024-01-16 21:48:04       29 阅读
  3. 装箱问题(C语言)

    2024-01-16 21:48:04       34 阅读
  4. 模拟和高精度简单总结(依靠洛谷题单)

    2024-01-16 21:48:04       30 阅读
  5. ABC210(A-C)

    2024-01-16 21:48:04       31 阅读
  6. 【归并排序】264. 丑数 II

    2024-01-16 21:48:04       32 阅读
  7. Linux网络编程---socket编程接口接口函数

    2024-01-16 21:48:04       31 阅读
  8. Opencv基础用法学习2

    2024-01-16 21:48:04       31 阅读
  9. git 清除不受git控制的文件夹和文件

    2024-01-16 21:48:04       33 阅读