Reputation: 1299
I have a main.go
and main_test.go
, and go test
works fine:
$ cat main.go
package main
func main() {}
func F() int { return 1 }
$ cat main_test.go
package main
import "testing"
func TestF(t *testing.T) {
if F() != 1 {
t.Fatalf("error")
}
}
From link1 and link2, I could use package main_test
in main_test.go
. But go test
shows ./main_test.go:6:5: undefined: F
. How to fix the error?
Upvotes: 0
Views: 1344
Reputation: 120931
Import the main package. Use the following code assuming that the package path is "example.com/example".
$ cat main_test.go
package main
import (
"testing"
"example.com/example" // import main
)
func TestF(t *testing.T) {
if main.F() != 1 { // use package selector when referring to F
t.Fatalf("error")
}
}
Replace example.com/example
with the actual path of your package.
Upvotes: 1