the-way-to-go_ZH_CN
Search…
Introduction
前言
第一部分:学习 Go 语言
第1章:Go 语言的起源,发展与普及
第2章:安装与运行环境
第3章:编辑器、集成开发环境与其它工具
第二部分:语言的核心结构与技术
第4章:基本结构和基本数据类型
第5章:控制结构
第6章:函数(function)
第7章:数组与切片
第8章:Map
第9章:包(package)
第10章:结构(struct)与方法(method)
第11章:接口(interface)与反射(reflection)
第三部分:Go 高级编程
第12章:读写数据
第13章:错误处理与测试
错误处理
运行时异常和 panic
从 panic 中恢复(Recover)
自定义包中的错误处理和 panicking
一种用闭包处理错误的模式
启动外部命令和程序
Go 中的单元测试和基准测试
测试的具体例子
用(测试数据)表驱动测试
性能调试:分析并优化 Go 程序
第14章:协程(goroutine)与通道(channel)
第15章:网络、模版与网页应用
第四部分:实际应用
第16章:常见的陷阱与错误
第17章:模式
第18章:出于性能考虑的实用代码片段
第19章:构建一个完整的应用程序
第20章:Go 语言在 Google App Engine 的使用
第21章:实际部署案例
附录
A 代码引用
B 有趣的 Go 引用
C 代码示例列表
D 书中的包引用
E 书中的工具引用
F 常见问题解答
G 习题答案
H 参考文献
Powered By
GitBook
用(测试数据)表驱动测试
编写测试代码时,一个较好的办法是把测试的输入数据和期望的结果写在一起组成一个数据表:表中的每条记录都是一个含有输入和期望值的完整测试用例,有时还可以结合像测试名字这样的额外信息来让测试输出更多的信息。
实际测试时简单迭代表中的每条记录,并执行必要的测试。这在练习
13.4
中有具体的应用。
可以抽象为下面的代码段:
var
tests
=
[]
struct
{
// Test table
in
string
out
string
}{
{
"in1"
,
"exp1"
},
{
"in2"
,
"exp2"
},
{
"in3"
,
"exp3"
},
...
}
func
TestFunction
(
t
*
testing
.
T
)
{
for
i
,
tt
:=
range
tests
{
s
:=
FuncToBeTested
(
tt
.
in
)
if
s
!=
tt
.
out
{
t
.
Errorf
(
"%d. %q => %q, wanted: %q"
,
i
,
tt
.
in
,
s
,
tt
.
out
)
}
}
}
如果大部分函数都可以写成这种形式,那么写一个帮助函数 verify 对实际测试会很有帮助:
func
verify
(
t
*
testing
.
T
,
testnum
int
,
testcase
,
input
,
output
,
expected
string
)
{
if
expected
!=
output
{
t
.
Errorf
(
"%d. %s with input = %s: output %s != %s"
,
testnum
,
testcase
,
input
,
output
,
expected
)
}
}
TestFunction 则变为:
func
TestFunction
(
t
*
testing
.
T
)
{
for
i
,
tt
:=
range
tests
{
s
:=
FuncToBeTested
(
tt
.
in
)
verify
(
t
,
i
,
“FuncToBeTested
:
“
,
tt
.
in
,
s
,
tt
.
out
)
}
}
链接
目录
上一节:
测试的具体例子
下一节:
性能调试:分析并优化 Go 程序
Previous
测试的具体例子
Next
性能调试:分析并优化 Go 程序
Last modified
3yr ago
Copy link