-
Notifications
You must be signed in to change notification settings - Fork 8
/
testing-and-benchmarking.html
286 lines (229 loc) · 15.1 KB
/
testing-and-benchmarking.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Go by Example 中文版: 单元测试和基准测试</title>
<link rel=stylesheet href="site.css">
</head>
<script>
onkeydown = (e) => {
if (e.key == "ArrowLeft") {
window.location.href = 'temporary-files-and-directories';
}
if (e.key == "ArrowRight") {
window.location.href = 'command-line-arguments';
}
}
</script>
<body>
<div class="example" id="testing-and-benchmarking">
<h2><a href="./">Go by Example 中文版</a>: 单元测试和基准测试</h2>
<table>
<tr>
<td class="docs">
<p>想要写出好的 Go 程序,单元测试是很重要的一部分。
<code>testing</code> 包为提供了编写单元测试所需的工具,写好单元测试后,我们可以通过 <code>go test</code> 命令运行测试。</p>
</td>
<td class="code empty leading">
</td>
</tr>
<tr>
<td class="docs">
<p>为方便演示,例子的代码位于 <code>main</code> 包,实际上,单元测试的代码可以位于任何包下。
测试代码通常与需要被测试的代码位于同一个包下。</p>
</td>
<td class="code empty leading">
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<a href="https://play.studygolang.com/p/6_USpbeJGKP"><img title="Run code" src="play.png" class="run" /></a><img title="Copy code" src="clipboard.png" class="copy" />
<pre class="chroma"><span class="kn">package</span> <span class="nx">main</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"><span class="kn">import</span> <span class="p">(</span>
<span class="s">"fmt"</span>
<span class="s">"testing"</span>
<span class="p">)</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>我们要测试下面这个简单的函数——返回最小值。
一般地,需要被测试的代码应该在类似于 <code>intutils.go</code> 的文件下,
其对应的测试文件应该被命名为 <code>intutils_test.go</code>。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="kd">func</span> <span class="nf">IntMin</span><span class="p">(</span><span class="nx">a</span><span class="p">,</span> <span class="nx">b</span> <span class="kt">int</span><span class="p">)</span> <span class="kt">int</span> <span class="p">{</span>
<span class="k">if</span> <span class="nx">a</span> <span class="p"><</span> <span class="nx">b</span> <span class="p">{</span>
<span class="k">return</span> <span class="nx">a</span>
<span class="p">}</span>
<span class="k">return</span> <span class="nx">b</span>
<span class="p">}</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>通常编写一个名称以 <code>Test</code> 开头的函数来创建测试。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="kd">func</span> <span class="nf">TestIntMinBasic</span><span class="p">(</span><span class="nx">t</span> <span class="o">*</span><span class="nx">testing</span><span class="p">.</span><span class="nx">T</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">ans</span> <span class="o">:=</span> <span class="nf">IntMin</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="o">-</span><span class="mi">2</span><span class="p">)</span>
<span class="k">if</span> <span class="nx">ans</span> <span class="o">!=</span> <span class="o">-</span><span class="mi">2</span> <span class="p">{</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p><code>t.Error*</code> 会报告测试失败的信息,然后继续运行测试。
<code>t.Fatal*</code> 会报告测试失败的信息,然后立即终止测试。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="nx">t</span><span class="p">.</span><span class="nf">Errorf</span><span class="p">(</span><span class="s">"IntMin(2, -2) = %d; want -2"</span><span class="p">,</span> <span class="nx">ans</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">}</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>单元测试可以重复,所以会经常使用 <em>表驱动</em> 风格编写单元测试,
表中列出了输入数据,预期输出,使用循环,遍历并执行测试逻辑。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="kd">func</span> <span class="nf">TestIntMinTableDriven</span><span class="p">(</span><span class="nx">t</span> <span class="o">*</span><span class="nx">testing</span><span class="p">.</span><span class="nx">T</span><span class="p">)</span> <span class="p">{</span>
<span class="kd">var</span> <span class="nx">tests</span> <span class="p">=</span> <span class="p">[]</span><span class="kd">struct</span> <span class="p">{</span>
<span class="nx">a</span><span class="p">,</span> <span class="nx">b</span> <span class="kt">int</span>
<span class="nx">want</span> <span class="kt">int</span>
<span class="p">}{</span>
<span class="p">{</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">},</span>
<span class="p">{</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">0</span><span class="p">},</span>
<span class="p">{</span><span class="mi">2</span><span class="p">,</span> <span class="o">-</span><span class="mi">2</span><span class="p">,</span> <span class="o">-</span><span class="mi">2</span><span class="p">},</span>
<span class="p">{</span><span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">},</span>
<span class="p">{</span><span class="o">-</span><span class="mi">1</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="o">-</span><span class="mi">1</span><span class="p">},</span>
<span class="p">}</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>t.Run 可以运行一个 “subtests” 子测试,一个子测试对应表中一行数据。
运行 <code>go test -v</code> 时,他们会分开显示。</p>
</td>
<td class="code leading">
<pre class="chroma"> <span class="k">for</span> <span class="nx">_</span><span class="p">,</span> <span class="nx">tt</span> <span class="o">:=</span> <span class="k">range</span> <span class="nx">tests</span> <span class="p">{</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
</td>
<td class="code leading">
<pre class="chroma"> <span class="nx">testname</span> <span class="o">:=</span> <span class="nx">fmt</span><span class="p">.</span><span class="nf">Sprintf</span><span class="p">(</span><span class="s">"%d,%d"</span><span class="p">,</span> <span class="nx">tt</span><span class="p">.</span><span class="nx">a</span><span class="p">,</span> <span class="nx">tt</span><span class="p">.</span><span class="nx">b</span><span class="p">)</span>
<span class="nx">t</span><span class="p">.</span><span class="nf">Run</span><span class="p">(</span><span class="nx">testname</span><span class="p">,</span> <span class="kd">func</span><span class="p">(</span><span class="nx">t</span> <span class="o">*</span><span class="nx">testing</span><span class="p">.</span><span class="nx">T</span><span class="p">)</span> <span class="p">{</span>
<span class="nx">ans</span> <span class="o">:=</span> <span class="nf">IntMin</span><span class="p">(</span><span class="nx">tt</span><span class="p">.</span><span class="nx">a</span><span class="p">,</span> <span class="nx">tt</span><span class="p">.</span><span class="nx">b</span><span class="p">)</span>
<span class="k">if</span> <span class="nx">ans</span> <span class="o">!=</span> <span class="nx">tt</span><span class="p">.</span><span class="nx">want</span> <span class="p">{</span>
<span class="nx">t</span><span class="p">.</span><span class="nf">Errorf</span><span class="p">(</span><span class="s">"got %d, want %d"</span><span class="p">,</span> <span class="nx">ans</span><span class="p">,</span> <span class="nx">tt</span><span class="p">.</span><span class="nx">want</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">})</span>
<span class="p">}</span>
<span class="p">}</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>基准测试通常在”_test.go”文件中,并以 <code>Benchmark</code> 开头命名。
<code>testing</code> 运行器多次执行每个基准测试函数,并在每次运行时增加 <code>b.N</code>,
直到它收集到精确的测量值。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="kd">func</span> <span class="nf">BenchmarkIntMin</span><span class="p">(</span><span class="nx">b</span> <span class="o">*</span><span class="nx">testing</span><span class="p">.</span><span class="nx">B</span><span class="p">)</span> <span class="p">{</span>
</pre>
</td>
</tr>
<tr>
<td class="docs">
<p>通常,基准测试运行一个函数,我们在一个 <code>b.N</code> 次的循环内进行基准测试。</p>
</td>
<td class="code">
<pre class="chroma">
<span class="k">for</span> <span class="nx">i</span> <span class="o">:=</span> <span class="mi">0</span><span class="p">;</span> <span class="nx">i</span> <span class="p"><</span> <span class="nx">b</span><span class="p">.</span><span class="nx">N</span><span class="p">;</span> <span class="nx">i</span><span class="o">++</span> <span class="p">{</span>
<span class="nf">IntMin</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
<span class="p">}</span>
<span class="p">}</span>
</pre>
</td>
</tr>
</table>
<table>
<tr>
<td class="docs">
<p>以啰嗦模式运行当前项目下的所有测试。</p>
</td>
<td class="code leading">
<pre class="chroma">
<span class="gp">$</span> go test -v
<span class="go">== RUN TestIntMinBasic
</span><span class="go">--- PASS: TestIntMinBasic (0.00s)
</span><span class="go">=== RUN TestIntMinTableDriven
</span><span class="go">=== RUN TestIntMinTableDriven/0,1
</span><span class="go">=== RUN TestIntMinTableDriven/1,0
</span><span class="go">=== RUN TestIntMinTableDriven/2,-2
</span><span class="go">=== RUN TestIntMinTableDriven/0,-1
</span><span class="go">=== RUN TestIntMinTableDriven/-1,0
</span><span class="go">--- PASS: TestIntMinTableDriven (0.00s)
</span><span class="go"> --- PASS: TestIntMinTableDriven/0,1 (0.00s)
</span><span class="go"> --- PASS: TestIntMinTableDriven/1,0 (0.00s)
</span><span class="go"> --- PASS: TestIntMinTableDriven/2,-2 (0.00s)
</span><span class="go"> --- PASS: TestIntMinTableDriven/0,-1 (0.00s)
</span><span class="go"> --- PASS: TestIntMinTableDriven/-1,0 (0.00s)
</span><span class="go">PASS
</span><span class="go">ok examples/testing-and-benchmarking 0.023s</span></pre>
</td>
</tr>
<tr>
<td class="docs">
<p>运行当前项目中的所有基准测试。所有测试都在基准测试之前运行。
<code>bench</code> 标志使用正则表达式过滤基准函数名称。</p>
</td>
<td class="code">
<pre class="chroma">
<span class="gp">$</span> go test -bench=.
<span class="go">goos: darwin
</span><span class="go">goarch: arm64
</span><span class="go">pkg: examples/testing
</span><span class="go">BenchmarkIntMin-8 1000000000 0.3136 ns/op
</span><span class="go">PASS
</span><span class="go">ok examples/testing-and-benchmarking 0.351s</span></pre>
</td>
</tr>
</table>
<p class="next">
下一个例子: <a href="command-line-arguments">命令行参数</a>
</p>
<p class="footer">
<a href="https://twitter.com/mmcgrana">@mmcgrana</a> 和<a href="https://eli.thegreenplace.net">Eli Bendersky</a>编写 | <a href="https://github.com/gobyexample-cn">gobyexample-cn</a> 翻译 | <a href="https://github.com/gobyexample-cn/gobyexample/issues">反馈</a> | <a href="https://github.com/gobyexample-cn/gobyexample">源码</a> | <a href="https://github.com/mmcgrana/gobyexample#license">license</a> </p>
</p>
</div>
<script>
var codeLines = [];
codeLines.push('');codeLines.push('');codeLines.push('package main\u000A');codeLines.push('import (\u000A \"fmt\"\u000A \"testing\"\u000A)\u000A');codeLines.push('func IntMin(a, b int) int {\u000A if a \u003C b {\u000A return a\u000A }\u000A return b\u000A}\u000A');codeLines.push('func TestIntMinBasic(t *testing.T) {\u000A ans :\u003D IntMin(2, -2)\u000A if ans !\u003D -2 {\u000A');codeLines.push(' t.Errorf(\"IntMin(2, -2) \u003D %d; want -2\", ans)\u000A }\u000A}\u000A');codeLines.push('func TestIntMinTableDriven(t *testing.T) {\u000A var tests \u003D []struct {\u000A a, b int\u000A want int\u000A }{\u000A {0, 1, 0},\u000A {1, 0, 0},\u000A {2, -2, -2},\u000A {0, -1, -1},\u000A {-1, 0, -1},\u000A }\u000A');codeLines.push(' for _, tt :\u003D range tests {\u000A');codeLines.push(' testname :\u003D fmt.Sprintf(\"%d,%d\", tt.a, tt.b)\u000A t.Run(testname, func(t *testing.T) {\u000A ans :\u003D IntMin(tt.a, tt.b)\u000A if ans !\u003D tt.want {\u000A t.Errorf(\"got %d, want %d\", ans, tt.want)\u000A }\u000A })\u000A }\u000A}\u000A');codeLines.push('func BenchmarkIntMin(b *testing.B) {\u000A');codeLines.push(' for i :\u003D 0; i \u003C b.N; i++ {\u000A IntMin(1, 2)\u000A }\u000A}\u000A');codeLines.push('');codeLines.push('');
</script>
<script src="site.js" async></script>
</body>
</html>