Golang show that I have only 50% of covering code, and as I see code in main is not covered, I've tried to search but not found anything with explanations how to cover code in main.
main.go
package main
func Sum(x int, y int) int {
return x + y
}
func main() {
Sum(5, 5)
}
main_test.go
package main
import (
"testing"
)
func TestSum(t *testing.T) {
total := Sum(5, 5)
if total != 10 {
t.Fail()
}
}
Test files usually are directly next to the code they test. Depending on the size of your project you don't have to extract your sum function into another package just to test it and it also doesn't have to be public:
main.go
package main
func main() {
sum()
}
func sum() int {
return 5 + 5
}
main_test.go:
package main
import (
"testing"
)
func TestSum(t *testing.T) {
total := sum()
if total != 10 {
t.Fail()
}
}