uokadaの見逃し三振は嫌いです

ここで述べられていることは私の個人的な意見に基づくものであり、私が所属する組織には一切の関係はありません。

Goのテストフレームワークを試してみた

go言語のテスティングフレームワークについて — さにあらず

Go言語のテスト用ライブラリとGospel - ✘╹◡╹✘

上のエントリで紹介されていたgospelとGoblinが気になったのでちょっと触ってみた。

テスト対象のコードはこちら

sqrt.goという名前で保存

package newmath

// Sqrt returns an approximation to the square root of x.
func Sqrt(x float64) float64 {
    z := 1.0
    for i := 0; i < 1000; i++ {
        z -= (z*z - x) / (2 * z)
    }
    return z
}

まずは、gospelから

gospelのテストコードはこんな感じ。

package newmath

import (
    . "github.com/r7kamura/gospel"
    "testing"
)

func TestDescribe(t *testing.T) {
    Describe(t, "newmath", func() {
        Context("Sqrt", func() {
            It("The value should sqrt number", func() {
                Expect(Sqrt(4)).To(Equal, float64(2))
            })
            It("The value should fail assertion", func() {
                Expect(Sqrt(4)).To(Equal, 22)
            })
        })
    })
}

テストの実行結果はこんな感じ。

% ls
sqrt.go         sqrt_test.go
% go  test
.F

  newmath Sqrt The value should fail assertion
  Expected `2` to equal `22`
  /Users/uokada/go_testing/newmath/gospel/sqrt_test.go:16
    15.            It("The value should fail assertion", func() {
    16.                Expect(Sqrt(4)).To(Equal, 22)
    17.            })

--- FAIL: TestDescribe (0.00 seconds)
FAIL
exit status 1
FAIL    _/Users/uokada/go_testing/newmath/gospel    0.027s
% go  test -v
=== RUN TestDescribe
newmath
  Sqrt
    The value should sqrt number
    The value should fail assertion
    Expected `2` to equal `22`
    /Users/uokada/go_testing/newmath/gospel/sqrt_test.go:16
      15.            It("The value should fail assertion", func() {
      16.                Expect(Sqrt(4)).To(Equal, 22)
      17.            })
--- FAIL: TestDescribe (0.00 seconds)
FAIL
exit status 1
FAIL    _/Users/uokada/go_testing/newmath/gospel    0.026s

go test でvオプションを渡すことで詳細なテスト結果が見れるようになってました。

そして、Goblin

package newmath

import (
    "testing"
    . "github.com/franela/goblin"
)

func TestGoblin(t *testing.T) {
    g := Goblin(t)
    g.Describe("Goblin", func() {
        g.It("Should sqrt number ", func() {
            g.Assert(Sqrt(4)).Equal(2)
        })
        g.It("Should fail ", func() {
            g.Assert(Sqrt(4)).Equal(22)
        })
    })
}
% ls
sqrt.go         sqrt_test.go

% go test

  Goblin
    ✓ Should sqrt number
    1) Should fail


 1 tests complete (1 ms)
 1 tests failed:

  1) Goblin Should fail :

    2 does not equal 22
    /usr/local/Cellar/go/1.1.1/src/github.com/franela/goblin/assertions.go:30 (0x8ad85)
    /Users/uokada/go_testing/newmath/sqrt_test.go:16 (0x31dc0)
    /usr/local/Cellar/go/1.1.1/src/github.com/franela/goblin/goblin.go:161 (0x8b5b7)
    /usr/local/Cellar/go/1.1.1/src/github.com/franela/goblin/goblin.go:121 (0x8b377)
    /usr/local/Cellar/go/1.1.1/src/github.com/franela/goblin/goblin.go:81 (0x8b210)
    /usr/local/Cellar/go/1.1.1/src/github.com/franela/goblin/goblin.go:28 (0x8af5a)
    /Users/uokada/go_testing/newmath/sqrt_test.go:18 (0x31c30)
    /usr/local/Cellar/go/1.1.1/src/pkg/testing/testing.go:353 (0x2dd9a)
--- FAIL: TestGoblin (0.00 seconds)
FAIL
exit status 1
FAIL    _/Users/uokada/go_testing/newmath   0.031s

オプションは持ってない様子。

まとめ

GospelもGoblinが他のフレームワークに比べるとテストコード書きやすそうなイメージでしっかりと見て行きたいって感じ。 まだ Testfy も気になっているのでそっちの検証も時間のあるときにやってみたい。