Skip to Main Content
Go程序设计语言
book

Go程序设计语言

by 艾伦A. A.多诺万, 布莱恩W. 柯尼汉
August 2021
Beginner to intermediate content levelBeginner to intermediate
450 pages
6h 1m
Chinese
Pearson
Content preview from Go程序设计语言

11.2 Test函数

每一个测试文件必须导入testing包。这些函数的函数签名如下:

功能测试函数必须以Test开头,可选的后缀名称必须以大写字母开头:

参数t提供了汇报测试失败和日志记录的功能。定义一个示例包gopl.io/ch11/word1,这个包包含一个函数IsPalindrome,用来判断一个字符串是否是回文字符串(这个函数在字符串是回文字符串的情况下对于每个字节检查了两次,后面会实现简短的版本)。

在同一个目录中,文件word_test.go包含了两个功能测试函数TestPalindrome和TestNonPalindrome。两个函数都检查isPalindrome是否针对单个输入参数给出了正确的结果,并且用t.Error来报错。

go test(或者go build)命令在不指定包参数的情况下,以当前目录所在的包为参数。可以用下面的命令来编译和运行测试: ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

C++语言导学(原书第2版)

C++语言导学(原书第2版)

本贾尼 斯特劳斯特鲁普
Go语言编程

Go语言编程

威廉·肯尼迪

Publisher Resources

ISBN: 9787111558422