files ending with _test.go
functions starting with Test[A-Z]
func(t *testing.T)
signature
import "testing"
func TestSomething(t *testing.T) {
// test something
// this will succeed
}
import "testing"
func TestSomething(t *testing.T) {
t.Run("name", func(t *testing.T) {
})
t.Run("name2", func(t *testing.T) {
})
}
t.Log(f)
t.Error(f)
t.Fatal(f)
t.Skip(f)
import "testing"
func TestSomething(t *testing.T) {
t.Parallel()
}
# Run tests
go test
# Run tests for all packages
go test ./...
# Run tests in verbose mode to see every output
go test -v
# Run a specific test
go test -run ^TestIntegration$
if want, have := SOME_VALUE, result; want != have {
t.Errorf("value does not match the expected one\n"+
"actual: %s\n"+
"expected: %s",
have, want,
)
}
_test
package// mypkg_test is allowed to be in the same directory as mypkg
package mypkg_test
import (
"testing"
"mypkg"
)
func TestSomething(t *testing.T) {
// this is fine
mypkg.New()
// this will fail
mypkg.new()
}
func TestSomething(t *testing.T) {
tests := struct{
name string
input int
output int
}{
{
name: "example name",
input: 1,
output: 2,
},
}
// ...
}
func TestSomething(t *testing.T) {
// ...
for _, test := range tests {
test := test
t.Run(test.name, func(t *testing.T) {
t.Parallel()
// do the test
})
}
}
// +build integration
package mypkg
// integration tests
go test -tags integration
func TestIntegration(t *testing.T) {
if m := flag.Lookup("test.run").Value.String(); m == "" ||
!regexp.MustCompile(m).MatchString(t.Name()
) {
t.Skip("skipping as execution was not requested"+
"explicitly using go test -run")
}
t.Parallel()
t.Run("SomeIntegrationTest", testSomeIntegrationTest)
}
github.com/stretchr/testify
assert
assert.Equal(t, expected, actual)
uses t.Error
under the hood
require
require.NoError(t, err)
uses t.Fatal
under the hood
mock
import (
"github.com/stretchr/testify/mock"
)
type MockSomething struct {
mock.Mock
}
// implement Something interface
func (m *MockSomething) DoSomething(number int) (bool, error) {
args := m.Called(number)
return args.Bool(0), args.Error(1)
}
mock
func TestSomething(t *testing.T) {
something := new(MockSomething)
something.On("DoSomething", 1).Return(true, nil)
// use something
something.AssertExpectations(t)
}
github.com/vektra/mockery
mockery -name Something -inpkg -testonly
Calculate
function accepts a number x
and calculates a magic value.
Calculate
returns x+2
Hint: write a single test method with manual assertion
Calculate
returns:
x+1
if x
is oddx+2
otherwiseHint: write subtests with testify assertions
Calculate
returns:
0
if x
is zerox+1
if x
is oddx+2
otherwiseHint: write table-driven tests
Implement a Car Rental service that finds a car matching certain criteria:
(car should match at least brand and model, color is optional)
Return information to the caller:
Implement the car rental service
Defer making decisions on finding cars
Hint: generate mocks
Implement a Car repository
Write tests for business logic (find a matching car)
Hint: use table-driven tests
Rewrite tests for the car rental service using real implementations.
Add generated ID to the returned Rent.
Hint: mocks first then real implementation
Write integration tests for remote ID generator
Hint: use any of the integration test strategies