提交 f05351d8 编写于 作者: 写代码的明哥's avatar 写代码的明哥

Add:新增文章<单元测试>

上级 4b8264b6
6.1 go test 工具命令详解
========================
接下来几篇文章,我将介绍 下 Golang 中有关测试相关的一些文章。
在学习如何编写测试代码之前,需要先了解一下Go 提供的测试工具 :go test
go test
本身可以携带很多的参数,熟悉这些参数,可以让我们的测试过程更加方便。
下面就根据场景来解释一下常用的几个参数:
(由于下一节才会讲到如何编写测试代码,所以请好结合下一篇文章进行学习)
1、运行整个项目的测试文件
.. code:: shell
$ go test
PASS
ok _/home/wangbm/golang/math 0.003s
2、只运行某个测试文件( math_test.go, math.go
是一对,缺一不可,前后顺序可对调)
.. code:: shell
$ go test math_test.go math.go
ok command-line-arguments 0.002s
3、加 ``-v`` 查看详细的结果
.. code:: shell
$ go test math_test.go math.go
=== RUN TestAdd
TestAdd: main_test.go:22: the result is ok
TestAdd: main_test.go:22: the result is ok
TestAdd: main_test.go:22: the result is ok
TestAdd: main_test.go:22: the result is ok
TestAdd: main_test.go:22: the result is ok
--- PASS: TestAdd (0.00s)
PASS
ok command-line-arguments 0.003s
4、只测试某个函数,-run 支持正则,如下例子中
TestAdd,如果还有一个测试函数为 TestAdd02,那么它也会被运行。
.. code:: shell
$ go test -v -run="TestAdd"
=== RUN TestAdd
TestAdd: math_test.go:22: the result is ok
TestAdd: math_test.go:22: the result is ok
TestAdd: math_test.go:22: the result is ok
TestAdd: math_test.go:22: the result is ok
TestAdd: math_test.go:22: the result is ok
--- PASS: TestAdd (0.00s)
PASS
ok _/home/wangbm/golang/math 0.003s
5、生成 test 的二进制文件:加 ``-c`` 参数
.. code:: shell
$ go test -v -run="TestAdd" -c
$
$ ls -l
total 3208
-rw-r--r-- 1 root root 95 May 25 20:56 math.go
-rwxr-xr-x 1 root root 3272760 May 25 21:00 math.test
-rw-r--r-- 1 root root 525 May 25 20:56 math_test.go
6、执行这个 test 测试文件:加 ``-o`` 参数
.. code:: shell
$ go test -v -o math.test
=== RUN TestAdd
TestAdd: math_test.go:22: the result is ok
TestAdd: math_test.go:22: the result is ok
TestAdd: math_test.go:22: the result is ok
TestAdd: math_test.go:22: the result is ok
TestAdd: math_test.go:22: the result is ok
--- PASS: TestAdd (0.00s)
=== RUN TestAum
TestAum: math_test.go:30: 6
--- PASS: TestAum (0.00s)
PASS
ok _/home/wangbm/golang/math 0.002s
7、只测试安装/重新安装 依赖包,而不运行代码:加 ``-i`` 参数
.. code:: shell
# 这里没有输出
$ go test -i
|image0|
.. |image0| image:: http://image.python-online.cn/image-20200320125724880.png
6.2 如何进行单元测试?
======================
在计算机编程中,单元测试(英语:Unit
Testing)又称为模块测试,是针对程序模块(软件设计的最小单位)来进行正确性检验的测试工作。
程序单元是应用的最小可测试部件,一般来说都是对某一个函数方法进行测试,以尽可能的保证没有问题或者问题可被我们预知。为了达到这个目的,我们可以使用各种手段、逻辑,模拟不同的场景进行测试。
那么我们如何在写 Golang 代码时,进行单元测试呢?
由于实在是太简单了,我这里直接上例子吧
1. 单元测试
-----------
准备两个 Go 文件
**math.go**
.. code:: go
package math
func Add(x,y int) int {
return x+y
}
**math_test.go**
.. code:: go
package math
import "testing"
func TestAdd(t *testing.T) {
t.Log(Add(1, 2))
}
然后使用 ``go test`` 工具去执行
.. code:: shell
$ go test .
ok _/home/wangbm/golang/math 0.003s
从上面这个例子中,可以总结中几点 Go 语言测试框架要遵循的规则
1. 单元测试代码的
go文件必须以\ ``_test.go``\ 结尾,而前面最好是被测试的文件名(不过并不是强制的),比如要测试
math.go 测试文件名就为 math_test.go
2. 单元测试的函数名必须以\ ``Test``\ 开头,后面直接跟要测试的函数名,比如要测试
Add函数,单元测试的函数名就得是 TestAdd
3. 单元测试的函数必须接收一个指向\ ``testing.T``\ 类型的指针,并且不能返回任何值。
2. 表组测试
-----------
Add(1, 2) 是一次单元测试的场景,而 Add(2, 4) Add(3, 6)
又是另外两种单元测试的场景。
对于多种输入场景的测试,我们可以同时放在 TestAdd
里进行测试,这种测试方法就是表组测试。
修改 **math_test.go** 如下
.. code:: go
package math
import "testing"
func TestAdd(t *testing.T) {
sum:=Add(1,2)
if sum == 3 {
t.Log("the result is ok")
} else {
t.Fatal("the result is wrong")
}
sum=Add(2,4)
if sum == 6 {
t.Log("the result is ok")
} else {
t.Fatal("the result is wrong")
}
}
执行 ``go test``
.. code:: shell
$ go test . -v
=== RUN TestAdd
TestAdd: math_test.go:8: the result is ok
TestAdd: math_test.go:15: the result is ok
--- PASS: TestAdd (0.00s)
PASS
ok _/home/wangbm/golang/math 0.003s
稍微如果输入的场景实在太多(比如下面用的五组输入),用上面的方法,可能需要写很多重复的代码,这时候可以利用
**表格测试法**
.. code:: go
package math
import "testing"
type TestTable struct {
xarg int
yarg int
}
func TestAdd(t *testing.T){
tables := []TestTable{
{1,2},
{2,4},
{4,8},
{5,10},
{6,12},
}
for _, table := range tables{
result := Add(table.xarg, table.yarg)
if result == (table.xarg + table.yarg){
t.Log("the result is ok")
} else {
t.Fatal("the result is wrong")
}
}
}
执行 ``go test``
.. code:: shell
$ go test . -v
=== RUN TestAdd
TestAdd: math_test.go:22: the result is ok
TestAdd: math_test.go:22: the result is ok
TestAdd: math_test.go:22: the result is ok
TestAdd: math_test.go:22: the result is ok
TestAdd: math_test.go:22: the result is ok
--- PASS: TestAdd (0.00s)
PASS
ok _/home/wangbm/golang/math 0.002s
|image0|
.. |image0| image:: http://image.python-online.cn/image-20200320125724880.png
6.1 整理了 20 个学习 Go 语言的精品网站
======================================
本文写自我的知乎回答:https://www.zhihu.com/question/23486344/answer/1204644361
--------------
先说下我个人的情况吧,我本是个 Python
重度使用者,年初出于工作的需要,开始学习 Golang
,学到现在已经有4个多月的时间了。
期间为了记录自己的学习过程,同时给正想学习 Go
语言的同学一些帮助,我将自己的所学写成一个教程,发布在我的 Github
上(https://github.com/iswbm/GolangCodingTime)以及我的个人网站上。
**网站链接** 是:http://golang.iswbm.com/
目前已经更新了 30 多篇,覆盖了 Golang 90%
的入门必学知识点,内容我还在不断完善更新中,而且\ **我还规划实战板块,比如
Web 开发,爬虫程序的编写等**\ ,敬请期待。
如果你还在入门阶段,或者准备入门,那么建议把我的个人网站收藏一下,完全可以当做
wiki 查阅。对于基础知识我讲的还是比较全面,细致的。
|image0|
写 《\ `Go
编程时光 <https://golang.iswbm.com/>`__\ 》这个系列教程,为了让文章出错的概率降到最低,我如履薄冰,我每写一个知识点,就要翻阅大量的其他人的教程、博客,以及官方文档。
也因此,\ **我收藏了很多学习 Go 语言的网站和资源。**
俗话说,授人以鱼,不如授人以渔。今天就把这些资源全部分享给大家。
接下来,废话不多说了,直接上干货。
下面网站比较多,我把将它们分成 五大类,你可以根据自己的实际
情况进行选择:
1. 入门阶段
2. Web开发
3. 进阶阶段
4. 工具使用
5. 技术社区
6. 源码阅读
另外,为了方便大家,我制作了张思维导图,这下清晰多了吧(hhhhh
|image1|
1. 入门阶段
-----------
Go语言入门教程
~~~~~~~~~~~~~~
C语言中文网的系列教程,对新手非常友好的一个入门教程,很多内容我也是从这里学习的,推荐大家看看。
**网站链接**\ :http://c.biancheng.net/golang/
|image2|
菜鸟教程 - Go 系列
~~~~~~~~~~~~~~~~~~
菜鸟教程(RUNOOB)是一个一站式编程入门学习网站,想当年我学习 Python
时也经常 在这里同样也有 Go 语言的系列。
**网站链接**\ :https://www.runoob.com/go/go-tutorial.html
|image3|
易百教程 - Go 系列
~~~~~~~~~~~~~~~~~~
和菜鸟教程,W3Cschool一样,易百教程也是一个一站式的编程语言入门教程网站,目前为止,共发布了
157 个系列教程,每个教程都很适合新手学习。
**网站链接**\ :https://www.yiibai.com/go/
|image4|
W3Cshcool - Go 系列
~~~~~~~~~~~~~~~~~~~
W3Cschool
也是一个专业的编程入门学习及技术文档查询应用,提供包括\ `HTML <https://baike.baidu.com/item/HTML>`__\ ,\ `CSS <https://baike.baidu.com/item/CSS/5457>`__\ ,\ `Javascript <https://baike.baidu.com/item/Javascript>`__\ ,\ `jQuery <https://baike.baidu.com/item/jQuery>`__\ ,\ `C <https://baike.baidu.com/item/C/7252092>`__\ ,\ `PHP <https://baike.baidu.com/item/PHP/9337>`__\ ,\ `Java <https://baike.baidu.com/item/Java/85979>`__\ ,\ `Python <https://baike.baidu.com/item/Python>`__\ ,\ `Sql <https://baike.baidu.com/item/Sql>`__\ ,MySQL,Golang
等编程语言和开源技术的在线教程及使用手册,是类似国外w3schools的学习社区及菜鸟编程平台。
**网站链接**\ :https://www.w3cschool.cn/go/
|image5|
TopGoer 教程
~~~~~~~~~~~~
这个网站是我在搜索资料的时候偶然发现的,从目录可以看出内容非常多,网络编程、并发编程,很多主流的
Web 框架都有。
**网站链接**\ :http://www.topgoer.com/
|image6|
The Way to Go
~~~~~~~~~~~~~
《the way to go》的
中文版本,目前还在持续更新中,截止今天(2020/04/30)翻译进度已经达到
96.92%。
**网站链接**\ :https://learnku.com/docs/the-way-to-go
|image7|
极客学院(Go 教程)
~~~~~~~~~~~~~~~~~~~
极客学院,这个网站里收集了很多领域的编程语言相关的教程,在这里依然可以找到不少
Go 的教程。
我不知道这些内容是否获得授权,但对于要学习需求的同学来说,确实是一个不错的网站。
**网站链接**\ :https://wiki.jikexueyuan.com/list/go/
|image8|
Go 语言简明教程
~~~~~~~~~~~~~~~
**网站链接**\ :https://geektutu.com/post/quick-golang.html
|image9|
Go by Example
~~~~~~~~~~~~~
这个网站的 idea 非常好,网站里收集了很多的小例子,来帮助你快速了解 Go
语言里那些基础的知识点。不过要深入理解这些知识,还是需要你学习更多的资料,并加以练习。
**网站链接**\ :https://gobyexample-cn.github.io/
|image10|
2. Web开发
----------
gin 中文文档
~~~~~~~~~~~~
**网站链接**\ :https://github.com/skyhee/gin-doc-cn
beego 开发文档
~~~~~~~~~~~~~~
**网站链接**\ :https://www.kancloud.cn/hello123/beego/126086
echo 中文文档
~~~~~~~~~~~~~
**网站链接**\ :https://www.bookstack.cn/read/go-echo/README.md
Iris 中文文档
~~~~~~~~~~~~~
**网站链接**\ :https://studyiris.com/doc/
Buffalo 中文文档
~~~~~~~~~~~~~~~~
**网站链接**\ :https://learnku.com/docs/buffalo-doc-cn
Revel 中文文档
~~~~~~~~~~~~~~
**网站链接**\ :https://www.bookstack.cn/books/gorevel-manual-zh
3. 进阶学习
-----------
跟煎鱼学Go
~~~~~~~~~~
煎鱼大佬的博客,跟着学习了很多的干货。良心推荐一波。
**网站链接**\ :https://eddycjy.gitbook.io/golang/
|image11|
Go语言圣经
~~~~~~~~~~
本书由《C程序设计语言》
的作者Kernighan和谷歌公司Go团队合作编写而成,是关于Go语言编程的权威著作。
**网站链接**\ :https://books.studygolang.com/gopl-zh/
|image12|
mojotv 进阶系列
~~~~~~~~~~~~~~~
一个致力于 Go 语言编程知识分享的高质量网站,里面有大量关于 Go
进阶的文章,此外还有 Python、 Docker ,K8S
,算法的文章。我试图在网站上找出作者的相关信息,不过并没有什么收获。
**网站链接**\ :https://mojotv.cn/404#Golang
|image13|
Go 语言高级编程
~~~~~~~~~~~~~~~
**网站链接**\ :https://chai2010.gitbooks.io/advanced-go-programming-book/content/
|image14|
4. 工具使用
-----------
Go 命令教程
~~~~~~~~~~~
go 的命令非常多,如果想系统的学习,推荐郝林的 Go 命令教程,非常的全。
**网站链接**\ :
1、 https://hyper0x.github.io/go_command_tutorial/#/
2、https://wiki.jikexueyuan.com/project/go-command-tutorial/0.0.html
|image15|
Uber 编程规范
~~~~~~~~~~~~~
Uber 开源了其公司内部使用的《\ `Go
语言编程规范 <https://github.com/uber-go/guide/blob/master/style.md>`__\ 》。该指南是为了使代码库更易于管理,同时让工程师有效地使用
Go 语言特性。文档中详细描述了在 Uber 编写 Go
代码的各种注意事项,包括具体的“Dos and Don’ts of writing Go code at
Uber”,也就是 Go 代码应该怎样写、不该怎样写。
**网站链接**
英文原文:https://github.com/uber-go/guide/blob/master/style.md
中文译文:https://www.infoq.cn/article/G6c95VyU5telNXXCC9yO
|image16|
Go Walker
~~~~~~~~~
Go Walker 是一个可以在线生成并浏览 `Go <https://golang.org/>`__ 项目 API
文档的 Web 服务器,目前已支持包括 **GitHub** 等代码托管平台。
|image17|
CTOLib 码库
~~~~~~~~~~~
像是一个收集中心,收藏着网络上的各种教程资源 ,里面也可以看到 Go
的很多工具,教程。
**网站链接**\ :https://www.ctolib.com/go/categories/go-guide.html
|image18|
5. 技术社区
-----------
GoCN
~~~~
**网站链接**\ :https://gocn.vip/
|image19|
Go 语言中文网
~~~~~~~~~~~~~
Go 语言爱好者的聚集地,是目前最大的Go 语言中文社区,关于 Go 语言
你所需要的,不需要的,都可以在这里找得到,包括
Go历史版本的下载,各种高质量的电子书资源,各种大牛写的高质量文章等。
**网站链接**\ :https://studygolang.com/
|image20|
6. 源码学习
-----------
Go 夜读
~~~~~~~
这是一个由众多资深 Gopher 组织的开源项目,主要是对 Go
源码进行解读,并约定每周四晚上进行技术分享。
**网站链接**\ :https://talkgo.org/
|image21|
Go 语言原本
~~~~~~~~~~~
**网站链接**\ :https://changkun.de/golang/
|image22|
Go 语言设计与实现
~~~~~~~~~~~~~~~~~
目前还在更新中,写得有点深,进阶的可以看看
**网站链接**\ :https://draveness.me/golang/
|image23|
能翻到这里的,一定是真爱了,本以为 Go
语言还处于不温不火的状态,没想到收集整理一下,资料还挺多的。
--------------
|image24|
.. |image0| image:: http://image.iswbm.com/20200430112024.png
.. |image1| image:: http://image.iswbm.com/20200506192746.png
.. |image2| image:: http://image.iswbm.com/20200430102243.png
.. |image3| image:: http://image.iswbm.com/20200430170656.png
.. |image4| image:: http://image.iswbm.com/20200430172511.png
.. |image5| image:: http://image.iswbm.com/20200430171029.png
.. |image6| image:: http://image.iswbm.com/20200430102508.png
.. |image7| image:: http://image.iswbm.com/20200430165344.png
.. |image8| image:: http://image.iswbm.com/20200430104324.png
.. |image9| image:: http://image.iswbm.com/20200430174507.png
.. |image10| image:: http://image.iswbm.com/20200430112319.png
.. |image11| image:: http://image.iswbm.com/20200430105116.png
.. |image12| image:: http://image.iswbm.com/20200430100755.png
.. |image13| image:: http://image.iswbm.com/20200430095544.png
.. |image14| image:: http://image.iswbm.com/20200430175818.png
.. |image15| image:: http://image.iswbm.com/20200430102821.png
.. |image16| image:: http://image.iswbm.com/20200430113756.png
.. |image17| image:: http://image.iswbm.com/20200430170054.png
.. |image18| image:: http://image.iswbm.com/20200430174109.png
.. |image19| image:: http://image.iswbm.com/20200506192127.png
.. |image20| image:: http://image.iswbm.com/20200430134207.png
.. |image21| image:: http://image.iswbm.com/20200430174216.png
.. |image22| image:: http://image.iswbm.com/20200506191803.png
.. |image23| image:: http://image.iswbm.com/20200506191632.png
.. |image24| image:: http://image.python-online.cn/image-20200320125724880.png
6.2 配置高颜值 IDE:Goland
==========================
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册