LoginSignup
4
4

More than 5 years have passed since last update.

`go test -count n -timeout t`

Last updated at Posted at 2016-02-24

go testの引数についてのメモ

  • -count n: テストをn(int)回実行する
  • -timeout t: 1回毎ではなく,n回全体についてのタイムアウトをt(time.Duration)に設定する.

TestFooBar 1回分が1秒以内に終わって欲しい場合,

  • :o: go test -count 1 -timeout 1s -v TestFooBar
  • :o: go test -count 100 -timeout 100s -v TestFooBar
  • :x: go test -count 100 -timeout 1s -v TestFooBar

となる.

package main

import (
        "testing"
        "time"
)

func TestFooBar(t *testing.T) {
        t.Logf("enter %s", time.Now())
        epsilon := 100 * time.Millisecond
        <-time.After(1 * time.Second - epsilon)
        t.Logf("leave %s", time.Now())
}
$ go test -v -count 1 -timeout 1s -run TestFooBar
=== RUN   TestFooBar
--- PASS: TestFooBar (0.90s)
        main_test.go:9: enter 2016-02-24 12:06:40.528802812 +0900 JST
        main_test.go:12: leave 2016-02-24 12:06:41.429077271 +0900 JST
PASS
ok      _/home/suda/WORK/testfoobar     0.902s
$ go test -v -count 100 -timeout 1s -run TestFooBar
=== RUN   TestFooBar
--- PASS: TestFooBar (0.90s)
        main_test.go:9: enter 2016-02-24 12:07:14.381237659 +0900 JST
        main_test.go:12: leave 2016-02-24 12:07:15.281427292 +0900 JST
=== RUN   TestFooBar
panic: test timed out after 1s

goroutine 8 [running]:
panic(0x51b300, 0xc8200107b0)
        /home/suda/go/src/runtime/panic.go:464 +0x3e6
testing.startAlarm.func1()
        /home/suda/go/src/testing/testing.go:725 +0x14b
created by time.goFunc
        /home/suda/go/src/time/sleep.go:129 +0x3a

goroutine 1 [chan receive]:
testing.RunTests(0x5c2380, 0x645df0, 0x1, 0x1, 0x1)
        /home/suda/go/src/testing/testing.go:583 +0x8d2
testing.(*M).Run(0xc820041ef8, 0xc820010588)
        /home/suda/go/src/testing/testing.go:515 +0x81
main.main()
        _/home/suda/WORK/testfoobar/_test/_testmain.go:54 +0x117

goroutine 7 [chan receive]:
_/home/suda/WORK/testfoobar.TestFooBar(0xc8200aa090)
        /home/suda/WORK/testfoobar/main_test.go:11 +0x153
testing.tRunner(0xc8200aa090, 0x645df0)
        /home/suda/go/src/testing/testing.go:473 +0x98
created by testing.RunTests
        /home/suda/go/src/testing/testing.go:582 +0x892
exit status 2
FAIL    _/home/suda/WORK/testfoobar     1.004s
4
4
0

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