go
генерира автоматична документация на нашия код, вземайки под внимание:
/* Example fobonacci numbers implementation */ package fibonacci
// Fastest Fibonacci Implementation func FibonacciFastest(n uint64) uint64 {
// lookupTable stores computed results from FibonacciFast or FibonacciFastest. var lookupTable = map[uint64]uint64{}
На всички локално инсталирани пакети
godoc -http=:6060
Документация на (почти) всички go пакети качени в BitBucket, GitHub, Launchpad и Google Project Hosting
testing.B
Benchmark
и слеващата буква да е главнаfor
цикъл, извикващ b.N
пъти тестваната функцияgo
е достатъчно умен да реши колко пъти да я извика, за да получи адекватни резултатиgithub.com/ChristianSiegert/go-testing-example
func BenchmarkFibonacciFastest(b *testing.B) { for i := 0; i < b.N; i++ { FibonacciFastest(40) } }
func testingFunc(tb testing.TB, input, expectedResult int) { if result := Fibonacci(input); result != expectedResult { tb.Fatalf("Expected %d for Fiboncci(%d) but got %d", expectedResult, input, result) } }
func TestFibonacci(t *testing.T) { var tests = []struct { input, result int }{ {input: 2, result: 1}, {input: 8, result: 21}, {input: 10, result: 55}, } for _, test := range tests { testingFunc(t, test.input, test.result) } }
func TestSubFibonacci(t *testing.T) { var tests = []struct { input, result int }{ {input: 2, result: 1}, {input: 8, result: 21}, {input: 10, result: 55}, } for _, test := range tests { t.Run(fmt.Sprintf("Fibonacci(%d)", test.input), func(t *testing.T) { testingFunc(t, test.input, test.result) }) } }
--- FAIL: TestFibonacci (0.00s) table_test.go:68: Expected 1 for Fiboncci(2) but got 21 --- FAIL: TestSubFibonacci (0.00s) --- FAIL: TestSubFibonacci/Fibonacci(2) (0.00s) table_test.go:68: Expected 1 for Fiboncci(2) but got 21 --- FAIL: TestSubFibonacci/Fibonacci(10) (0.00s) table_test.go:68: Expected 55 for Fiboncci(10) but got 21 FAIL
func BenchmarkSubFibonacci(b *testing.B) { var tests = []struct { input, result int }{ {input: 2, result: 1}, {input: 8, result: 21}, {input: 10, result: 55}, } for _, test := range tests { b.Run(fmt.Sprintf("BFibonacci(%d)", test.input), func(b *testing.B) { for i := 0; i < b.N; i++ { testingFunc(b, test.input, test.result) } }) } }
--- FAIL: BenchmarkSubFibonacci/BFibonacci(2) table_test.go:68: Expected 1 for Fiboncci(2) but got 21 BenchmarkSubFibonacci/BFibonacci(8)-16 1000000000 2.65 ns/op --- FAIL: BenchmarkSubFibonacci/BFibonacci(10) table_test.go:68: Expected 55 for Fiboncci(10) but got 21 --- FAIL: BenchmarkSubFibonacci FAIL
func TestGroupSubFibonacci(t *testing.T) { t.Run("group1", func(t *testing.T) { for _, test := range tests { t.Run(fmt.Sprintf("Fibonacci(%d)", test.input), func(t *testing.T) { var input, result = test.input, test.result t.Parallel() time.Sleep(time.Second) testingFunc(t, input, result) }) } t.Run("NonParallel", func(t *testing.T) { t.Fatal("Just cause") }) t.Fatal("Oops") t.Run("NonParallel2", func(t *testing.T) { t.Fatal("Just Cause II") }) }) }
--- FAIL: TestGroupSubFibonacci (1.00s) --- FAIL: TestGroupSubFibonacci/group1 (0.00s) --- FAIL: TestGroupSubFibonacci/group1/NonParallel (0.00s) table_test.go:96: Just cause table_test.go:98: Oops --- FAIL: TestGroupSubFibonacci/group1/Fibonacci(2) (1.00s) table_test.go:69: Expected 1 for Fiboncci(2) but got 21 --- FAIL: TestGroupSubFibonacci/group1/Fibonacci(10) (1.00s) table_test.go:69: Expected 55 for Fiboncci(10) but got 21 FAIL
Example
, последвана от името на типа или функциятаFoo -> ExampleFoo
Bar
го слагаме с подчертавка след типа ExampleFoo_Bar
Output:
и ще бъде тествано че изхода на кода съвпада с останалата част от коментараfunc ExampleHello() { Hello("hello") // Output: // hello }
type Sleeper interface { Sleep(time.Duration) }
mock
пакет