Skip to content

Commit 81e1aa2

Browse files
committed
Format and remove 11.3.md spaces
1 parent 3337159 commit 81e1aa2

File tree

1 file changed

+58
-58
lines changed

1 file changed

+58
-58
lines changed

zh/11.3.md

+58-58
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Go语言中自带有一个轻量级的测试框架`testing`和自带的`go test`
66
另外建议安装[gotests](https://github.com/cweill/gotests)插件自动生成测试代码:
77

88
```Go
9-
go get -u -v github.com/cweill/gotests/...
9+
go get -u -v github.com/cweill/gotests/...
1010

1111
```
1212

@@ -19,19 +19,19 @@ Go语言中自带有一个轻量级的测试框架`testing`和自带的`go test`
1919

2020
```Go
2121

22-
package gotest
23-
24-
import (
25-
"errors"
26-
)
27-
28-
func Division(a, b float64) (float64, error) {
29-
if b == 0 {
30-
return 0, errors.New("除数不能为0")
31-
}
32-
33-
return a / b, nil
22+
package gotest
23+
24+
import (
25+
"errors"
26+
)
27+
28+
func Division(a, b float64) (float64, error) {
29+
if b == 0 {
30+
return 0, errors.New("除数不能为0")
3431
}
32+
33+
return a / b, nil
34+
}
3535

3636
```
3737

@@ -49,23 +49,23 @@ Go语言中自带有一个轻量级的测试框架`testing`和自带的`go test`
4949

5050
```Go
5151

52-
package gotest
53-
54-
import (
55-
"testing"
56-
)
57-
58-
func Test_Division_1(t *testing.T) {
59-
if i, e := Division(6, 2); i != 3 || e != nil { //try a unit test on function
60-
t.Error("除法函数测试没通过") // 如果不是如预期的那么就报错
61-
} else {
62-
t.Log("第一个测试通过了") //记录一些你期望记录的信息
63-
}
64-
}
65-
66-
func Test_Division_2(t *testing.T) {
67-
t.Error("就是不通过")
52+
package gotest
53+
54+
import (
55+
"testing"
56+
)
57+
58+
func Test_Division_1(t *testing.T) {
59+
if i, e := Division(6, 2); i != 3 || e != nil { //try a unit test on function
60+
t.Error("除法函数测试没通过") // 如果不是如预期的那么就报错
61+
} else {
62+
t.Log("第一个测试通过了") //记录一些你期望记录的信息
6863
}
64+
}
65+
66+
func Test_Division_2(t *testing.T) {
67+
t.Error("就是不通过")
68+
}
6969

7070
```
7171

@@ -91,13 +91,13 @@ Go语言中自带有一个轻量级的测试框架`testing`和自带的`go test`
9191

9292
```Go
9393

94-
func Test_Division_2(t *testing.T) {
95-
if _, e := Division(6, 0); e == nil { //try a unit test on function
96-
t.Error("Division did not work as expected.") // 如果不是如预期的那么就报错
97-
} else {
98-
t.Log("one test passed.", e) //记录一些你期望记录的信息
99-
}
100-
}
94+
func Test_Division_2(t *testing.T) {
95+
if _, e := Division(6, 0); e == nil { //try a unit test on function
96+
t.Error("Division did not work as expected.") // 如果不是如预期的那么就报错
97+
} else {
98+
t.Log("one test passed.", e) //记录一些你期望记录的信息
99+
}
100+
}
101101
```
102102
然后我们执行`go test -v`,就显示如下信息,测试通过了:
103103

@@ -116,7 +116,7 @@ Go语言中自带有一个轻量级的测试框架`testing`和自带的`go test`
116116
- 压力测试用例必须遵循如下格式,其中XXX可以是任意字母数字的组合,但是首字母不能是小写字母
117117

118118
```Go
119-
func BenchmarkXXX(b *testing.B) { ... }
119+
func BenchmarkXXX(b *testing.B) { ... }
120120
```
121121

122122
- `go test`不会默认执行压力测试的函数,如果要执行压力测试需要带上参数`-test.bench`,语法:`-test.bench="test_name_regex"`,例如`go test -test.bench=".*"`表示测试全部的压力测试函数
@@ -127,29 +127,29 @@ Go语言中自带有一个轻量级的测试框架`testing`和自带的`go test`
127127

128128
```Go
129129

130-
package gotest
131-
132-
import (
133-
"testing"
134-
)
135-
136-
func Benchmark_Division(b *testing.B) {
137-
for i := 0; i < b.N; i++ { //use b.N for looping
138-
Division(4, 5)
139-
}
130+
package gotest
131+
132+
import (
133+
"testing"
134+
)
135+
136+
func Benchmark_Division(b *testing.B) {
137+
for i := 0; i < b.N; i++ { //use b.N for looping
138+
Division(4, 5)
140139
}
141-
142-
func Benchmark_TimeConsumingFunction(b *testing.B) {
143-
b.StopTimer() //调用该函数停止压力测试的时间计数
144-
145-
//做一些初始化的工作,例如读取文件数据,数据库连接之类的,
146-
//这样这些时间不影响我们测试函数本身的性能
147-
148-
b.StartTimer() //重新开始时间
149-
for i := 0; i < b.N; i++ {
150-
Division(4, 5)
151-
}
140+
}
141+
142+
func Benchmark_TimeConsumingFunction(b *testing.B) {
143+
b.StopTimer() //调用该函数停止压力测试的时间计数
144+
145+
//做一些初始化的工作,例如读取文件数据,数据库连接之类的,
146+
//这样这些时间不影响我们测试函数本身的性能
147+
148+
b.StartTimer() //重新开始时间
149+
for i := 0; i < b.N; i++ {
150+
Division(4, 5)
152151
}
152+
}
153153

154154
```
155155

0 commit comments

Comments
 (0)