-
Notifications
You must be signed in to change notification settings - Fork 148
/
Copy pathtesting.htm
1159 lines (710 loc) · 75.9 KB
/
testing.htm
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head profile="http://a9.com/-/spec/opensearch/1.1/">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="../assets/site.css" rel="stylesheet">
<title>testing</title>
<meta name="twitter:title" content="Package testing">
<meta property="og:title" content="Package testing">
<meta name="description" content="Package testing provides support for automated testing of Go packages.">
<meta name="twitter:description" content="Package testing provides support for automated testing of Go packages.">
<meta property="og:description" content="Package testing provides support for automated testing of Go packages.">
<meta name="twitter:card" content="summary">
<meta name="twitter:site" content="@golang">
</head>
<body>
<div class="container">
<h2 id="pkg-overview">package testing</h2>
<p><code>import "testing"</code>
<p>
testing 提供对 Go 包的自动化测试的支持。通过 `go test` 命令,能够自动执行如下形式的任何函数:
</p>
<pre>func TestXxx(*testing.T)
</pre>
<p>
其中 Xxx 可以是任何字母数字字符串(但第一个字母不能是
[a-z]),用于识别测试例程。
</p>
<p>
在这些函数中,使用 Error, Fail 或相关方法来发出失败信号。
</p>
<p>
要编写一个新的测试套件,需要创建一个名称以 _test.go 结尾的文件,该文件包含 `TestXxx` 函数,如上所述。 将该文件放在与被测试的包相同的包中。该文件将被排除在正常的程序包之外,但在运行 “go test” 命令时将被包含。 有关详细信息,请运行 “go help test” 和 “go help testflag” 了解。
</p>
<p>
如果有需要,可以调用 *T 和 *B 的 Skip 方法,跳过该测试或基准测试:
</p>
<pre>func TestTimeConsuming(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
...
}
</pre>
<h4 id="hdr-Benchmarks">Benchmarks <a class="permalink" href="#hdr-Benchmarks">¶</a></h4>
<p>
如下形式的函数:
</p>
<pre>func BenchmarkXxx(*testing.B)
</pre>
<p>
被认为是基准测试,通过 "go test" 命令,加上 -bench flag 来执行。多个基准测试按照顺序运行。
</p>
<p>
testing flags 的详细描述, 参见
<a href="https://github.com/golang/go/blob/master/cmd/go/#hdr-Description_of_testing_flags">https://github.com/golang/go/blob/master/cmd/go/#hdr-Description_of_testing_flags</a>.
</p>
<p>
基准测试函数样例看起来如下所示:
</p>
<pre>func BenchmarkHello(b *testing.B) {
for i := 0; i < b.N; i++ {
fmt.Sprintf("hello")
}
}
</pre>
<p>
基准函数会运行目标代码 b.N 次。在基准执行期间,会调整 b.N 直到基准测试函数持续足够长的时间。输出
</p>
<pre>BenchmarkHello 10000000 282 ns/op
</pre>
<p>
意味着循环执行了 10000000 次,每次循环花费 282 纳秒(ns)。
</p>
<p>
如果在运行前基准测试需要一些耗时的配置,则可以先重置定时器:
</p>
<pre>func BenchmarkBigLen(b *testing.B) {
big := NewBig()
b.ResetTimer()
for i := 0; i < b.N; i++ {
big.Len()
}
}
</pre>
<p>
如果基准测试需要在并行设置中测试性能,则可以使用 RunParallel 辅助函数; 这样的基准测试一般与 go test -cpu 标志一起使用:
</p>
<pre>func BenchmarkTemplateParallel(b *testing.B) {
templ := template.Must(template.New("test").Parse("Hello, {{.}}!"))
b.RunParallel(func(pb *testing.PB) {
var buf bytes.Buffer
for pb.Next() {
buf.Reset()
templ.Execute(&buf, "World")
}
})
}
</pre>
<h4 id="hdr-Examples">Examples <a class="permalink" href="#hdr-Examples">¶</a></h4>
<p>
该包还运行并验证示例代码。示例函数可以包括以 "Output:" 开头的行注释,并在运行测试时与函数的标准输出进行比较。 (比较时会忽略前导和尾随空格。)这些是一个 example 的例子:
</p>
<pre>func ExampleHello() {
fmt.Println("hello")
// Output: hello
}
func ExampleSalutations() {
fmt.Println("hello, and")
fmt.Println("goodbye")
// Output:
// hello, and
// goodbye
}
</pre>
<p>
"Unordered output:" 形式的注释,和 "Output:" 类似,但是能够以任意顺序匹配行:
</p>
<pre>
func ExamplePerm() {
for _, value := range Perm(4) {
fmt.Println(value)
}
// Unordered output: 4
// 2
// 1
// 3
// 0
}
</pre>
<p>
没有输出注释的示例函数被编译但不执行。
</p>
<p>
example 声明的命名约定:包,函数 F,类型 T,类型 T 上的方法 M 依次是:
</p>
<pre>func Example() { ... }
func ExampleF() { ... }
func ExampleT() { ... }
func ExampleT_M() { ... }
</pre>
<p>
可以为 包/类型/函数/方法 提供多个 example 函数,这通过在名称上附加一个不同的后缀来实现。后缀必须是以小写字母开头。
</p>
<pre>func Example_suffix() { ... }
func ExampleF_suffix() { ... }
func ExampleT_suffix() { ... }
func ExampleT_M_suffix() { ... }
</pre>
<p>
当一个文件包含一个示例函数,同时至少一个其他函数,类型,变量或常量声明,或没有测试或基准函数时,这个测试文件作为示例存在,通常命名为 example_test.go
</p>
<h4 id="hdr-Subtests_and_Sub_benchmarks">Subtests 和 Sub-benchmarks <a class="permalink" href="#hdr-Subtests_and_Sub_benchmarks">¶</a></h4>
<p>
T 和 B 的 Run 方法允许定义子单元测试和子基准测试,而不必为每个子测试和子基准定义单独的函数。这使得可以使用 Table-Driven 的基准测试和创建层级测试。它还提供了一种共享通用 setup 和 tear-down 代码的方法:
</p>
<pre>func TestFoo(t *testing.T) {
// <setup code>
t.Run("A=1", func(t *testing.T) { ... })
t.Run("A=2", func(t *testing.T) { ... })
t.Run("B=1", func(t *testing.T) { ... })
// <tear-down code>
}
</pre>
<p>
每个子测试和子基准测试都有一个唯一的名称:顶级测试的名称和传递给 Run 的名称的组合,以斜杠分隔,并具有用于消歧的可选尾随序列号。
</p>
<p>
-run 和 -bench 命令行标志的参数是与测试名称相匹配的非固定的正则表达式。对于具有多个斜杠分隔元素(例如子测试)的测试,该参数本身是斜杠分隔的,其中表达式依次匹配每个名称元素。因为它是非固定的,一个空的表达式匹配任何字符串。例如,使用 "匹配" 表示 "其名称包含":
</p>
<pre>go test -run '' # Run 所有测试。
go test -run Foo # Run 匹配 "Foo" 的顶层测试,例如 "TestFooBar"。
go test -run Foo/A= # 匹配顶层测试 "Foo",运行其匹配 "A=" 的子测试。
go test -run /A=1 # 运行所有匹配 "A=1" 的子测试。
</pre>
<p>
子测试也可用于控制并行性。所有的子测试完成后,父测试才会完成。在这个例子中,所有的测试是相互并行运行的,当然也只是彼此之间,不包括定义在其他顶层测试的子测试:
</p>
<pre>func TestGroupedParallel(t *testing.T) {
for _, tc := range tests {
tc := tc // capture range variable
t.Run(tc.Name, func(t *testing.T) {
t.Parallel()
...
})
}
}
</pre>
<p>
在并行子测试完成之前,Run 方法不会返回,这提供了一种测试后清理的方法:
</p>
<pre>func TestTeardownParallel(t *testing.T) {
// This Run will not return until the parallel tests finish.
t.Run("group", func(t *testing.T) {
t.Run("Test1", parallelTest1)
t.Run("Test2", parallelTest2)
t.Run("Test3", parallelTest3)
})
// <tear-down code>
}
</pre>
<h4 id="hdr-Main">Main <a class="permalink" href="#hdr-Main">¶</a></h4>
<p>
测试程序有时需要在测试之前或之后进行额外的设置(setup)或拆卸(teardown)。有时, 测试还需要控制在主线程上运行的代码。为了支持这些和其他一些情况, 如果测试文件包含函数:
</p>
<pre>func TestMain(m *testing.M)
</pre>
<p>
那么生成的测试将调用 TestMain(m),而不是直接运行测试。TestMain 运行在主 goroutine 中, 可以在调用 m.Run 前后做任何设置和拆卸。应该使用 m.Run 的返回值作为参数调用 os.Exit。在调用 TestMain 时, flag.Parse 并没有被调用。所以,如果 TestMain 依赖于 command-line 标志 (包括 testing 包的标记), 则应该显示的调用 flag.Parse。
</p>
<p>
一个简单的 TestMain 的实现:
</p>
<pre>func TestMain(m *testing.M) {
// call flag.Parse() here if TestMain uses flags
// 如果 TestMain 使用了 flags,这里应该加上 flag.Parse()
os.Exit(m.Run())
}</pre>
<h3 id="pkg-index" class="section-header">Index <a class="permalink" href="#pkg-index">¶</a></h3>
<ul class="list-unstyled">
<li><a href="#AllocsPerRun">func AllocsPerRun(runs int, f func()) (avg float64)</a></li><li><a href="#CoverMode">func CoverMode() string</a></li><li><a href="#Coverage">func Coverage() float64</a></li><li><a href="#Main">func Main(matchString func(pat, str string) (bool, error), tests []InternalTest, benchmarks []InternalBenchmark, examples []InternalExample)</a></li><li><a href="#RegisterCover">func RegisterCover(c Cover)</a></li><li><a href="#RunBenchmarks">func RunBenchmarks(matchString func(pat, str string) (bool, error), benchmarks []InternalBenchmark)</a></li><li><a href="#RunExamples">func RunExamples(matchString func(pat, str string) (bool, error), examples []InternalExample) (ok bool)</a></li><li><a href="#RunTests">func RunTests(matchString func(pat, str string) (bool, error), tests []InternalTest) (ok bool)</a></li><li><a href="#Short">func Short() bool</a></li><li><a href="#Verbose">func Verbose() bool</a></li>
<li><a href="#B">type B</a></li>
<ul>
<li><a href="#B.Error">func (c *B) Error(args ...interface{})</a></li><li><a href="#B.Errorf">func (c *B) Errorf(format string, args ...interface{})</a></li><li><a href="#B.Fail">func (c *B) Fail()</a></li><li><a href="#B.FailNow">func (c *B) FailNow()</a></li><li><a href="#B.Failed">func (c *B) Failed() bool</a></li><li><a href="#B.Fatal">func (c *B) Fatal(args ...interface{})</a></li><li><a href="#B.Fatalf">func (c *B) Fatalf(format string, args ...interface{})</a></li><li><a href="#B.Log">func (c *B) Log(args ...interface{})</a></li><li><a href="#B.Logf">func (c *B) Logf(format string, args ...interface{})</a></li><li><a href="#B.Name">func (c *B) Name() string</a></li><li><a href="#B.ReportAllocs">func (b *B) ReportAllocs()</a></li><li><a href="#B.ResetTimer">func (b *B) ResetTimer()</a></li><li><a href="#B.Run">func (b *B) Run(name string, f func(b *B)) bool</a></li><li><a href="#B.RunParallel">func (b *B) RunParallel(body func(*PB))</a></li><li><a href="#B.SetBytes">func (b *B) SetBytes(n int64)</a></li><li><a href="#B.SetParallelism">func (b *B) SetParallelism(p int)</a></li><li><a href="#B.Skip">func (c *B) Skip(args ...interface{})</a></li><li><a href="#B.SkipNow">func (c *B) SkipNow()</a></li><li><a href="#B.Skipf">func (c *B) Skipf(format string, args ...interface{})</a></li><li><a href="#B.Skipped">func (c *B) Skipped() bool</a></li><li><a href="#B.StartTimer">func (b *B) StartTimer()</a></li><li><a href="#B.StopTimer">func (b *B) StopTimer()</a></li>
</ul>
<li><a href="#BenchmarkResult">type BenchmarkResult</a></li>
<ul>
<li><a href="#Benchmark">func Benchmark(f func(b *B)) BenchmarkResult</a></li>
<li><a href="#BenchmarkResult.AllocedBytesPerOp">func (r BenchmarkResult) AllocedBytesPerOp() int64</a></li><li><a href="#BenchmarkResult.AllocsPerOp">func (r BenchmarkResult) AllocsPerOp() int64</a></li><li><a href="#BenchmarkResult.MemString">func (r BenchmarkResult) MemString() string</a></li><li><a href="#BenchmarkResult.NsPerOp">func (r BenchmarkResult) NsPerOp() int64</a></li><li><a href="#BenchmarkResult.String">func (r BenchmarkResult) String() string</a></li>
</ul>
<li><a href="#Cover">type Cover</a></li>
<li><a href="#CoverBlock">type CoverBlock</a></li>
<li><a href="#InternalBenchmark">type InternalBenchmark</a></li>
<li><a href="#InternalExample">type InternalExample</a></li>
<li><a href="#InternalTest">type InternalTest</a></li>
<li><a href="#M">type M</a></li>
<ul>
<li><a href="#MainStart">func MainStart(deps testDeps, tests []InternalTest, benchmarks []InternalBenchmark, examples []InternalExample) *M</a></li>
<li><a href="#M.Run">func (m *M) Run() int</a></li>
</ul>
<li><a href="#PB">type PB</a></li>
<ul>
<li><a href="#PB.Next">func (pb *PB) Next() bool</a></li>
</ul>
<li><a href="#T">type T</a></li>
<ul>
<li><a href="#T.Error">func (c *T) Error(args ...interface{})</a></li><li><a href="#T.Errorf">func (c *T) Errorf(format string, args ...interface{})</a></li><li><a href="#T.Fail">func (c *T) Fail()</a></li><li><a href="#T.FailNow">func (c *T) FailNow()</a></li><li><a href="#T.Failed">func (c *T) Failed() bool</a></li><li><a href="#T.Fatal">func (c *T) Fatal(args ...interface{})</a></li><li><a href="#T.Fatalf">func (c *T) Fatalf(format string, args ...interface{})</a></li><li><a href="#T.Log">func (c *T) Log(args ...interface{})</a></li><li><a href="#T.Logf">func (c *T) Logf(format string, args ...interface{})</a></li><li><a href="#T.Name">func (c *T) Name() string</a></li><li><a href="#T.Parallel">func (t *T) Parallel()</a></li><li><a href="#T.Run">func (t *T) Run(name string, f func(t *T)) bool</a></li><li><a href="#T.Skip">func (c *T) Skip(args ...interface{})</a></li><li><a href="#T.SkipNow">func (c *T) SkipNow()</a></li><li><a href="#T.Skipf">func (c *T) Skipf(format string, args ...interface{})</a></li><li><a href="#T.Skipped">func (c *T) Skipped() bool</a></li>
</ul>
<li><a href="#TB">type TB</a></li>
</ul>
<h4 id="pkg-examples">Examples <a class="permalink" href="#pkg-examples">¶</a></h4>
<ul class="list-unstyled">
<li><a href="#example-B-RunParallel" onclick="$('#ex-B-RunParallel').addClass('in').removeClass('collapse').height('auto')">B.RunParallel</a></li>
</ul>
<h4 id="pkg-files">
<a href="https://github.com/golang/go/blob/master/src/testing/">Package Files</a>
<a class="permalink" href="#pkg-files">¶</a>
</h4>
<p><a href="https://github.com/golang/go/blob/master/src/testing/allocs.go">allocs.go</a> <a href="https://github.com/golang/go/blob/master/src/testing/benchmark.go">benchmark.go</a> <a href="https://github.com/golang/go/blob/master/src/testing/cover.go">cover.go</a> <a href="https://github.com/golang/go/blob/master/src/testing/example.go">example.go</a> <a href="https://github.com/golang/go/blob/master/src/testing/match.go">match.go</a> <a href="https://github.com/golang/go/blob/master/src/testing/testing.go">testing.go</a> </p>
<h3 id="AllocsPerRun" data-kind="f">func <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/allocs.go#L20">AllocsPerRun</a> <a class="permalink" href="#AllocsPerRun">¶</a> <a class="uses" title="List Function Callers" href="https://sourcegraph.com/-/godoc/refs?def=AllocsPerRun&pkg=testing&repo=">Uses</a></h3>
<div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/allocs.go#L20">❖</a><pre>func AllocsPerRun(runs <a href="/builtin#int">int</a>, f func()) (avg <a href="/builtin#float64">float64</a>)</pre></div><p>
AllocsPerRun 返回在调用 f 期间内存平均分配次数。虽然返回值的类型为 float64,但它始终是一个整数值。
</p>
<p>
要计算分配次数,该函数将首先作为热身运行一次。然后将测量并返回指定数量(runs 参数)运行的内存平均分配次数。
</p>
<p>
AllocsPerRun 在测量过程中将 GOMAXPROCS 设置为1,并在返回前将其还原。
</p>
<h3 id="CoverMode" data-kind="f">func <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L292">CoverMode</a> <a class="permalink" href="#CoverMode">¶</a> <a class="uses" title="List Function Callers" href="https://sourcegraph.com/-/godoc/refs?def=CoverMode&pkg=testing&repo=">Uses</a></h3>
<div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L292">❖</a><pre>func CoverMode() <a href="/builtin#string">string</a></pre></div><p>
CoverMode reports what the test coverage mode is set to. The
values are "set", "count", or "atomic". The return value will be
empty if test coverage is not enabled.
</p>
<h3 id="Coverage" data-kind="f">func <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/cover.go#L45">Coverage</a> <a class="permalink" href="#Coverage">¶</a> <a class="uses" title="List Function Callers" href="https://sourcegraph.com/-/godoc/refs?def=Coverage&pkg=testing&repo=">Uses</a></h3>
<div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/cover.go#L45">❖</a><pre>func Coverage() <a href="/builtin#float64">float64</a></pre></div><p>
Coverage reports the current code coverage as a fraction in the range [0, 1].
If coverage is not enabled, Coverage returns 0.
</p>
<p>
When running a large set of sequential test cases, checking Coverage after each one
can be useful for identifying which test cases exercise new code paths.
It is not a replacement for the reports generated by 'go test -cover' and
'go tool cover'.
</p>
<h3 id="Main" data-kind="f">func <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L774">Main</a> <a class="permalink" href="#Main">¶</a> <a class="uses" title="List Function Callers" href="https://sourcegraph.com/-/godoc/refs?def=Main&pkg=testing&repo=">Uses</a></h3>
<div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L774">❖</a><pre>func Main(matchString func(pat, str <a href="/builtin#string">string</a>) (<a href="/builtin#bool">bool</a>, <a href="/builtin#error">error</a>), tests []<a href="#InternalTest">InternalTest</a>, benchmarks []<a href="#InternalBenchmark">InternalBenchmark</a>, examples []<a href="#InternalExample">InternalExample</a>)</pre></div><p>
Main is an internal function, part of the implementation of the "go test" command.
It was exported because it is cross-package and predates "internal" packages.
It is no longer used by "go test" but preserved, as much as possible, for other
systems that simulate "go test" using Main, but Main sometimes cannot be updated as
new functionality is added to the testing package.
Systems simulating "go test" should be updated to use MainStart.
</p>
<h3 id="RegisterCover" data-kind="f">func <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/cover.go#L64">RegisterCover</a> <a class="permalink" href="#RegisterCover">¶</a> <a class="uses" title="List Function Callers" href="https://sourcegraph.com/-/godoc/refs?def=RegisterCover&pkg=testing&repo=">Uses</a></h3>
<div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/cover.go#L64">❖</a><pre>func RegisterCover(c <a href="#Cover">Cover</a>)</pre></div><p>
RegisterCover records the coverage data accumulators for the tests.
NOTE: This function is internal to the testing infrastructure and may change.
It is not covered (yet) by the Go 1 compatibility guidelines.
</p>
<h3 id="RunBenchmarks" data-kind="f">func <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L365">RunBenchmarks</a> <a class="permalink" href="#RunBenchmarks">¶</a> <a class="uses" title="List Function Callers" href="https://sourcegraph.com/-/godoc/refs?def=RunBenchmarks&pkg=testing&repo=">Uses</a></h3>
<div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L365">❖</a><pre>func RunBenchmarks(matchString func(pat, str <a href="/builtin#string">string</a>) (<a href="/builtin#bool">bool</a>, <a href="/builtin#error">error</a>), benchmarks []<a href="#InternalBenchmark">InternalBenchmark</a>)</pre></div><p>
An internal function but exported because it is cross-package; part of the implementation
of the "go test" command.
</p>
<h3 id="RunExamples" data-kind="f">func <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/example.go#L26">RunExamples</a> <a class="permalink" href="#RunExamples">¶</a> <a class="uses" title="List Function Callers" href="https://sourcegraph.com/-/godoc/refs?def=RunExamples&pkg=testing&repo=">Uses</a></h3>
<div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/example.go#L26">❖</a><pre>func RunExamples(matchString func(pat, str <a href="/builtin#string">string</a>) (<a href="/builtin#bool">bool</a>, <a href="/builtin#error">error</a>), examples []<a href="#InternalExample">InternalExample</a>) (ok <a href="/builtin#bool">bool</a>)</pre></div><p>
An internal function but exported because it is cross-package; part of the implementation
of the "go test" command.
</p>
<h3 id="RunTests" data-kind="f">func <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L858">RunTests</a> <a class="permalink" href="#RunTests">¶</a> <a class="uses" title="List Function Callers" href="https://sourcegraph.com/-/godoc/refs?def=RunTests&pkg=testing&repo=">Uses</a></h3>
<div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L858">❖</a><pre>func RunTests(matchString func(pat, str <a href="/builtin#string">string</a>) (<a href="/builtin#bool">bool</a>, <a href="/builtin#error">error</a>), tests []<a href="#InternalTest">InternalTest</a>) (ok <a href="/builtin#bool">bool</a>)</pre></div><p>
An internal function but exported because it is cross-package; part of the implementation
of the "go test" command.
</p>
<h3 id="Short" data-kind="f">func <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L285">Short</a> <a class="permalink" href="#Short">¶</a> <a class="uses" title="List Function Callers" href="https://sourcegraph.com/-/godoc/refs?def=Short&pkg=testing&repo=">Uses</a></h3>
<div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L285">❖</a><pre>func Short() <a href="/builtin#bool">bool</a></pre></div><p>
Short reports whether the -test.short flag is set.
</p>
<h3 id="Verbose" data-kind="f">func <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L297">Verbose</a> <a class="permalink" href="#Verbose">¶</a> <a class="uses" title="List Function Callers" href="https://sourcegraph.com/-/godoc/refs?def=Verbose&pkg=testing&repo=">Uses</a></h3>
<div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L297">❖</a><pre>func Verbose() <a href="/builtin#bool">bool</a></pre></div><p>
Verbose reports whether the -test.v flag is set.
</p>
<h3 id="B" data-kind="t">type <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L48">B</a> <a class="permalink" href="#B">¶</a> <a class="uses" title="List Uses of This Type" href="https://sourcegraph.com/-/godoc/refs?def=B&pkg=testing&repo=">Uses</a></h3>
<div class="decl" data-kind="d"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L48">❖</a><pre>type B struct {
<span id="B.N">N</span> <a href="/builtin#int">int</a>
<span class="com">// contains filtered or unexported fields</span>
}</pre></div><p>
B 是传递给基准测试函数的一种类型,它用于管理基准测试的计时行为,并指示应该迭代地运行测试多少次。
</p>
<p>
一个基准测试在它的基准测试函数返回时,又或者在它的基准测试函数调用 FailNow、Fatal、Fatalf、SkipNow、Skip 或者 Skipf 中的任意一个方法时,测试即宣告结束。至于其他报告方法,比如 Log 和 Error 的变种,则可以在其他 goroutine 中同时进行调用。
</p>
<p>
跟单元测试一样,基准测试会在执行的过程中积累日志,并在测试完毕时将日志转储到标准错误。但跟单元测试不一样的是,为了避免基准测试的结果受到日志打印操作的影响,基准测试总是会把日志打印出来。
</p>
<h4 id="B.Error" data-kind="m">func (*B) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L510">Error</a> <a class="permalink" href="#B.Error">¶</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=common%2FError&pkg=testing&repo=">Uses</a></h4>
<div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L510">❖</a><pre>func (c *B) Error(args ...interface{})</pre></div><p>
调用 Error 相当于在调用 Log 之后调用 Fail 。
</p>
<h4 id="B.Errorf" data-kind="m">func (*B) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L516">Errorf</a> <a class="permalink" href="#B.Errorf">¶</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=common%2FErrorf&pkg=testing&repo=">Uses</a></h4>
<div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L516">❖</a><pre>func (c *B) Errorf(format <a href="/builtin#string">string</a>, args ...interface{})</pre></div><p>
调用 Errorf 相当于在调用 Logf 之后调用 Fail 。
</p>
<h4 id="B.Fail" data-kind="m">func (*B) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L437">Fail</a> <a class="permalink" href="#B.Fail">¶</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=common%2FFail&pkg=testing&repo=">Uses</a></h4>
<div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L437">❖</a><pre>func (c *B) Fail()</pre></div><p>
将当前的测试函数标识为“失败”,但仍然继续执行该函数。
</p>
<h4 id="B.FailNow" data-kind="m">func (*B) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L463">FailNow</a> <a class="permalink" href="#B.FailNow">¶</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=common%2FFailNow&pkg=testing&repo=">Uses</a></h4>
<div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L463">❖</a><pre>func (c *B) FailNow()</pre></div><p>
将当前的测试函数标识为“失败”,并停止执行该函数。在此之后,测试过程将在下一个测试或者下一个基准测试中继续。
FailNow 必须在运行测试函数或者基准测试函数的 goroutine 中调用,而不能在测试期间创建的 goroutine 中调用。调用 FailNow 不会导致其他 goroutine 停止。
</p>
<h4 id="B.Failed" data-kind="m">func (*B) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L451">Failed</a> <a class="permalink" href="#B.Failed">¶</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=common%2FFailed&pkg=testing&repo=">Uses</a></h4>
<div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L451">❖</a><pre>func (c *B) Failed() <a href="/builtin#bool">bool</a></pre></div><p>
Failed 用于报告测试函数是否已失败。
</p>
<h4 id="B.Fatal" data-kind="m">func (*B) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L522">Fatal</a> <a class="permalink" href="#B.Fatal">¶</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=common%2FFatal&pkg=testing&repo=">Uses</a></h4>
<div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L522">❖</a><pre>func (c *B) Fatal(args ...interface{})</pre></div><p>
调用 Fatal 相当于在调用 Log 之后调用 FailNow 。
</p>
<h4 id="B.Fatalf" data-kind="m">func (*B) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L528">Fatalf</a> <a class="permalink" href="#B.Fatalf">¶</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=common%2FFatalf&pkg=testing&repo=">Uses</a></h4>
<div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L528">❖</a><pre>func (c *B) Fatalf(format <a href="/builtin#string">string</a>, args ...interface{})</pre></div><p>
调用 Fatalf 相当于在调用 Logf 之后调用 FailNow 。
</p>
<h4 id="B.Log" data-kind="m">func (*B) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L500">Log</a> <a class="permalink" href="#B.Log">¶</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=common%2FLog&pkg=testing&repo=">Uses</a></h4>
<div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L500">❖</a><pre>func (c *B) Log(args ...interface{})</pre></div><p>
Log 使用与 Println 相同的格式化语法对它的参数进行格式化, 然后将格式化后的文本记录到错误日志里面:
</p>
<p>
对于测试来说, 格式化文本只会在测试失败或者设置了 -test.v 标志的情况下被打印出来;
对于基准测试来说, 为了避免 -test.v 标志的值对测试的性能产生影响, 格式化文本总会被打印出来。
</p>
<h4 id="B.Logf" data-kind="m">func (*B) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L507">Logf</a> <a class="permalink" href="#B.Logf">¶</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=common%2FLogf&pkg=testing&repo=">Uses</a></h4>
<div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L507">❖</a><pre>func (c *B) Logf(format <a href="/builtin#string">string</a>, args ...interface{})</pre></div><p>
Log 使用与 Printf 相同的格式化语法对它的参数进行格式化, 然后将格式化后的文本记录到错误日志里面。 如果输入的格式化文本最末尾没有出现新行, 那么将一个新行添加到格式化后的文本末尾。
</p>
<p>
对于测试来说,Logf 产生的格式化文本只会在测试失败或者设置了 -test.v 标志的情况下被打印出来; 对于基准测试来说, 为了避免 -test.v 标志的值对测试的性能产生影响, Logf 产生的格式化文本总会被打印出来。
</p>
<h4 id="B.Name" data-kind="m">func (*B) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L423">Name</a> <a class="permalink" href="#B.Name">¶</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=common%2FName&pkg=testing&repo=">Uses</a></h4>
<div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L423">❖</a><pre>func (c *B) Name() <a href="/builtin#string">string</a></pre></div><p>
返回正在运行的测试或者基准测试的名字。
</p>
<h4 id="B.ReportAllocs" data-kind="m">func (*B) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L117">ReportAllocs</a> <a class="permalink" href="#B.ReportAllocs">¶</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=B%2FReportAllocs&pkg=testing&repo=">Uses</a></h4>
<div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L117">❖</a><pre>func (b *<a href="#B">B</a>) ReportAllocs()</pre></div><p>
打开当前基准测试的内存统计功能,与使用 -test.benchmem 设置类似,但 ReportAllocs 只影响那些调用了该函数的基准测试。
</p>
<h4 id="B.ResetTimer" data-kind="m">func (*B) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L98">ResetTimer</a> <a class="permalink" href="#B.ResetTimer">¶</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=B%2FResetTimer&pkg=testing&repo=">Uses</a></h4>
<div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L98">❖</a><pre>func (b *<a href="#B">B</a>) ResetTimer()</pre></div><p>
对已经逝去的基准测试时间以及内存分配计数器进行清零。对于正在运行中的计时器,这个方法不会产生任何效果。
</p>
<h4 id="B.Run" data-kind="m">func (*B) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L466">Run</a> <a class="permalink" href="#B.Run">¶</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=B%2FRun&pkg=testing&repo=">Uses</a></h4>
<div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L466">❖</a><pre>func (b *<a href="#B">B</a>) Run(name <a href="/builtin#string">string</a>, f func(b *<a href="#B">B</a>)) <a href="/builtin#bool">bool</a></pre></div><p>
执行名字为 name 的子基准测试(subbenchmark)f ,并报告 f 在执行过程中是否出现了任何失败。
</p>
<p>
子基准测试跟其他普通的基准测试一样。一个调用了 Run 方法至少一次的基准测试将不会对其自身进行测量(measure),并且在 N 为 1 时, 这个基准测试将只会被执行一次。
</p>
<p>
Run 可以同时在多个 goroutine 中被调用,但这些调用必须发生在 b 的外部基准函数(outer benchmark function)返回之前。
</p>
<h4 id="B.RunParallel" data-kind="m">func (*B) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L573">RunParallel</a> <a class="permalink" href="#B.RunParallel">¶</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=B%2FRunParallel&pkg=testing&repo=">Uses</a></h4>
<div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L573">❖</a><pre>func (b *<a href="#B">B</a>) RunParallel(body func(*<a href="#PB">PB</a>))</pre></div><p>
以并行的方式执行给定的基准测试。 RunParallel 会创建出多个 goroutine ,并将 b.N 分配给这些 goroutine 执行, 其中 goroutine 数量的默认值为 GOMAXPROCS 。用户如果想要增加非CPU受限(non-CPU-bound)基准测试的并行性, 那么可以在 RunParallel 之前调用 SetParallelism 。RunParallel 通常会与 -cpu 标志一同使用。
</p>
<p>
body 函数将在每个 goroutine 中执行,这个函数需要设置所有 goroutine 本地的状态, 并迭代直到 pb.Next 返回 false 值为止。因为 StartTimer 、 StopTimer 和 ResetTimer 这三个函数都带有全局作用,所以 body 函数不应该调用这些函数;除此之外,body 函数也不应该调用 Run 函数。
</p>
<div class="panel-group">
<div class="panel panel-default" id="example-B-RunParallel">
<div class="panel-heading"><a class="accordion-toggle" data-toggle="collapse" href="#ex-B-RunParallel">Example</a></div>
<div id="ex-B-RunParallel" class="panel-collapse collapse"><div class="panel-body">
<p>Code:<span class="pull-right"><a href="?play=B-RunParallel">play</a> </span>
<pre>
<span class="com">// Parallel benchmark for text/template.Template.Execute on a single object.</span>
testing.Benchmark(func(b *testing.B) {
templ := template.Must(template.New("test").Parse("Hello, {{.}}!"))
<span class="com">// RunParallel will create GOMAXPROCS goroutines
// and distribute work among them.</span>
b.RunParallel(func(pb *testing.PB) {
<span class="com">// Each goroutine has its own bytes.Buffer.</span>
var buf bytes.Buffer
for pb.Next() {
<span class="com">// The loop body is executed b.N times total across all goroutines.</span>
buf.Reset()
templ.Execute(&buf, "World")
}
})
})
</pre>
</div></div>
</div>
</div>
<h4 id="B.SetBytes" data-kind="m">func (*B) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L112">SetBytes</a> <a class="permalink" href="#B.SetBytes">¶</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=B%2FSetBytes&pkg=testing&repo=">Uses</a></h4>
<div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L112">❖</a><pre>func (b *<a href="#B">B</a>) SetBytes(n <a href="/builtin#int64">int64</a>)</pre></div><p>
记录在单个操作中处理的字节数量。在调用了这个方法之后,基准测试将会报告 ns/op 以及 MB/s 。
</p>
<h4 id="B.SetParallelism" data-kind="m">func (*B) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L617">SetParallelism</a> <a class="permalink" href="#B.SetParallelism">¶</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=B%2FSetParallelism&pkg=testing&repo=">Uses</a></h4>
<div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L617">❖</a><pre>func (b *<a href="#B">B</a>) SetParallelism(p <a href="/builtin#int">int</a>)</pre></div><p>
将 RunParallel 使用的 goroutine 数量设置为 p*GOMAXPROCS ,如果 p 小于 1 ,那么调用将不产生任何效果。
</p>
<p>
CPU受限(CPU-bound)的基准测试通常不需要调用这个方法。
</p>
<h4 id="B.Skip" data-kind="m">func (*B) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L534">Skip</a> <a class="permalink" href="#B.Skip">¶</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=common%2FSkip&pkg=testing&repo=">Uses</a></h4>
<div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L534">❖</a><pre>func (c *B) Skip(args ...interface{})</pre></div><p>
调用 Skip 相当于在调用 Log 之后调用 SkipNow 。
</p>
<h4 id="B.SkipNow" data-kind="m">func (*B) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L552">SkipNow</a> <a class="permalink" href="#B.SkipNow">¶</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=common%2FSkipNow&pkg=testing&repo=">Uses</a></h4>
<div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L552">❖</a><pre>func (c *B) SkipNow()</pre></div><p>
将当前测试标识为“被跳过”并停止执行该测试。 如果一个测试在失败(参考 Error 、 Errorf 和 Fail)之后被跳过了, 那么它还是会被判断为是“失败的”。
</p>
<p>
在停止当前测试之后, 测试过程将在下一个测试或者下一个基准测试中继续, 具体请参考 FailNow 。
</p>
<p>
SkipNow 必须在运行测试的 goroutine 中进行调用, 而不能在测试期间创建的 goroutine 中调用。 调用 SkipNow 不会导致其他 goroutine 停止。
</p>
<h4 id="B.Skipf" data-kind="m">func (*B) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L540">Skipf</a> <a class="permalink" href="#B.Skipf">¶</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=common%2FSkipf&pkg=testing&repo=">Uses</a></h4>
<div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L540">❖</a><pre>func (c *B) Skipf(format <a href="/builtin#string">string</a>, args ...interface{})</pre></div><p>
调用 Skipf 相当于在调用 Logf 之后调用 SkipNow 。
</p>
<h4 id="B.Skipped" data-kind="m">func (*B) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L565">Skipped</a> <a class="permalink" href="#B.Skipped">¶</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=common%2FSkipped&pkg=testing&repo=">Uses</a></h4>
<div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L565">❖</a><pre>func (c *B) Skipped() <a href="/builtin#bool">bool</a></pre></div><p>
报告测试是否已被跳过。
</p>
<h4 id="B.StartTimer" data-kind="m">func (*B) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L73">StartTimer</a> <a class="permalink" href="#B.StartTimer">¶</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=B%2FStartTimer&pkg=testing&repo=">Uses</a></h4>
<div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L73">❖</a><pre>func (b *<a href="#B">B</a>) StartTimer()</pre></div><p>
开始对测试进行计时。
这个函数在基准测试开始时会自动被调用, 它也可以在调用 StopTimer 之后恢复进行计时。
</p>
<h4 id="B.StopTimer" data-kind="m">func (*B) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L86">StopTimer</a> <a class="permalink" href="#B.StopTimer">¶</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=B%2FStopTimer&pkg=testing&repo=">Uses</a></h4>
<div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L86">❖</a><pre>func (b *<a href="#B">B</a>) StopTimer()</pre></div><p>
停止对测试进行计时。
当你需要执行一些复杂的初始化操作, 并且你不想对这些操作进行测量时, 就可以使用这个方法来暂时地停止计时。
</p>
<h3 id="BenchmarkResult" data-kind="t">type <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L287">BenchmarkResult</a> <a class="permalink" href="#BenchmarkResult">¶</a> <a class="uses" title="List Uses of This Type" href="https://sourcegraph.com/-/godoc/refs?def=BenchmarkResult&pkg=testing&repo=">Uses</a></h3>
<div class="decl" data-kind="d"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L287">❖</a><pre>type BenchmarkResult struct {
<span id="BenchmarkResult.N">N</span> <a href="/builtin#int">int</a> <span class="com">// The number of iterations.</span>
<span id="BenchmarkResult.T">T</span> <a href="/time">time</a>.<a href="/time#Duration">Duration</a> <span class="com">// The total time taken.</span>
<span id="BenchmarkResult.Bytes">Bytes</span> <a href="/builtin#int64">int64</a> <span class="com">// Bytes processed in one iteration.</span>
<span id="BenchmarkResult.MemAllocs">MemAllocs</span> <a href="/builtin#uint64">uint64</a> <span class="com">// The total number of memory allocations.</span>
<span id="BenchmarkResult.MemBytes">MemBytes</span> <a href="/builtin#uint64">uint64</a> <span class="com">// The total number of bytes allocated.</span>
}</pre></div><p>
基准测试运行的结果。
</p>
<h4 id="Benchmark" data-kind="f">func <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L628">Benchmark</a> <a class="permalink" href="#Benchmark">¶</a> <a class="uses" title="List Function Callers" href="https://sourcegraph.com/-/godoc/refs?def=Benchmark&pkg=testing&repo=">Uses</a></h4>
<div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L628">❖</a><pre>func Benchmark(f func(b *<a href="#B">B</a>)) <a href="#BenchmarkResult">BenchmarkResult</a></pre></div><p>
测试单个函数。用于创建不使用 "go test" 命令的自定义基准测试。
</p>
<p>
如果 f 调用 Run,则结果将是运行其所有子基准的结果估计,该子基准在单个基准测试中不会顺序调用 Run。
</p>
<h4 id="BenchmarkResult.AllocedBytesPerOp" data-kind="m">func (BenchmarkResult) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L316">AllocedBytesPerOp</a> <a class="permalink" href="#BenchmarkResult.AllocedBytesPerOp">¶</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=BenchmarkResult%2FAllocedBytesPerOp&pkg=testing&repo=">Uses</a></h4>
<div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L316">❖</a><pre>func (r <a href="#BenchmarkResult">BenchmarkResult</a>) AllocedBytesPerOp() <a href="/builtin#int64">int64</a></pre></div>
<h4 id="BenchmarkResult.AllocsPerOp" data-kind="m">func (BenchmarkResult) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L309">AllocsPerOp</a> <a class="permalink" href="#BenchmarkResult.AllocsPerOp">¶</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=BenchmarkResult%2FAllocsPerOp&pkg=testing&repo=">Uses</a></h4>
<div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L309">❖</a><pre>func (r <a href="#BenchmarkResult">BenchmarkResult</a>) AllocsPerOp() <a href="/builtin#int64">int64</a></pre></div>
<h4 id="BenchmarkResult.MemString" data-kind="m">func (BenchmarkResult) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L343">MemString</a> <a class="permalink" href="#BenchmarkResult.MemString">¶</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=BenchmarkResult%2FMemString&pkg=testing&repo=">Uses</a></h4>
<div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L343">❖</a><pre>func (r <a href="#BenchmarkResult">BenchmarkResult</a>) MemString() <a href="/builtin#string">string</a></pre></div>
<h4 id="BenchmarkResult.NsPerOp" data-kind="m">func (BenchmarkResult) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L295">NsPerOp</a> <a class="permalink" href="#BenchmarkResult.NsPerOp">¶</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=BenchmarkResult%2FNsPerOp&pkg=testing&repo=">Uses</a></h4>
<div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L295">❖</a><pre>func (r <a href="#BenchmarkResult">BenchmarkResult</a>) NsPerOp() <a href="/builtin#int64">int64</a></pre></div>
<h4 id="BenchmarkResult.String" data-kind="m">func (BenchmarkResult) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L323">String</a> <a class="permalink" href="#BenchmarkResult.String">¶</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=BenchmarkResult%2FString&pkg=testing&repo=">Uses</a></h4>
<div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L323">❖</a><pre>func (r <a href="#BenchmarkResult">BenchmarkResult</a>) String() <a href="/builtin#string">string</a></pre></div>
<h3 id="Cover" data-kind="t">type <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/cover.go#L31">Cover</a> <a class="permalink" href="#Cover">¶</a> <a class="uses" title="List Uses of This Type" href="https://sourcegraph.com/-/godoc/refs?def=Cover&pkg=testing&repo=">Uses</a></h3>
<div class="decl" data-kind="d"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/cover.go#L31">❖</a><pre>type Cover struct {
<span id="Cover.Mode">Mode</span> <a href="/builtin#string">string</a>
<span id="Cover.Counters">Counters</span> map[<a href="/builtin#string">string</a>][]<a href="/builtin#uint32">uint32</a>
<span id="Cover.Blocks">Blocks</span> map[<a href="/builtin#string">string</a>][]<a href="#CoverBlock">CoverBlock</a>
<span id="Cover.CoveredPackages">CoveredPackages</span> <a href="/builtin#string">string</a>
}</pre></div><p>
Cover records information about test coverage checking.
NOTE: This struct is internal to the testing infrastructure and may change.
It is not covered (yet) by the Go 1 compatibility guidelines.
</p>
<h3 id="CoverBlock" data-kind="t">type <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/cover.go#L18">CoverBlock</a> <a class="permalink" href="#CoverBlock">¶</a> <a class="uses" title="List Uses of This Type" href="https://sourcegraph.com/-/godoc/refs?def=CoverBlock&pkg=testing&repo=">Uses</a></h3>
<div class="decl" data-kind="d"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/cover.go#L18">❖</a><pre>type CoverBlock struct {
<span id="CoverBlock.Line0">Line0</span> <a href="/builtin#uint32">uint32</a>
<span id="CoverBlock.Col0">Col0</span> <a href="/builtin#uint16">uint16</a>
<span id="CoverBlock.Line1">Line1</span> <a href="/builtin#uint32">uint32</a>
<span id="CoverBlock.Col1">Col1</span> <a href="/builtin#uint16">uint16</a>
<span id="CoverBlock.Stmts">Stmts</span> <a href="/builtin#uint16">uint16</a>
}</pre></div><p>
CoverBlock records the coverage data for a single basic block.
NOTE: This struct is internal to the testing infrastructure and may change.
It is not covered (yet) by the Go 1 compatibility guidelines.
</p>
<h3 id="InternalBenchmark" data-kind="t">type <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L30">InternalBenchmark</a> <a class="permalink" href="#InternalBenchmark">¶</a> <a class="uses" title="List Uses of This Type" href="https://sourcegraph.com/-/godoc/refs?def=InternalBenchmark&pkg=testing&repo=">Uses</a></h3>
<div class="decl" data-kind="d"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L30">❖</a><pre>type InternalBenchmark struct {
<span id="InternalBenchmark.Name">Name</span> <a href="/builtin#string">string</a>
<span id="InternalBenchmark.F">F</span> func(b *<a href="#B">B</a>)
}</pre></div><p>
An internal type but exported because it is cross-package; part of the implementation
of the "go test" command.
</p>
<h3 id="InternalExample" data-kind="t">type <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/example.go#L17">InternalExample</a> <a class="permalink" href="#InternalExample">¶</a> <a class="uses" title="List Uses of This Type" href="https://sourcegraph.com/-/godoc/refs?def=InternalExample&pkg=testing&repo=">Uses</a></h3>
<div class="decl" data-kind="d"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/example.go#L17">❖</a><pre>type InternalExample struct {
<span id="InternalExample.Name">Name</span> <a href="/builtin#string">string</a>
<span id="InternalExample.F">F</span> func()
<span id="InternalExample.Output">Output</span> <a href="/builtin#string">string</a>
<span id="InternalExample.Unordered">Unordered</span> <a href="/builtin#bool">bool</a>
}</pre></div>
<h3 id="InternalTest" data-kind="t">type <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L597">InternalTest</a> <a class="permalink" href="#InternalTest">¶</a> <a class="uses" title="List Uses of This Type" href="https://sourcegraph.com/-/godoc/refs?def=InternalTest&pkg=testing&repo=">Uses</a></h3>
<div class="decl" data-kind="d"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L597">❖</a><pre>type InternalTest struct {
<span id="InternalTest.Name">Name</span> <a href="/builtin#string">string</a>
<span id="InternalTest.F">F</span> func(*<a href="#T">T</a>)
}</pre></div><p>
An internal type but exported because it is cross-package; part of the implementation
of the "go test" command.
</p>
<h3 id="M" data-kind="t">type <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L779">M</a> <a class="permalink" href="#M">¶</a> <a class="uses" title="List Uses of This Type" href="https://sourcegraph.com/-/godoc/refs?def=M&pkg=testing&repo=">Uses</a></h3>
<div class="decl" data-kind="d"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L779">❖</a><pre>type M struct {
<span class="com">// contains filtered or unexported fields</span>
}</pre></div><p>
M 是传递给 TestMain 函数以运行实际测试的类型。
</p>
<h4 id="MainStart" data-kind="f">func <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L801">MainStart</a> <a class="permalink" href="#MainStart">¶</a> <a class="uses" title="List Function Callers" href="https://sourcegraph.com/-/godoc/refs?def=MainStart&pkg=testing&repo=">Uses</a></h4>
<div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L801">❖</a><pre>func MainStart(deps testDeps, tests []<a href="#InternalTest">InternalTest</a>, benchmarks []<a href="#InternalBenchmark">InternalBenchmark</a>, examples []<a href="#InternalExample">InternalExample</a>) *<a href="#M">M</a></pre></div><p>
MainStart is meant for use by tests generated by 'go test'.
It is not meant to be called directly and is not subject to the Go 1 compatibility document.
It may change signature from release to release.
</p>
<h4 id="M.Run" data-kind="m">func (*M) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L811">Run</a> <a class="permalink" href="#M.Run">¶</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=M%2FRun&pkg=testing&repo=">Uses</a></h4>
<div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L811">❖</a><pre>func (m *<a href="#M">M</a>) Run() <a href="/builtin#int">int</a></pre></div><p>
Run 运行这些测试。它返回要传递给 os.Exit 的退出代码。
</p>
<h3 id="PB" data-kind="t">type <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L540">PB</a> <a class="permalink" href="#PB">¶</a> <a class="uses" title="List Uses of This Type" href="https://sourcegraph.com/-/godoc/refs?def=PB&pkg=testing&repo=">Uses</a></h3>
<div class="decl" data-kind="d"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L540">❖</a><pre>type PB struct {
<span class="com">// contains filtered or unexported fields</span>
}</pre></div><p>
PB 被 RunParallel 使用来运行并行基准测试。
</p>
<h4 id="PB.Next" data-kind="m">func (*PB) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L548">Next</a> <a class="permalink" href="#PB.Next">¶</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=PB%2FNext&pkg=testing&repo=">Uses</a></h4>
<div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/benchmark.go#L548">❖</a><pre>func (pb *<a href="#PB">PB</a>) Next() <a href="/builtin#bool">bool</a></pre></div><p>
Next 判断是否有更多的迭代要执行
</p>
<h3 id="T" data-kind="t">type <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L414">T</a> <a class="permalink" href="#T">¶</a> <a class="uses" title="List Uses of This Type" href="https://sourcegraph.com/-/godoc/refs?def=T&pkg=testing&repo=">Uses</a></h3>
<div class="decl" data-kind="d"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L414">❖</a><pre>type T struct {
<span class="com">// contains filtered or unexported fields</span>
}</pre></div><p>
T 是传递给测试函数的一种类型,它用于管理测试状态并支持格式化测试日志。测试日志会在执行测试的过程中不断累积, 并在测试完成时转储至标准输出。
</p>
<p>
当一个测试的测试函数返回时, 又或者当一个测试函数调用 FailNow 、 Fatal 、 Fatalf 、 SkipNow 、 Skip 或者 Skipf 中的任意一个时, 该测试即宣告结束。 跟 Parallel 方法一样, 以上提到的这些方法只能在运行测试函数的 goroutine 中调用。
</p>
<p>
至于其他报告方法, 比如 Log 以及 Error 的变种, 则可以在多个 goroutine 中同时进行调用。
</p>
<h4 id="T.Error" data-kind="m">func (*T) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L510">Error</a> <a class="permalink" href="#T.Error">¶</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=common%2FError&pkg=testing&repo=">Uses</a></h4>
<div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L510">❖</a><pre>func (c *T) Error(args ...interface{})</pre></div><p>
调用 Error 相当于在调用 Log 之后调用 Fail 。
</p>
<h4 id="T.Errorf" data-kind="m">func (*T) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L516">Errorf</a> <a class="permalink" href="#T.Errorf">¶</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=common%2FErrorf&pkg=testing&repo=">Uses</a></h4>
<div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L516">❖</a><pre>func (c *T) Errorf(format <a href="/builtin#string">string</a>, args ...interface{})</pre></div><p>
调用 Errorf 相当于在调用 Logf 之后调用 Fail 。
</p>
<h4 id="T.Fail" data-kind="m">func (*T) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L437">Fail</a> <a class="permalink" href="#T.Fail">¶</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=common%2FFail&pkg=testing&repo=">Uses</a></h4>
<div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L437">❖</a><pre>func (c *T) Fail()</pre></div><p>
将当前测试标识为失败,但是仍继续执行该测试。
</p>
<h4 id="T.FailNow" data-kind="m">func (*T) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L463">FailNow</a> <a class="permalink" href="#T.FailNow">¶</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=common%2FFailNow&pkg=testing&repo=">Uses</a></h4>
<div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L463">❖</a><pre>func (c *T) FailNow()</pre></div><p>
将当前测试标识为失败并停止执行该测试,在此之后,测试过程将在下一个测试或者下一个基准测试中继续。
</p>
<p>
FailNow 必须在运行测试函数或者基准测试函数的 goroutine 中调用,而不能在测试期间创建的 goroutine 中调用。调用 FailNow 不会导致其他 goroutine 停止。
</p>
<h4 id="T.Failed" data-kind="m">func (*T) <a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L451">Failed</a> <a class="permalink" href="#T.Failed">¶</a> <a class="uses" title="List Method Callers" href="https://sourcegraph.com/-/godoc/refs?def=common%2FFailed&pkg=testing&repo=">Uses</a></h4>
<div class="funcdecl decl"><a title="View Source" href="https://github.com/golang/go/blob/master/src/testing/testing.go#L451">❖</a><pre>func (c *T) Failed() <a href="/builtin#bool">bool</a></pre></div><p>
Failed 用于报告测试函数是否已失败。
</p>