?
本文檔使用 PHP中文網(wǎng)手冊(cè) 發(fā)布
import "testing"
概觀
索引
示例
子目錄
軟件包測(cè)試為 Go 軟件包的自動(dòng)化測(cè)試提供支持。它旨在與“go test”命令配合使用,該命令可自動(dòng)執(zhí)行表單的任何功能
func TestXxx(*testing.T)
其中 Xxx 可以是任何字母數(shù)字字符串(但第一個(gè)字母不能在 a-z 中)并用于標(biāo)識(shí)測(cè)試?yán)獭?/p>
在這些功能中,使用錯(cuò)誤,失敗或相關(guān)方法發(fā)送失敗信號(hào)。
要編寫(xiě)一個(gè)新的測(cè)試套件,請(qǐng)創(chuàng)建一個(gè)名稱以 _test.go 結(jié)尾的文件,其中包含 TestXxx 函數(shù),如此處所述。將該文件放在與正在測(cè)試的文件相同的包中。該文件將從常規(guī)軟件包版本中排除,但會(huì)在運(yùn)行“go test”命令時(shí)包含該文件。有關(guān)更多詳細(xì)信息,請(qǐng)運(yùn)行“go help test”和“go help testflag”。
如果對(duì) * T 和 * B 的 Skip 方法的調(diào)用不適用,測(cè)試和基準(zhǔn)可能會(huì)被跳過(guò):
func TestTimeConsuming(t *testing.T) { if testing.Short() { t.Skip("skipping test in short mode.") } ...}
表單的功能
func BenchmarkXxx(*testing.B)
被認(rèn)為是基準(zhǔn)測(cè)試,并且當(dāng)它的 -bench 標(biāo)志被提供時(shí)由“go test”命令執(zhí)行。基準(zhǔn)按順序運(yùn)行。
示例基準(zhǔn)函數(shù)如下所示:
func BenchmarkHello(b *testing.B) { for i := 0; i < b.N; i++ { fmt.Sprintf("hello") }}
基準(zhǔn)函數(shù)必須運(yùn)行目標(biāo)代碼 b.N 倍。在執(zhí)行基準(zhǔn)測(cè)試期間,將調(diào)整 b.N,直到基準(zhǔn)測(cè)試功能持續(xù)足夠長(zhǎng)時(shí)間以可靠定時(shí)。輸出
BenchmarkHello 10000000 282 ns/op
意味著循環(huán)以每個(gè)循環(huán) 282 ns 的速度運(yùn)行10000000次。
如果基準(zhǔn)測(cè)試在運(yùn)行之前需要昂貴的設(shè)置,則計(jì)時(shí)器可能會(huì)重置:
func BenchmarkBigLen(b *testing.B) { big := NewBig() b.ResetTimer() for i := 0; i < b.N; i++ { big.Len() }}
如果基準(zhǔn)測(cè)試需要在并行設(shè)置中測(cè)試性能,則可以使用 RunParalle l幫助函數(shù); 這樣的基準(zhǔn)測(cè)試旨在與 go 測(cè)試 -cpu 標(biāo)志一起使用:
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") } })}
該軟件包還運(yùn)行并驗(yàn)證示例代碼。示例函數(shù)可能包含以“Output:”開(kāi)頭的結(jié)束語(yǔ)注釋,并在運(yùn)行測(cè)試時(shí)與函數(shù)的標(biāo)準(zhǔn)輸出進(jìn)行比較。(比較忽略前導(dǎo)空間和尾隨空間。)以下是一個(gè)示例的示例:
func ExampleHello() { fmt.Println("hello") // Output: hello}func ExampleSalutations() { fmt.Println("hello, and") fmt.Println("goodbye") // Output: // hello, and // goodbye}
注釋前綴“Unordered output:”與“Output:”類似,但匹配任何行順序:
func ExamplePerm() { for _, value := range Perm(4) { fmt.Println(value) } // Unordered output: 4 // 2 // 1 // 3 // 0}
沒(méi)有輸出注釋的示例函數(shù)被編譯但不被執(zhí)行。
聲明該包的示例的命名約定,類型 T 上的函數(shù) F,類型 T 和方法 M 是:
func Example() { ... }func ExampleF() { ... }func ExampleT() { ... }func ExampleT_M() { ... }
可以通過(guò)為名稱添加一個(gè)不同的后綴來(lái)提供包/類型/函數(shù)/方法的多個(gè)示例函數(shù)。后綴必須以小寫(xiě)字母開(kāi)頭。
func Example_suffix() { ... }func ExampleF_suffix() { ... }func ExampleT_suffix() { ... }func ExampleT_M_suffix() { ... }
當(dāng)整個(gè)測(cè)試文件包含單個(gè)示例函數(shù),至少一個(gè)其他函數(shù),類型,變量或常量聲明,并且沒(méi)有測(cè)試或基準(zhǔn)測(cè)試函數(shù)時(shí),將以整個(gè)測(cè)試文件為例。
T 和 B 的 Run 方法允許定義子測(cè)試和子基準(zhǔn),而不必為每個(gè)測(cè)試定義單獨(dú)的功能。這使得像表驅(qū)動(dòng)的基準(zhǔn)測(cè)試和創(chuàng)建分層測(cè)試成為可能。它還提供了一種共享通用設(shè)置和拆卸代碼的方法:
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>}
每個(gè)子測(cè)試和子基準(zhǔn)具有唯一的名稱:頂級(jí)測(cè)試的名稱和傳遞給 Run 的名稱序列的組合,用斜杠分隔,并帶有可選的尾部序列號(hào)以消除歧義。
-run 和 -bench 命令行標(biāo)志的參數(shù)是與測(cè)試名稱匹配的未錨定正則表達(dá)式。對(duì)于具有多個(gè)斜杠分隔元素(例如子測(cè)試)的測(cè)試,參數(shù)本身是斜線分隔的,表達(dá)式依次匹配每個(gè)名稱元素。由于它是未錨定的,因此一個(gè)空的表達(dá)式匹配任何字符串。例如,使用“匹配”來(lái)表示“其名稱包含”:
go test -run '' # Run all tests.go test -run Foo # Run top-level tests matching "Foo", such as "TestFooBar".go test -run Foo/A= # For top-level tests matching "Foo", run subtests matching "A=".go test -run /A=1 # For all top-level tests, run subtests matching "A=1".
子測(cè)試也可以用來(lái)控制并行性。家長(zhǎng)測(cè)試只有在完成所有分測(cè)驗(yàn)后才能完成。在這個(gè)例子中,所有的測(cè)試都是相互平行的,并且只與對(duì)方一起運(yùn)行,而不管可能定義的其他頂級(jí)測(cè)試:
func TestGroupedParallel(t *testing.T) { for _, tc := range tests { tc := tc // capture range variable t.Run(tc.Name, func(t *testing.T) { t.Parallel() ... }) }}
運(yùn)行直到并行子測(cè)試完成才會(huì)返回,這提供了一種在一組并行測(cè)試后進(jìn)行清理的方法:
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>}
測(cè)試程序有時(shí)需要在測(cè)試之前或之后進(jìn)行額外的設(shè)置或拆卸。測(cè)試有時(shí)還需要控制在主線程上運(yùn)行哪些代碼。為了支持這些和其他情況,如果測(cè)試文件包含一個(gè)函數(shù):
func TestMain(m *testing.M)
那么生成的測(cè)試將調(diào)用 TestMain(m) ,而不是直接運(yùn)行測(cè)試。 TestMain 在主要的例程中運(yùn)行,并且可以在 m.Run 的調(diào)用周?chē)M(jìn)行任何設(shè)置和拆卸。然后它應(yīng)該用 m.Run 的結(jié)果調(diào)用 os.Exit 。當(dāng)調(diào)用 TestMain 時(shí), flag.Parse 尚未運(yùn)行。如果 TestMain 依賴于命令行標(biāo)志,包括那些測(cè)試包,它應(yīng)該明確調(diào)用 flag.Parse。
A simple implementation of TestMain is:
func TestMain(m *testing.M) {// call flag.Parse() here if TestMain uses flags os.Exit(m.Run())}
func AllocsPerRun(runs int, f func()) (avg float64)
func CoverMode() string
func Coverage() float64
func Main(matchString func(pat, str string) (bool, error), tests []InternalTest, benchmarks []InternalBenchmark, examples []InternalExample)
func RegisterCover(c Cover)
func RunBenchmarks(matchString func(pat, str string) (bool, error), benchmarks []InternalBenchmark)
func RunExamples(matchString func(pat, str string) (bool, error), examples []InternalExample) (ok bool)
func RunTests(matchString func(pat, str string) (bool, error), tests []InternalTest) (ok bool)
func Short() bool
func Verbose() bool
type B
func (c *B) Error(args ...interface{})
func (c *B) Errorf(format string, args ...interface{})
func (c *B) Fail()
func (c *B) FailNow()
func (c *B) Failed() bool
func (c *B) Fatal(args ...interface{})
func (c *B) Fatalf(format string, args ...interface{})
func (c *B) Helper()
func (c *B) Log(args ...interface{})
func (c *B) Logf(format string, args ...interface{})
func (c *B) Name() string
func (b *B) ReportAllocs()
func (b *B) ResetTimer()
func (b *B) Run(name string, f func(b *B)) bool
func (b *B) RunParallel(body func(*PB))
func (b *B) SetBytes(n int64)
func (b *B) SetParallelism(p int)
func (c *B) Skip(args ...interface{})
func (c *B) SkipNow()
func (c *B) Skipf(format string, args ...interface{})
func (c *B) Skipped() bool
func (b *B) StartTimer()
func (b *B) StopTimer()
type BenchmarkResult
func Benchmark(f func(b *B)) BenchmarkResult
func (r BenchmarkResult) AllocedBytesPerOp() int64
func (r BenchmarkResult) AllocsPerOp() int64
func (r BenchmarkResult) MemString() string
func (r BenchmarkResult) NsPerOp() int64
func (r BenchmarkResult) String() string
type Cover
type CoverBlock
type InternalBenchmark
type InternalExample
type InternalTest
type M
func MainStart(deps testDeps, tests []InternalTest, benchmarks []InternalBenchmark, examples []InternalExample) *M
func (m *M) Run() int
type PB
func (pb *PB) Next() bool
type T
func (c *T) Error(args ...interface{})
func (c *T) Errorf(format string, args ...interface{})
func (c *T) Fail()
func (c *T) FailNow()
func (c *T) Failed() bool
func (c *T) Fatal(args ...interface{})
func (c *T) Fatalf(format string, args ...interface{})
func (c *T) Helper()
func (c *T) Log(args ...interface{})
func (c *T) Logf(format string, args ...interface{})
func (c *T) Name() string
func (t *T) Parallel()
func (t *T) Run(name string, f func(t *T)) bool
func (c *T) Skip(args ...interface{})
func (c *T) SkipNow()
func (c *T) Skipf(format string, args ...interface{})
func (c *T) Skipped() bool
type TB
B.RunParallel
allocs.go benchmark.go cover.go example.go match.go testing.go
func AllocsPerRun(runs int, f func()) (avg float64)
AllocsPerRun 返回f期間的平均分配數(shù)量。盡管返回值的類型為 float64 ,但它始終是一個(gè)整數(shù)值。
要計(jì)算分配數(shù)量,函數(shù)將首先作為熱身運(yùn)行一次。然后測(cè)量并返回指定運(yùn)行次數(shù)內(nèi)的平均分配數(shù)量。
AllocsPerRun 在測(cè)量過(guò)程中將 GOMAXPROCS 設(shè)置為1,并在返回之前將其恢復(fù)。
func CoverMode() string
CoverMode 報(bào)告測(cè)試覆蓋模式的設(shè)置。值為“set”,“count”或“atomic”。如果測(cè)試覆蓋未啟用,返回值將為空。
func Coverage() float64
Coverage 將當(dāng)前代碼覆蓋范圍報(bào)告為范圍0,1中的一個(gè)分?jǐn)?shù)。如果未啟用 Coverage ,則 Coverage 返回0。
當(dāng)運(yùn)行大量順序測(cè)試用例時(shí),在每個(gè)測(cè)試用例之后檢查 Coverage 對(duì)于識(shí)別哪些測(cè)試用例可以使用新代碼路徑很有用。它不會(huì)替代 'go test -cover' 和 'go tool cover' 生成的報(bào)告。
func Main(matchString func(pat, str string) (bool, error), tests []InternalTest, benchmarks []InternalBenchmark, examples []InternalExample)
Main 是一個(gè)內(nèi)部函數(shù),是執(zhí)行“go test”命令的一部分。它被出口,因?yàn)樗墙徊姘b并且早于“內(nèi)部”包裝。它不再被“去測(cè)試”使用,而是盡可能地保留在模擬使用Main進(jìn)行“測(cè)試”的其他系統(tǒng)上,但是由于新功能被添加到測(cè)試包中,Main有時(shí)無(wú)法更新。模擬“go test”的系統(tǒng)應(yīng)該更新為使用 MainStart。
func RegisterCover(c Cover)
RegisterCover 記錄測(cè)試的覆蓋率數(shù)據(jù)累加器。注意:此功能是測(cè)試基礎(chǔ)架構(gòu)的內(nèi)部功能,可能會(huì)更改。Go 1 兼容性指南并未涵蓋此內(nèi)容。
func RunBenchmarks(matchString func(pat, str string) (bool, error), benchmarks []InternalBenchmark)
內(nèi)部函數(shù),因?yàn)樗墙徊姘b而被導(dǎo)出;部分執(zhí)行“go test”命令。
func RunExamples(matchString func(pat, str string) (bool, error), examples []InternalExample) (ok bool)
內(nèi)部函數(shù),因?yàn)樗墙徊姘b而被導(dǎo)出;部分執(zhí)行“go test”命令。
func RunTests(matchString func(pat, str string) (bool, error), tests []InternalTest) (ok bool)
內(nèi)部函數(shù),因?yàn)樗墙徊姘b而被導(dǎo)出;部分執(zhí)行“go test”命令。
func Short() bool
Short 報(bào)告是否設(shè)置了 -test.short 標(biāo)志。
func Verbose() bool
詳細(xì)報(bào)告是否設(shè)置了 -test.v 標(biāo)志。
B 是一種傳遞給 Benchmark 函數(shù)的類型,用于管理基準(zhǔn)計(jì)時(shí)并指定要運(yùn)行的迭代次數(shù)。
當(dāng) Benchmark 函數(shù)返回或調(diào)用 FailNow,F(xiàn)atal,F(xiàn)atalf,SkipNow,Skip 或 Skipf 方法時(shí),基準(zhǔn)結(jié)束。這些方法只能從運(yùn)行 Benchmark 函數(shù)的 goroutine 中調(diào)用。其他報(bào)告方法(如 Log 和 Error 的變體)可以從多個(gè) goroutines 同時(shí)調(diào)用。
就像在測(cè)試中一樣,基準(zhǔn)測(cè)試日志在執(zhí)行過(guò)程中會(huì)累積并在完成時(shí)轉(zhuǎn)儲(chǔ)到標(biāo)準(zhǔn)錯(cuò)誤。與測(cè)試不同,基準(zhǔn)測(cè)試日志始終打印出來(lái),以免隱藏存在可能影響基準(zhǔn)測(cè)試結(jié)果的輸出。
type B struct { N int // contains filtered or unexported fields}
func (c *B) Error(args ...interface{})
錯(cuò)誤等同于 Log ,然后是 Fail 。
func (c *B) Errorf(format string, args ...interface{})
Errorf 等同于 Logf ,然后是 Fail 。
func (c *B) Fail()
Fail 會(huì)將該功能標(biāo)記為失敗,但會(huì)繼續(xù)執(zhí)行。
func (c *B) FailNow()
FailNow 將該函數(shù)標(biāo)記為失敗并停止其執(zhí)行。執(zhí)行將繼續(xù)在下一個(gè)測(cè)試或基準(zhǔn)。必須從運(yùn)行測(cè)試或基準(zhǔn)測(cè)試函數(shù)的 goroutine 調(diào)用 FailNow ,而不是在測(cè)試期間創(chuàng)建的其他 goutoutine 調(diào)用。調(diào)用 FailNow 不會(huì)停止那些其他的 goroutine 。
func (c *B) Failed() bool
Failed 報(bào)告功能是否失敗。
func (c *B) Fatal(args ...interface{})
Fatal 相當(dāng)于 Log ,然后是 FailNow 。
func (c *B) Fatalf(format string, args ...interface{})
Fatalf 等同于 Logf,然后是 FailNow。
func (c *B) Helper()
助手將調(diào)用函數(shù)標(biāo)記為測(cè)試幫助函數(shù)。打印文件和行信息時(shí),該功能將被跳過(guò)??梢詮亩鄠€(gè) goroutines 同時(shí)調(diào)用助手。如果 Heller 從 TestXxx / BenchmarkXxx 函數(shù)或子測(cè)試/次級(jí)基準(zhǔn)測(cè)試函數(shù)直接調(diào)用,則 Helper 不起作用。
func (c *B) Log(args ...interface{})
日志使用默認(rèn)格式化格式化其參數(shù),類似于 Println ,并將文本記錄在錯(cuò)誤日志中。對(duì)于測(cè)試,僅當(dāng)測(cè)試失敗或設(shè)置了 -test.v 標(biāo)志時(shí)才會(huì)打印文本。對(duì)于基準(zhǔn)測(cè)試,總是打印文本以避免性能取決于 -test.v 標(biāo)志的值。
func (c *B) Logf(format string, args ...interface{})
Logf 根據(jù)格式格式化其參數(shù),類似于 Printf ,并將文本記錄在錯(cuò)誤日志中。如果沒(méi)有提供,最后換行符會(huì)被添加。對(duì)于測(cè)試,僅當(dāng)測(cè)試失敗或設(shè)置了 -test.v 標(biāo)志時(shí)才會(huì)打印文本。對(duì)于基準(zhǔn)測(cè)試,總是打印文本以避免性能取決于 -test.v 標(biāo)志的值。
func (c *B) Name() string
Name 返回正在運(yùn)行的測(cè)試或基準(zhǔn)的名稱。
func (b *B) ReportAllocs()
ReportAllocs 為此基準(zhǔn)啟用 malloc 統(tǒng)計(jì)信息。它相當(dāng)于設(shè)置 -test.benchmem,但它只影響調(diào)用 ReportAllocs 的基準(zhǔn)函數(shù)。
func (b *B) ResetTimer()
ResetTimer 將經(jīng)過(guò)的基準(zhǔn)時(shí)間和內(nèi)存分配計(jì)數(shù)器清零。它不會(huì)影響計(jì)時(shí)器是否正在運(yùn)行。
func (b *B) Run(name string, f func(b *B)) bool
運(yùn)行基準(zhǔn)f作為具有給定名稱的子基準(zhǔn)。它報(bào)告是否有任何失敗。
子基準(zhǔn)與其他基準(zhǔn)相似。調(diào)用 Run 至少一次的基準(zhǔn)測(cè)試本身不會(huì)被測(cè)量,并且會(huì)在 N = 1 時(shí)被調(diào)用一次。
運(yùn)行可以從多個(gè) goroutine 同時(shí)調(diào)用,但所有此類調(diào)用都必須在 b 返回的外部基準(zhǔn)測(cè)試函數(shù)之前返回。
func (b *B) RunParallel(body func(*PB))
RunParallel 并行運(yùn)行一個(gè)基準(zhǔn)。它創(chuàng)建了多個(gè) goroutine 并在它們之間分配 b.N 迭代。goroutines 的數(shù)量默認(rèn)為 GOMAXPROCS 。為了增加非 CPU 綁定基準(zhǔn)的并行性,請(qǐng)?jiān)赗unParallel 之前調(diào)用 SetParallelism。RunParalle l通常與 go-test 標(biāo)志一起使用。
body 函數(shù)將在每個(gè) goroutine 中運(yùn)行。它應(yīng)該設(shè)置任何 goroutine-local 狀態(tài),然后迭代直到 pb.Next 返回 false。它不應(yīng)該使用 StartTimer,StopTimer 或 ResetTimer 函數(shù),因?yàn)樗鼈兙哂腥中Ч?。它也不?yīng)該叫 Run。
package mainimport ("bytes""testing""text/template")func main() {// Parallel benchmark for text/template.Template.Execute on a single object. testing.Benchmark(func(b *testing.B) { templ := template.Must(template.New("test").Parse("Hello, {{.}}!"))// RunParallel will create GOMAXPROCS goroutines// and distribute work among them. b.RunParallel(func(pb *testing.PB) {// Each goroutine has its own bytes.Buffer.var buf bytes.Bufferfor pb.Next() {// The loop body is executed b.N times total across all goroutines. buf.Reset() templ.Execute(&buf, "World")}})})}
func (b *B) SetBytes(n int64)
SetBytes 記錄單個(gè)操作中處理的字節(jié)數(shù)。如果這被調(diào)用,基準(zhǔn)將報(bào)告 ns / op和MB / s 。
func (b *B) SetParallelism(p int)
SetParallelism 將 RunParallel 使用的 goroutines 的數(shù)量設(shè)置為 p * GOMAXPROCS。通常不需要為 CPU 綁定的基準(zhǔn)調(diào)用 SetParallelism。如果 p 小于1,該調(diào)用將不起作用。
func (c *B) Skip(args ...interface{})
跳過(guò)相當(dāng)于 Log,然后是 SkipNow。
func (c *B) SkipNow()
SkipNow 將測(cè)試標(biāo)記為已被跳過(guò)并停止執(zhí)行。如果測(cè)試失?。ㄕ?qǐng)參閱錯(cuò)誤,Errorf,失?。?,然后跳過(guò),它仍然被認(rèn)為失敗。執(zhí)行將繼續(xù)在下一個(gè)測(cè)試或基準(zhǔn)。另請(qǐng)參閱 FailNow。必須從運(yùn)行測(cè)試的 goroutine 調(diào)用 SkipNow,而不是從測(cè)試期間創(chuàng)建的其他 goutoutine 調(diào)用。調(diào)用 SkipNow 不會(huì)停止那些其他的 goroutines。
func (c *B) Skipf(format string, args ...interface{})
Skipf 相當(dāng)于 Logf,后跟 SkipNow。
func (c *B) Skipped() bool
Skipped 報(bào)告是否跳過(guò)測(cè)試。
func (b *B) StartTimer()
StartTimer 開(kāi)始計(jì)時(shí)測(cè)試。該功能在基準(zhǔn)測(cè)試開(kāi)始之前自動(dòng)調(diào)用,但它也可用于在調(diào)用 StopTimer 之后恢復(fù)計(jì)時(shí)。
func (b *B) StopTimer()
StopTimer 停止計(jì)時(shí)測(cè)試。這可用于在執(zhí)行復(fù)雜的初始化時(shí)暫停計(jì)時(shí)器,而不需要進(jìn)行測(cè)量。
基準(zhǔn)測(cè)試的結(jié)果。
type BenchmarkResult struct { N int // The number of iterations. T time.Duration // The total time taken. Bytes int64 // Bytes processed in one iteration. MemAllocs uint64 // The total number of memory allocations. MemBytes uint64 // The total number of bytes allocated.}
func Benchmark(f func(b *B)) BenchmarkResult
基準(zhǔn)測(cè)試標(biāo)準(zhǔn)的單一功能。用于創(chuàng)建不使用“go test”命令的自定義基準(zhǔn)。
如果 f 調(diào)用 Run,則結(jié)果將是運(yùn)行其所有不調(diào)用單個(gè)基準(zhǔn)測(cè)試中順序運(yùn)行的子基準(zhǔn)的估計(jì)值。
func (r BenchmarkResult) AllocedBytesPerOp() int64
AllocedBytesPerOp 返回 r.MemBytes / rN
func (r BenchmarkResult) AllocsPerOp() int64
AllocsPerOp 返回 r.MemAllocs / rN
func (r BenchmarkResult) MemString() string
MemString 以' go test '的格式返回 r.AllocedBytesPerOp 和 r.AllocsPerOp。
func (r BenchmarkResult) NsPerOp() int64
func (r BenchmarkResult) String() string
Cover 記錄了測(cè)試覆蓋率檢查的信息。注意:此結(jié)構(gòu)是測(cè)試基礎(chǔ)架構(gòu)的內(nèi)部結(jié)構(gòu),可能會(huì)更改。Go 1 兼容性指南并未涵蓋此內(nèi)容。
type Cover struct { Mode string Counters map[string][]uint32 Blocks map[string][]CoverBlock CoveredPackages string}
CoverBlock 記錄單個(gè)基本塊的覆蓋率數(shù)據(jù)。注意:此結(jié)構(gòu)是測(cè)試基礎(chǔ)架構(gòu)的內(nèi)部結(jié)構(gòu),可能會(huì)更改。Go 1 兼容性指南并未涵蓋此內(nèi)容。
type CoverBlock struct { Line0 uint32 Col0 uint16 Line1 uint32 Col1 uint16 Stmts uint16}
內(nèi)部類型,但因?yàn)槭墙徊姘b而被導(dǎo)出; 部分執(zhí)行“go test”命令。
type InternalBenchmark struct { Name string F func(b *B)}
type InternalExample struct { Name string F func() Output string Unordered bool}
內(nèi)部類型,但因?yàn)槭墙徊姘b而被導(dǎo)出; 部分執(zhí)行“go test”命令。
type InternalTest struct { Name string F func(*T)}
M 是傳遞給 TestMain 函數(shù)來(lái)運(yùn)行實(shí)際測(cè)試的類型。
type M struct { // contains filtered or unexported fields}
func MainStart(deps testDeps, tests []InternalTest, benchmarks []InternalBenchmark, examples []InternalExample) *M
MainStart 旨在供'go test'生成的測(cè)試使用。這并不意味著直接被調(diào)用,并且不受 Go 1 兼容性文檔的約束。它可能會(huì)改變從發(fā)布到發(fā)布的簽名。
func (m *M) Run() int
Run 運(yùn)行測(cè)試。它返回一個(gè)退出代碼傳遞給 os.Exit。
RunParallel 使用 PB 來(lái)運(yùn)行平行基準(zhǔn)。
type PB struct { // contains filtered or unexported fields}
func (pb *PB) Next() bool
Next 報(bào)告是否有更多的迭代要執(zhí)行。
T 是傳遞給測(cè)試功能以管理測(cè)試狀態(tài)并支持格式化測(cè)試日志的類型。日志在執(zhí)行過(guò)程中累計(jì),完成后轉(zhuǎn)儲(chǔ)到標(biāo)準(zhǔn)輸出。
當(dāng)測(cè)試函數(shù)返回或調(diào)用任何方法 FailNow,F(xiàn)atal,F(xiàn)atalf,SkipNow,Skip 或 Skip f時(shí),測(cè)試結(jié)束。這些方法以及 Parallel 方法只能從運(yùn)行 Test 函數(shù)的 goroutine 中調(diào)用。
其他報(bào)告方法(如 Log 和 Error 的變體)可以從多個(gè) goroutines 同時(shí)調(diào)用。
type T struct { // contains filtered or unexported fields}
func (c *T) Error(args ...interface{})
Error 等同于 Log,然后是 Fail。
func (c *T) Errorf(format string, args ...interface{})
Errorf 等同于 Logf,然后是 Fail。
func (c *T) Fail()
Fail 會(huì)將該功能標(biāo)記為失敗,但會(huì)繼續(xù)執(zhí)行。
func (c *T) FailNow()
FailNow 將該函數(shù)標(biāo)記為失敗并停止其執(zhí)行。執(zhí)行將繼續(xù)在下一個(gè)測(cè)試或基準(zhǔn)。必須從運(yùn)行測(cè)試或基準(zhǔn)測(cè)試函數(shù)的 goroutine 調(diào)用 FailNow,而不是在測(cè)試期間創(chuàng)建的其他 goutoutine 調(diào)用。調(diào)用 FailNow 不會(huì)停止那些其他的 goroutine。
func (c *T) Failed() bool
Failed 的報(bào)告功能是否失敗。
func (c *T) Fatal(args ...interface{})
Fatal 相當(dāng)于 Log,然后是 FailNow。
func (c *T) Fatalf(format string, args ...interface{})
Fatalf 等同于 Logf,然后是 FailNow。
func (c *T) Helper()
Helper 將調(diào)用函數(shù)標(biāo)記為測(cè)試幫助函數(shù)。打印文件和行信息時(shí),該功能將被跳過(guò)??梢詮亩鄠€(gè) goroutines 同時(shí)調(diào)用 Helper 。如果 Heller 從 TestXxx / BenchmarkXxx 函數(shù)或子測(cè)試/次級(jí)基準(zhǔn)測(cè)試函數(shù)直接調(diào)用,則 Helper 不起作用。
func (c *T) Log(args ...interface{})
日志使用默認(rèn)格式化格式化其參數(shù),類似于 Println,并將文本記錄在錯(cuò)誤日志中。對(duì)于測(cè)試,僅當(dāng)測(cè)試失敗或設(shè)置了 -test.v 標(biāo)志時(shí)才會(huì)打印文本。對(duì)于基準(zhǔn)測(cè)試,總是打印文本以避免性能取決于 -test.v 標(biāo)志的值。
func (c *T) Logf(format string, args ...interface{})
Logf 根據(jù)格式格式化其參數(shù),類似于 Printf,并將文本記錄在錯(cuò)誤日志中。如果沒(méi)有提供,最后換行符會(huì)被添加。對(duì)于測(cè)試,僅當(dāng)測(cè)試失敗或設(shè)置了 -test.v 標(biāo)志時(shí)才會(huì)打印文本。對(duì)于基準(zhǔn)測(cè)試,總是打印文本以避免性能取決于 -test.v 標(biāo)志的值。
func (c *T) Name() string
Name 返回正在運(yùn)行的測(cè)試或基準(zhǔn)的名稱。
func (t *T) Parallel()
并行信號(hào)表示該測(cè)試將與其他并行測(cè)試并行運(yùn)行(并且僅與其他測(cè)試并行)。當(dāng)由于使用 -test.count 或 -test.cpu 而多次運(yùn)行測(cè)試時(shí),單個(gè)測(cè)試的多個(gè)實(shí)例永遠(yuǎn)不會(huì)彼此并行運(yùn)行。
func (t *T) Run(name string, f func(t *T)) bool
Run 運(yùn)行 f 作為名為 t 的子測(cè)試。它報(bào)告f是否成功。運(yùn)行 f 在一個(gè)單獨(dú)的 goroutine 中運(yùn)行 f 并阻塞,直到所有并行子測(cè)試完成。
Run 可以從多個(gè) goroutine 同時(shí)調(diào)用,但所有這些調(diào)用必須在外部測(cè)試函數(shù)返回之前返回。
func (c *T) Skip(args ...interface{})
Skip 相當(dāng)于 Log,然后是 SkipNow。
func (c *T) SkipNow()
SkipNow 將測(cè)試標(biāo)記為已被跳過(guò)并停止執(zhí)行。如果測(cè)試失敗(請(qǐng)參閱錯(cuò)誤,Errorf,失敗),然后跳過(guò),它仍然被認(rèn)為失敗。執(zhí)行將繼續(xù)在下一個(gè)測(cè)試或基準(zhǔn)。另請(qǐng)參閱 FailNow。必須從運(yùn)行測(cè)試的 goroutine 調(diào)用 SkipNow,而不是從測(cè)試期間創(chuàng)建的其他 goutoutine 調(diào)用。調(diào)用 SkipNow 不會(huì)停止那些其他的 goroutines。
func (c *T) Skipf(format string, args ...interface{})
Skipf 相當(dāng)于 Logf,后跟 SkipNow。
func (c *T) Skipped() bool
Skipped 報(bào)告是否跳過(guò)測(cè)試。
TB 是 T 和 B 共同的界面。
type TB interface { Error(args ...interface{}) Errorf(format string, args ...interface{}) Fail() FailNow() Failed() bool Fatal(args ...interface{}) Fatalf(format string, args ...interface{}) Log(args ...interface{}) Logf(format string, args ...interface{}) Name() string Skip(args ...interface{}) SkipNow() Skipf(format string, args ...interface{}) Skipped() bool Helper() // contains filtered or unexported methods}
名稱 | 概要 |
---|
| .. |
| iotest | 打包iotest 實(shí)現(xiàn)主要用于測(cè)試的讀者和寫(xiě)入者。|
| quick | 打包quick 實(shí)現(xiàn)實(shí)用功能,以幫助進(jìn)行黑匣子測(cè)試。|