Go Testing with Testify
This documentation explains how to write tests using Testify in Go.
Installation
To install Testify, use the following command:
go get github.com/stretchr/testify
Structure
The directory structure for organizing your tests:
project-root/
├── internal/
│ ├── area/
│ │ ├── service.go
│ │ └── model.go
├── test/
│ ├── area/
│ │ ├── service_test.go
│ │ ├── mock_service.go
│ │ └── testdata/
│ │ └── example.json
│ ├── action_consumer/
│ │ ├── consumer_test.go
│ │ ├── mock_rabbitmq.go
│ │ └── testdata/
│ │ └── example.json
├── cmd/
│ └── main.go
├── go.mod
├── go.sum
Example: Writing a Unit Test
Here is an example of a unit test using Testify:
package area_test
import (
"testing"
"AREA/internal/area"
"github.com/stretchr/testify/assert"
)
func TestAdd(t *testing.T) {
result := area.Add(2, 3)
assert.Equal(t, 5, result, "they should be equal")
}
Mocking with Testify
To mock dependencies like RabbitMQ, use Testify’s mock package:
package consumer
import "github.com/stretchr/testify/mock"
// MockRabbitMQ mocks RabbitMQ interactions
type MockRabbitMQ struct {
mock.Mock
}
func (m *MockRabbitMQ) Publish(message string) error {
args := m.Called(message)
return args.Error(0)
}
Running Tests with Coverage
To measure test coverage, run:
go test ./... -cover
To generate a detailed coverage report:
go test ./... -coverprofile=coverage.out
go tool cover -func=coverage.out
To generate an HTML report:
go tool cover -html=coverage.out -o coverage.html
Automating Testing with Makefile
You can use makefile command to run already implementing test commands:
To run all tests and logs them without coverage:
make test_server
To run all tests and get result preview in a html file:
make coverage_server
Best Practices
Organize your tests in a /test directory mirroring your code structure.
Use testify/mock to mock dependencies.
Keep your tests focused on specific functionalities.
Use testdata directories for static test resources.