doc.dev1x.org

Golangテスト周りに関する覚書

テストコード

package example

func SomethingFunc(s string) string {
    return s
}
package example

import (
    "testing"
)

func TestHello(t *testing.T) {
    message := "ieeeeeee"
    result := Echo(message)
    expext := message
    if result != expext {
        t.Error("\n実際: ", result, "\n理想: ", expext)
    }
}

ヘルパー関数

func helperFunction(t *testing.T, s string) string {
    t.Helper()  //
    u, err := url.Parse(s)
    if err != nil {
        t.Fatal(err)
    }
    return u
}
 type T
    func (c *T) Cleanup(f func())
    func (t *T) Deadline() (deadline time.Time, ok bool)
    func (c *T) Error(args ...interface{})
    func (c *T) Errorf(format string, args ...interface{})
    func (c *T) Fail()
    func (c *T) FailNow()
    func (c *T) Failed() bool
    func (c *T) Fatal(args ...interface{})
    func (c *T) Fatalf(format string, args ...interface{})
    func (c *T) Helper()
    func (c *T) Log(args ...interface{})
    func (c *T) Logf(format string, args ...interface{})
    func (c *T) Name() string
    func (t *T) Parallel()
    func (t *T) Run(name string, f func(t *T)) bool
    func (t *T) Setenv(key, value string)
    func (c *T) Skip(args ...interface{})
    func (c *T) SkipNow()
    func (c *T) Skipf(format string, args ...interface{})
    func (c *T) Skipped() bool
    func (c *T) TempDir() string

T.Cleanup

func TestHello(t *testing.T) {
    message := "ieeeeeee"
    result := Echo(message)
    expext := message
    if result != expext {
        t.Error("\n実際: ", result, "\n理想: ", expext)
    }
    t.Cleanup(func() {
        t.Log("Cleanup")
    })
}

T.Deadline

func TestHello(t *testing.T) {
    message := "ieeeeeee"
    result := Echo(message)
    expext := message
    if result != expext {
        t.Error("\n実際: ", result, "\n理想: ", expext)
    }
    t.Cleanup(func() {
        t.Log("Cleanup")
    })
}

T.Error

参考資料