Testing must go on

6.12.2016

В този епизод:

Документиране на кода

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 must go on

Benchmark тестове

func BenchmarkFibonacciFastest(b *testing.B) {
    for i := 0; i < b.N; i++ {
        FibonacciFastest(40)
    }
}

Demo

Сечението на testing.B и testing.T

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)
    }
}

SubTests

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)
        })
    }
}

SubTests continues

--- 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

SubBenchmarks

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)
            }
        })
    }
}

SubBenchmarks output:

--- 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

All together

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")
        })
    })
}

All together output

--- 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
func ExampleHello() {
    Hello("hello")
    // Output:
    // hello
}

Имитации

Иматации или Mock-ове

В Go

type Sleeper interface {
   Sleep(time.Duration)
}

Библиотеки за имитации

testify

mockery

mock

Стига думи с "mock"!

Пример от домашно 2

В следващият епизод

Въпроси?