Golangでテストコードを書く
Sep 2, 2018 · 2 min read
- Go 1.10.2
- OSX HighSierra
標準パッケージのtestingとサードパーティのstretchr/testifyを使う。
以下の内容でhello_test.goを作成。
package main
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestHello(t *testing.T) {
assert.Equal(t, true, true)
}要点は、
- ファイル名の末尾を
_test.goにする。 - 関数名は
Testで始める。 - テスト関数に
testing.Tのポインタ型の引数をとる - testingにはassertがないのでtestify/assertをインポートして用いる
実行するには、
go test -run ""