[Go] 유닛 테스트
Updated:
개요
- go test 명령어를 이용
- 파일 이름은
xxx_test.go
, 함수 이름은Testxxx
형태여야 함 - 플래그
-v
- 결과 상세 출력
예제
- 코드
- 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 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}, {value: 2, result: 2}} for _, d := range datas { result, err := job(d.value) if d.result != result || err != nil { t.Error(err) } } }
- main.go
- 실행 결과
$ go test -v === RUN TestJob main_test.go:16: invalid value : 2 --- FAIL: TestJob (0.00s) FAIL exit status 1 FAIL test 0.005s