Matt Hin
Matt Hin

Reputation: 51

Golang testing, same package, undefined func

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

Upvotes: 3

Views: 9996

Answers (2)

虫子樱桃
虫子樱桃

Reputation: 51

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

Upvotes: 3

Michael Ernst
Michael Ernst

Reputation: 191

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.

Upvotes: 3

Related Questions