Go unit test サンプル

Goでunit testを実行する場合の簡単なサンプルを作成してみる。

github.com

dog.go

package model

type Dog struct {
        Name string
        Age  int8
}

func NewDog(name string, age int8) *Dog {
        return &Dog{Name: name, Age:age}
}

dog_test.go

package model

import (
        "testing"
)

func TestSetDogBame(t *testing.T) {
        dog := NewDog("shiba",3)
        if dog.Name != "shiba" {
                t.Error("dog name should be shiba")
        }
        if dog.Age != 3 {
                t.Error("dog age should be 3")
        }
}

GOPATHを変更

export GOPATH=`pwd`

testを実行

go test ./model/

実行結果

$ go test -v ./model/
=== RUN   TestSetDogBame
--- PASS: TestSetDogBame (0.00s)
PASS
ok      0.005s

もうすこし、testingの使い方を学んでいこう、

testing - The Go Programming Language