@@ -6,7 +6,7 @@ Go语言中自带有一个轻量级的测试框架`testing`和自带的`go test`
6
6
另外建议安装[ gotests] ( https://github.com/cweill/gotests ) 插件自动生成测试代码:
7
7
8
8
``` Go
9
- go get -u -v github.com /cweill/gotests/...
9
+ go get -u -v github.com /cweill/gotests/...
10
10
11
11
```
12
12
@@ -19,19 +19,19 @@ Go语言中自带有一个轻量级的测试框架`testing`和自带的`go test`
19
19
20
20
``` Go
21
21
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" )
34
31
}
32
+
33
+ return a / b, nil
34
+ }
35
35
36
36
```
37
37
@@ -49,23 +49,23 @@ Go语言中自带有一个轻量级的测试框架`testing`和自带的`go test`
49
49
50
50
``` Go
51
51
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 (" 第一个测试通过了" ) // 记录一些你期望记录的信息
68
63
}
64
+ }
65
+
66
+ func Test_Division_2 (t *testing .T ) {
67
+ t.Error (" 就是不通过" )
68
+ }
69
69
70
70
```
71
71
@@ -91,13 +91,13 @@ Go语言中自带有一个轻量级的测试框架`testing`和自带的`go test`
91
91
92
92
``` Go
93
93
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
+ }
101
101
```
102
102
然后我们执行`go test -v`,就显示如下信息,测试通过了:
103
103
@@ -116,7 +116,7 @@ Go语言中自带有一个轻量级的测试框架`testing`和自带的`go test`
116
116
- 压力测试用例必须遵循如下格式,其中XXX可以是任意字母数字的组合,但是首字母不能是小写字母
117
117
118
118
``` Go
119
- func BenchmarkXXX (b *testing .B ) { ... }
119
+ func BenchmarkXXX (b *testing .B ) { ... }
120
120
```
121
121
122
122
- ` go test ` 不会默认执行压力测试的函数,如果要执行压力测试需要带上参数` -test.bench ` ,语法:` -test.bench="test_name_regex" ` ,例如` go test -test.bench=".*" ` 表示测试全部的压力测试函数
@@ -127,29 +127,29 @@ Go语言中自带有一个轻量级的测试框架`testing`和自带的`go test`
127
127
128
128
``` Go
129
129
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 )
140
139
}
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 )
152
151
}
152
+ }
153
153
154
154
```
155
155
0 commit comments