LoginSignup
4
4

More than 5 years have passed since last update.

goconveyのSetUp, TearDownの実行順序

Last updated at Posted at 2015-12-09

Go言語のテストツールGoConveyのSetUp, TearDownを行いたいときに記述箇所や実行順序が分かりづらかったので試してみました。

convey_test.go
package practice

import (
    . "github.com/smartystreets/goconvey/convey"
    "testing"
    "fmt"
)

func TestConvey(t *testing.T) {
    fmt.Println("1")
    Convey("Top level convey", t, func() {
        Println("2")
        defer Println("deferred func 1")
        Convey("first test convey", func() {
            Println("3")
            So(1, ShouldEqual, 1)
        })

        Println("betweeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeen")
        defer Println("deferred func 2")

        Convey("second test convey", func() {
            Println("4")
            So(2, ShouldEqual, 2)
        })

        Println("end of top level convey")

        Reset(func () {
            Println("reset")
        })
    })
}

上記のテストコードがあるディレクトリで下記コマンドを実行

goconvey

プリントされる順番は以下のようになる

1
2
3
betweeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeen
end of top level convey
deferred func 2
deferred func 1
reset
2
betweeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeen
4
end of top level convey
deferred func 2
deferred func 1
reset
4
4
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4