I am doing some testing. I have a file dao.go:
package model_dao
import "io/ioutil"
const fileExtension = ".txt"
type Page struct {
Title string
Body []byte
}
func (p Page) SaveAsFile() (e error) {
p.Title = p.Title + fileExtension
return ioutil.WriteFile(p.Title, p.Body, 0600)
}
func LoadFromFile(title string) (*Page, error) {
fileName := title + fileExtension
body, err := ioutil.ReadFile(fileName)
if err != nil {
return nil, err
}
return &Page{title, body}, nil
}
And a test file dao_test.go:
package model_dao_test
import (
"shopserver/model/dao"
"testing"
)
func TestDAOFileWorks(t *testing.T) {
TITLE := "test"
BODY := []byte("Hello, World!!")
p := &model_dao.Page{TITLE, BODY}
p.SaveAsFile()
p, _ = model_dao.LoadFromFile(TITLE)
result := p.Body
if string(BODY) != string(result) {
t.Error("Body", BODY, "saved.
", "Load:", result)
}
}
Here I test all 2 methods from Page, but after testing I see a message:
Why do I only get 85.7%? Where he get this numbers and how to get 100%?
See "The Go Blog - The cover story"
go test -coverprofile=coverage.out
go tool cover -html=coverage.out
That would display an HTML representation of your source file, where you will clearly see which lines are or are not covered by your test.
Other Go testing framework would show you the same visualization as well.
Seee for instance GoConvey.