[Go] 코드 커버리지
Updated:
개요
- 프로파일 생성
go test -cover -coverprofile=coverage.out .
- html 변환
go tool cover -html=./coverage.out -o ./coverage.html
예제
- 코드
- main.go
package main import ( "errors" "fmt" ) func job(value int) (int, error) { switch value { case 0: return 0, nil case 1: return 1, nil } return -1, errors.New(fmt.Sprint("invalid value : ", value)) } func test() { println("test cll") } func main() { }
- main_test.go
package main import "testing" func TestJob(t *testing.T) { type data struct { value int result int } datas := []data{{value: 0, result: 0}, {value: 1, result: 1}} for _, d := range datas { result, err := job(d.value) if d.result != result || err != nil { t.Error(err) } } }
- main.go
- 실행 결과
- 프로파일 생성
ok test 0.003s coverage: 60.0% of statements
- html
- 프로파일 생성