Format and remove 11.3.md spaces

This commit is contained in:
vCaesar
2017-06-10 12:24:34 +08:00
parent 3337159fee
commit 81e1aa2537

View File

@@ -6,7 +6,7 @@ Go语言中自带有一个轻量级的测试框架`testing`和自带的`go test`
另外建议安装[gotests](https://github.com/cweill/gotests)插件自动生成测试代码:
```Go
go get -u -v github.com/cweill/gotests/...
go get -u -v github.com/cweill/gotests/...
```
@@ -19,19 +19,19 @@ Go语言中自带有一个轻量级的测试框架`testing`和自带的`go test`
```Go
package gotest
import (
"errors"
)
func Division(a, b float64) (float64, error) {
if b == 0 {
return 0, errors.New("除数不能为0")
}
return a / b, nil
package gotest
import (
"errors"
)
func Division(a, b float64) (float64, error) {
if b == 0 {
return 0, errors.New("除数不能为0")
}
return a / b, nil
}
```
@@ -49,23 +49,23 @@ Go语言中自带有一个轻量级的测试框架`testing`和自带的`go test`
```Go
package gotest
import (
"testing"
)
func Test_Division_1(t *testing.T) {
if i, e := Division(6, 2); i != 3 || e != nil { //try a unit test on function
t.Error("除法函数测试没通过") // 如果不是如预期的那么就报错
} else {
t.Log("第一个测试通过了") //记录一些你期望记录的信息
}
}
func Test_Division_2(t *testing.T) {
t.Error("就是不通过")
package gotest
import (
"testing"
)
func Test_Division_1(t *testing.T) {
if i, e := Division(6, 2); i != 3 || e != nil { //try a unit test on function
t.Error("除法函数测试没通过") // 如果不是如预期的那么就报错
} else {
t.Log("第一个测试通过了") //记录一些你期望记录的信息
}
}
func Test_Division_2(t *testing.T) {
t.Error("就是不通过")
}
```
@@ -91,13 +91,13 @@ Go语言中自带有一个轻量级的测试框架`testing`和自带的`go test`
```Go
func Test_Division_2(t *testing.T) {
if _, e := Division(6, 0); e == nil { //try a unit test on function
t.Error("Division did not work as expected.") // 如果不是如预期的那么就报错
} else {
t.Log("one test passed.", e) //记录一些你期望记录的信息
}
}
func Test_Division_2(t *testing.T) {
if _, e := Division(6, 0); e == nil { //try a unit test on function
t.Error("Division did not work as expected.") // 如果不是如预期的那么就报错
} else {
t.Log("one test passed.", e) //记录一些你期望记录的信息
}
}
```
然后我们执行`go test -v`,就显示如下信息,测试通过了:
@@ -116,7 +116,7 @@ Go语言中自带有一个轻量级的测试框架`testing`和自带的`go test`
- 压力测试用例必须遵循如下格式其中XXX可以是任意字母数字的组合但是首字母不能是小写字母
```Go
func BenchmarkXXX(b *testing.B) { ... }
func BenchmarkXXX(b *testing.B) { ... }
```
- `go test`不会默认执行压力测试的函数,如果要执行压力测试需要带上参数`-test.bench`,语法:`-test.bench="test_name_regex"`,例如`go test -test.bench=".*"`表示测试全部的压力测试函数
@@ -127,29 +127,29 @@ Go语言中自带有一个轻量级的测试框架`testing`和自带的`go test`
```Go
package gotest
import (
"testing"
)
func Benchmark_Division(b *testing.B) {
for i := 0; i < b.N; i++ { //use b.N for looping
Division(4, 5)
}
package gotest
import (
"testing"
)
func Benchmark_Division(b *testing.B) {
for i := 0; i < b.N; i++ { //use b.N for looping
Division(4, 5)
}
func Benchmark_TimeConsumingFunction(b *testing.B) {
b.StopTimer() //调用该函数停止压力测试的时间计数
//做一些初始化的工作,例如读取文件数据,数据库连接之类的,
//这样这些时间不影响我们测试函数本身的性能
b.StartTimer() //重新开始时间
for i := 0; i < b.N; i++ {
Division(4, 5)
}
}
func Benchmark_TimeConsumingFunction(b *testing.B) {
b.StopTimer() //调用该函数停止压力测试的时间计数
//做一些初始化的工作,例如读取文件数据,数据库连接之类的,
//这样这些时间不影响我们测试函数本身的性能
b.StartTimer() //重新开始时间
for i := 0; i < b.N; i++ {
Division(4, 5)
}
}
```