Why can't *_test.go files find functions defined in different files within the same package?


This works fine with *.go files but not with *_test.go files. 

Goland's editor can see the function Dbconnect() because it displays the name as I'm typing it. The name also doesn't appear in red within my editor but gives me a green checkmark.  (see the image below)

I can go to the command line and do "go test -v" in the directory and this test works just fine. Here is the output:


$ ls

dbconnect.go dbconnect_test.go

$ go test -v

=== RUN   TestDbconnect

In TestDbconnect

calling dbconnect

In Dbconnect

Leaving Dbconnect

Leaving TestDbconnect

--- PASS: TestDbconnect (0.00s)


ok  github.com/KoddiDev/koddi-framework/test/dbconnect 0.233s




If I click the dbconnect directory and do a "Run" through Goland, the Dbconnect() function is undefined and the compilation fails (see picture).

I don't know how to attach my project files so I'll cut-n-paste them here.

File structure:





File: dbconnect_test.go

package dbconnect_test.go

// In regular *.go files you can call functions defined in the same package but in a different file as long
// as you capitalize the name of the function.
// In *_test.go files, Goland can find these functions (the IDE will not highlight them in red as undefined in your
// IDE editor) and you can do an autocomplete while typing their names. However, when you compile the tests you will
// get an undefined error.

import (

func TestDbconnect(t *testing.T) {
fmt.Println("In TestDbconnect")
fmt.Println("calling dbconnect")
//Dbconnect() // uncomment and this test will not compile.
fmt.Println("Leaving TestDbconnect")

File dbconnect.go 

package dbconnect

import (

func Dbconnect() {
fmt.Println("In Dbconnect ")
fmt.Println("Leaving Dbconnect ")



BTW, three days ago Daniil gave me this code: https://github.com/s0xzwasd/go-tests-examples

This was for another issue I was having.

I just looked and that code has the exact same problem. There is a function sum() that is being called from a file where it is not defined. The "undefined: sum" is there when you try to run that test in Goland but works just fine on the command line.

Command Line:

$ ls

main_test.go sum_test.go sumexample_test.go sumtable_test.go

$ go test -v

Init func run

Setup func run

TestMain run

=== RUN   TestSum

--- PASS: TestSum (0.00s)

=== RUN   TestSumTable

--- PASS: TestSumTable (0.00s)

=== RUN   ExampleSum

--- PASS: ExampleSum (0.00s)


TestMain exit 0

Teardown func run

ok  _/Users/reginamorris/workspace/go-tests-examples/tests 0.214s



Through Goland:


For Daniil's code, if I select to run the sumexample_test.go through Goland I get that compilation error.

If I select the directory tests and run through Goland it works. 


I now went back to my Dbconnect() example and did the same thing and it also works when I run through Goland at a higher level (i.e. run as a directory).

So this issue I just opened is working. I was hoping it would give me some insight as to why I can't import a package made by my own company and have it compile. I'm still having that issue that I reported a few days ago. I removed the out of sync library and still have that problem. :-(



The first problem I see here is that the names of the packages are different, but most likely, it is a typo, and the dbconnect package is used everywhere in the project (on screenshot dbconnect_test.go is in dbconnect package).

I completely tried to reproduce the situation:

  1. Create a new project.
  2. Create a folder inside the project. Name it dbconnect.
  3. Create the files you specified, select the dbconnect directory and then "Run" | go test.

As you can see, the code compiles and runs without any errors, similar to running from the terminal.

I was able to repeat the problem only if the package names don't match, for example, one is called dbconnect and the other dbconnect_test, but I get the exact same error when running from the terminal.

Looking at the screenshots, you run the configuration not by folder, but by a specific file, in this case, GoLand runs a slightly different command (note that "under the hood" the usual go test with arguments is used). For file, go test -c -o filename... is called, then go tool test2json -t. go test -json ./... is called on the directory (similar to what you do in the terminal).

The difference here is that when you run the configuration for a specific file, you need to specify two of them in your case: dbconnect.go and dbconnect_test.go, but GoLand provides this case if you create a configuration based on a directory or a specific package.

If you run go test -v dbconnect/dbconnect_test.go in the terminal (you get the same error and that's what you do when run configuration for a file), then it will not compile correctly since you need to list all the files to compile dbconnect_test.go: go test dbconnect/dbconnect_test.go dbconnect/dbconnect.go.

Command go test -v similar to go test -v ./... (you can read about the differences in the documentation, look for the line "Go test runs in two different modes".), which we use in the directory configuration. That is why you need to create a configuration and choose test kind: "Directory" (or just click on a directory, not a file).

I hope now it becomes clearer how configurations work and the difference between running a specific test file and a directory.


I can say that it's more of a feature of the go test tool and therefore, may be confusing at first (especially the difference between running go test -v and go test -v filename.go).

For another problem, I suggest communicating in the same thread so as not to lose context. You can describe in detail what the difficulty is now (or what have you already tried to do), and I'll try to help.


Please sign in to leave a comment.