Avatar
0
monkey Enlightened
monkey Enlightened
Go unit test trên intellij báo undefined
mình có lớp thế này:

package math

func Sum(x int, y int) int {
	return x + y
}

và lớp unit test thế này:

package math

import "testing"

func TestSum(t *testing.T) {
	total := Sum(5, 5)
	if total != 10 {
		t.Errorf("Sum was incorrect, got: %d, want: %d.", total, 10)
	}
}

Khi chạy bằng command line thì ok:

go test math_test.go math.go 
ok      command-line-arguments  0.301s

Tuy nhiên khi dùng intellij thì bị lỗi

# command-line-arguments [command-line-arguments.test]
./math_test.go:6:11: undefined: Sum

Liệu có cần cấu hình gì ở đâu không nhỉ?

  • Answer
go
Remain: 5
1 Answer
Avatar
ducnt114 Enlightened
ducnt114 Enlightened
Bạn xem config Go root cho project trên Goland chưa.

Chạy thử test bằng command line ở thư mục root của repo: go test -v ./...

  • 0
  • Reply
chạy test nó ra: /Users/xxx/Documents/projects/go/hello-go/math/, còn go root trên intellj thi đã config rồi  –  monkey 1630893221000