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