You can use example below to prepare dependencies that test cases require in order to run as expected. You also have an option to destroy all dependencies after running the test cases. It is same as PHPUnit's setUp and tearDown functionality.


Setup and Teardown helper


// internal/league/testutility_test.go
package league

import (
"fmt"
"os"
"testing"
)

func TestMain(m *testing.M) {
setup()
code := m.Run()
teardown()
os.Exit(code)
}

func setup() {
// Do something here.

fmt.Printf("\033[1;36m%s\033[0m", "> Setup completed\n")
}

func teardown() {
// Do something here.

fmt.Printf("\033[1;36m%s\033[0m", "> Teardown completed")
fmt.Printf("\n")
}

Test files


// internal/league/create_test.go
package league

import (
"testing"
)

func TestCreate_One(t *testing.T) {
if true != true {
t.Error("error one")
}
}

func TestCreate_Two(t *testing.T) {
if true != true {
t.Error("error two")
}
}

// internal/league/retrieve_test.go
package league

import (
"testing"
)

func TestRetrieve_One(t *testing.T) {
if true != true {
t.Error("error one")
}
}

func TestRetrieve_Two(t *testing.T) {
if true != true {
t.Error("error two")
}
}

Tests


// Success

$ go test ./... -v

> Setup completed
=== RUN TestCreate_One
--- PASS: TestCreate_One (0.00s)
=== RUN TestCreate_Two
--- PASS: TestCreate_Two (0.00s)
=== RUN TestRetrieve_One
--- PASS: TestRetrieve_One (0.00s)
=== RUN TestRetrieve_Two
--- PASS: TestRetrieve_Two (0.00s)
PASS
> Teardown completed
ok internal/league (cached)

// Failure

$ go test ./... -v

> Setup completed
=== RUN TestCreate_One
--- PASS: TestCreate_One (0.00s)
=== RUN TestCreate_Two
--- PASS: TestCreate_Two (0.00s)
=== RUN TestRetrieve_One
--- FAIL: TestRetrieve_One (0.00s)
retrieve_test.go:9: error one
=== RUN TestRetrieve_Two
--- PASS: TestRetrieve_Two (0.00s)
FAIL
> Teardown completed
FAIL internal/league 0.011s