Testing tips, golang edition

Documentation

Files

Test file should be place in the package directory and should be name using the following convention :

  • repo : github.com/go/pkg
  • package : github.com/go/pkg/example
  • package file : example/pkg.go
  • test file : exemple/pkg_test.go

Run

go test github.com/go/pkg/package

fmt.Println is not working

Gniagniagnia, use

t.Log()

or

t.Logf()

also,

go test github.com/go/pkg/package -v

How to fail

Mark test as failed (next tests executed)

t.Fail()

Mark test as failed AND exit

t.FailNow()
t.Error()

or

t.Errorf()
t.Fatal()

or

t.Fatalf()

I don't want my tests to be messy (kudos @athoune)

Assert

Shit, i want to disable my oauth test on my CI

if os.Getenv("DRONE") == "true" {
	t.Skip("Skipping test in CI environment")
}

Short mode

This test is too long ? Skip it !

if testing.Short() {
    t.Skip("Skipping test in short mode")
}
go test github.com/go/pkg/package --short

Cleanup test cache

go clean -testcache