Hi Everyone!
In this post we'll to check about coverage in Golang.
Alternatively, you can download the Source code and follow the instructions in the README.md.
TL;DR
# Run the tests and save the coverage profile in "coverage.out"
go test -coverprofile=coverage.out ./...
# View the coverage profile in your browser
go tool cover -html=coverage.out
Coverage in Golang
For this example, we have to create an user
entity and store it in memory. This is the structure of the project.
.
├── README.md
├── cmd
│ └── main.go
├── go.mod
├── go.sum
└── internal
└── user
├── storage.go
├── storage_test.go
└── user.go
we need to write the program
First of all, we need to declare the user
entity, the filename for this is 'user.go' (in the "internal" folder in called user
).
// Package user describe user entity attributes and manage user storage
package user
type User struct {
ID uint64
FirstName string
LastName string
Age uint16
}
And now, we have to create the user storage. In the same package, the storage.go
was created with the following structure and methods.
package user
type UserStorage struct {
DB []User
}
// AddUser Add user information to the UserStorage database
func (us *UserStorage) AddUser(user User) {
us.DB = append(us.DB, user)
}
// FindUserByID find the user by ID
func (us *UserStorage) FindUserByID(id uint64) User {
for _, user := range us.DB {
if user.ID == id {
return user
}
}
return User{}
}
// Count total users in the database
func (us *UserStorage) Count() int {
return len(us.DB)
}
We need to test the program
For now, we have three methods of adding, searching, and counting your own users in storage. But we have to test that, so we add the storage_test.go
file with its test. The following code has three tests that make 100% coverage for UserStorage.
package user_test
import (
"testing"
user "github.com/fransafu/coverage-golang-example/internal/user"
"github.com/stretchr/testify/assert"
)
func TestUserStorage_SaveUser(t *testing.T) {
var userStorage user.UserStorage
user1 := user.User{}
user1.ID = 1
user1.FirstName = "Francisco"
user1.LastName = "Sanchez"
user1.Age = 99
userStorage.AddUser(user1)
assert.Equal(t, 1, userStorage.Count())
}
func TestUserStorage_SearchUser(t *testing.T) {
var userStorage user.UserStorage
user1 := user.User{}
user1.ID = 1
user1.FirstName = "Francisco"
user1.LastName = "Sanchez"
user1.Age = 99
userStorage.AddUser(user1)
assert.Equal(t, user1, userStorage.FindUserByID(user1.ID))
}
func TestUserStorage_EmptySearchUser(t *testing.T) {
var userStorage user.UserStorage
assert.Equal(t, 0, userStorage.Count())
assert.Equal(t, user.User{}, userStorage.FindUserByID(1))
}
If you thinking about improving the tests with "Test Suite" or "Test Group", yes, this is the way, but for now, these examples are simple and atomic for this post.
Get the coverage
To get the coverage in a Golang project you can use the "./..." wildcard and the Golang CLI will search for all tests declared in a project.
# Run the tests and save the coverage profile in "coverage.out"
go test -coverprofile=coverage.out ./...
Ok, we have the file coverage.out
that contains the coverage information but we need to view the results, the following command line shows the coverage program in the browser.
# View the coverage profile in your browser
go tool cover -html=coverage.out
Now you can check all files that contain declared tests. If you don't declare a test file, you won't have coverage for that module.
It's all for now, thanks for reading this post.
:)!
Top comments (0)