2
votes

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
  1. 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
2
Everything you need to know to start is in How to Write Go Code. If you want help with this question, you first need to create a minimal reproducible example. - JimB
Without seeing the code in question no one will be able to offer much help. - Adrian
hello_test.go isn'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) - JimB
The reason your IDE is failing to run it is that it's calling go test specifically 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 call hello1() with -3 and 0, both of which should return no value, but you're asserting that they will return Hello, world, which is only the case if you pass a positive integer to hello1), but your IDE version doesn't. Also, as pointed out, your project is not in your GOPATH. - Kaedys
@Kaedys, thank you for your answer, would you mind to tell me how you tell the project is not in GOPATH? - Matt Hin

2 Answers

2
votes

For writing go it is really important to have the correct folder structure, so that you won't have headaches later on. You need your gopath like this:

bin/
    hello                          # command executable
    outyet                         # command executable
pkg/
    linux_amd64/
        github.com/golang/example/
           stringutil.a           # package object
src/
    github.com/golang/example/
        .git/                      # Git repository metadata
        hello/
            hello.go               # command source
        outyet/
            main.go                # command source
            main_test.go           # test source
        stringutil/
            reverse.go             # package source
            reverse_test.go        # test source
        golang.org/x/image/
           .git/                      # Git repository metadata
            bmp/
                reader.go              # package source
                writer.go              # package source
   ... (many more repositories and packages omitted) ...

( taken from https://golang.org/doc/code.html#Workspaces )

The gopath is by default "$HOME/go" or the equivalents on other Operating Systems ( home_directory/go )

So you would end up with your project under something like: $HOME/go/src/github.com/matthin/awesomeproject

And put all your files into it.

Then to run the test you could execute go test github.com/matthin/awesomeproject or you can run the test by specifying each file in that directory, like in your case:

go test main.go main_test.go

Then the go test runner knows that it needs to evaluate both files and the function will be found.

1
votes

try go test *.go -v ,it is ok for me .

czyt@MiniManjaro  ~/go/src/TestProject/casestatement  go test *.go -v                                                                                            ✔  2080  15:03:17
=== RUN   TestCaseStatement
    TestCaseStatement: casestatement_test.go:9: 11
    TestCaseStatement: casestatement_test.go:11: ===================
    TestCaseStatement: casestatement_test.go:12:  num 1 2
    TestCaseStatement: casestatement_test.go:14: ===================
    TestCaseStatement: casestatement_test.go:15:  num 1 2
--- PASS: TestCaseStatement (0.00s)
PASS
ok      command-line-arguments  0.002s