翻译13.8

This commit is contained in:
dake
2015-11-08 16:48:48 +08:00
parent 4207e28a11
commit e33618d70c
3 changed files with 137 additions and 0 deletions

View File

@@ -1,2 +1,137 @@
# 13.8 测试的具体例子 # 13.8 测试的具体例子
在练习 11.2 中你写了一个叫 main_oddeven.go 的程序用来测试前 100 个整数是否是偶数。这个函数在包 even 中。
下面是一种可能的方案:
示例 13.7 [even_main.go](examples/chapter_13/even_main/even_main.go)
```go
package main
import (
"fmt"
"even/even"
)
func main() {
for i:=0; i<=100; i++ {
fmt.Printf("Is the integer %d even? %v\n", i, even.Even(i))
}
}
```
上面使用了 even.go 中的 even 包:
示例 13.8 [even/even.go](examples/chapter_13/even/even.go)
```go
package even
func Even(i int) bool { // Exported function
return i%2 == 0
}
func Odd(i int) bool { // Exported function
return i%2 != 0
}
```
在 even 包的路径下,我们创建一个名为 oddeven_test.go 的测试程序:
示例 13.9 [even/oddeven_test.go](examples/chapter_13/even/oddeven_test.go)
```go
package even
import "testing"
func TestEven(t *testing.T) {
if !Even(10) {
t.Log(" 10 must be even!")
t.Fail()
}
if Even(7) {
t.Log(" 7 is not even!")
t.Fail()
}
}
func TestOdd(t *testing.T) {
if !Odd(11) {
t.Log(" 11 must be odd!")
t.Fail()
}
if Odd(10) {
t.Log(" 10 is not odd!")
t.Fail()
}
}
```
由于测试需要具体的输入用例且不可能测试到所有的用例(非常像一个无穷的数),所以我们必须对要使用的测试用例思考再三。
至少应该包括:
- 正常的用例
- 反面的用例(错误的输入,如用负数或字母代替数字,没有输入等)
- 边界检查用例(如果参数的取值范围是 0 到 1000检查 0 和 1000 的情况)
可以直接执行 go install 安装 even 或者创建一个 以下内容的 Makefile
```bash
include $(GOROOT)/src/Make.inc
TARG=even
GOFILES=\
even.go\
include $(GOROOT)/src/Make.pkg
```
然后执行 make或 gomake命令来构建归档文件 even.a
测试代码不能在 GOFILES 参数中引用因为我们不希望生成的程序中有测试代码。如果包含了测试代码go test 会给出错误提示go test 会生成一个单独的包含测试代码的 _test 程序。
现在我们可以用命令:```go test```(或 ```make test```)来测试 even 包。
因为示例 13.5 中的测试函数不会调用 t.Log 和 t.Fail所以会得到一个 PASS 的结果。在这个简单例子中一切都正常执行。
为了看到失败时的输出,把函数 TestEven 改为:
```go
func TestEven(t *testing.T) {
if Even(10) {
t.Log(“Everything OK: 10 is even, just a test to see failed output!”)
t.Fail()
}
}
```
现在会调用 t.Log 和 t.Fail得到的结果如下
```go
--- FAIL: even.TestEven (0.00 seconds)
Everything OK: 10 is even, just a test to see failed output!
FAIL
```
**练习 13.4**[string_reverse_test.go](exercises/chapter_13/string_reverse_test.go)
为练习 7.11 [string_reverse.go](exercises/chapter_7/string_reverse.go) 写一个单元测试。
把 string_reverse 放到自己的包 strev 中,只包含一个可导出函数 reverse。
实现并测试它。
## 链接
- [目录](directory.md)
- 上一节:[Go 中的单元测试和基准测试](13.7.md)
- 下一节:[用(测试数据)表驱动测试](13.9.md)

1
eBook/13.9.md Normal file
View File

@@ -0,0 +1 @@
# 13.9 用(测试数据)表驱动测试

View File

@@ -133,6 +133,7 @@
- 13.6 [启动外部命令和程序](13.6.md) - 13.6 [启动外部命令和程序](13.6.md)
- 13.7 [Go 中的单元测试和基准测试](13.7.md) - 13.7 [Go 中的单元测试和基准测试](13.7.md)
- 13.8 [测试的具体例子](13.8.md) - 13.8 [测试的具体例子](13.8.md)
- 13.9 [用(测试数据)表驱动测试](13.9.md)
- 第14章goroutine 与 channel - 第14章goroutine 与 channel
- 第15章网络、模版与网页应用 - 第15章网络、模版与网页应用