I am pretty new to go, and trying to write up a test file for go. When I run the test, I have two questions: 1. I have to run "go test -cover , then I can see this:
#command-line-arguments
./client_test.go:59: undefined: Init
FAIL command-line-arguments [build failed]
My question is why I can't just run: go test (If I do this, I see this:
exit status 1
FAIL command-line-arguments 0.008s
- My second question is there are two files in the package(one package in a big project), a file1, and a test.go file for that file1. Why I can't access the func in file1 in test.go (undefined). I am sure the test file is in correct format and the function is exported.
I have been checking online for solutions for days. Any thoughts would be helpful. I am sorry that I can't show the code and I really appreciate your help.
Thank you
Edit2: Thanks, guys, I am thinking about the env variables, (I am setting them up). I really appreciate your comments.
Edit1: Thanks for your reply, and I did try a small piece of code to see if the go test works ok: (I am sorry that I can't the project I am working on) hello.go:
package main
func hello1(i int) string {
if i > 0 {
return "Hello, world"
}
return "no value"
}
hello_test.go:
package main
import (
"testing"
"fmt"
)
func TestHello(t *testing.T) {
var s string
var s2 string
var s3 string
s = hello1(3)
if s != "Hello, world" {
t.Error("Expected: Hello, world, got", s)
}
s2 = hello1(-3)
if s2 != "Hello, world" {
t.Error("Expected: Hello, world, got", s2)
}
s3 = hello1(0)
if s3 != "Hello, world" {
t.Error("Expected: Hello, world, got", s3)
}
}
When I use the IDE to run these files, I see:
GOROOT=/usr/local/Cellar/go/1.9.3/libexec #gosetup
GOPATH=/Users/<here is my user name>/go #gosetup
/usr/local/Cellar/go/1.9.3/libexec/bin/go test -c -i -o /private/var/folders/cx/0mvyxrxj5755jc68mqvtywth0000gn/T/___TestHello_in_hello_test_go /Users/minghan/awesomeProject/hello_test.go #gosetup
# command-line-arguments
./hello_test.go:13:6: undefined: hello1
./hello_test.go:17:7: undefined: hello1
./hello_test.go:21:7: undefined: hello1
Compilation finished with exit code 2
When I run in command line: go test -cover:
--- FAIL: TestHello (0.00s)
hello_test.go:19: Expected: Hello, world, got no value
hello_test.go:23: Expected: Hello, world, got no value
FAIL
coverage: 100.0% of statements
exit status 1
FAIL _/Users/minghan/awesomeProject 0.007s
hello_test.goisn't even in your GOPATH, so it can't be in the same package as hello.go (which you haven't shown the location of) - JimBgo testspecifically and only on your test file itself. It needs to call it on all of the files in that package. That's why your command line version works (though it reports a failure because you have inaccurate assertions. You callhello1()with -3 and 0, both of which should returnno value, but you're asserting that they will returnHello, world, which is only the case if you pass a positive integer tohello1), but your IDE version doesn't. Also, as pointed out, your project is not in your GOPATH. - Kaedys