6

I've made simple application in go and trying to run tests with GUI tools of GoLand.

In myfile_test.go file near test func I press green button that should start test. But I get error message: "Compilation failed" and message in console:

# command-line-arguments [command-line-arguments.test]
./myfile_test.go:21:11: undefined: MyStruct
./myfile_test.go:22:12: undefined: MyFuncName
./myfile_test.go:33:12: undefined: AnotherStruct

Compilation finished with exit code 2

Other variants (Run test with Coverage/CPU Profile) don't work either. GoLand 2020.1 EAP. The same problem occurred in older versions of GoLand.

But test from console starts normally:

go test -v

=== RUN   TestMyStruct_MyMethod
--- PASS: TestMyStruct_MyMethod (0.00s)
PASS
ok      _/home/username/projects/my_project_name     0.002s
porfirion
  • 1,619
  • 1
  • 16
  • 23

3 Answers3

13

The answer from @porfirion worked for me.

Basically, you need to tick "Enable Go Modules integration" in GoLand under "Preferences" -> "Go" -> "Go Modules"

Goland will then reindex your project and this can take a long time if you have a large project with many modules. Mine took 30 mins. You can check the reindexing progress status at the bottom of the IDE.

The test run will work after the reindexing.

E_net4
  • 27,810
  • 13
  • 101
  • 139
aprilpn
  • 149
  • 1
  • 7
  • 3
    You also need to run `go mod init` to create a go.mod file as in @porfirion's answer. I also found that it did not work until I closed the project and reopened. – Andrew W. Phillips Sep 25 '20 at 08:01
8

I solved the problem with initializing new go module and enabling Go Modules Integration:

1) Run in Terminal go mod init my_module_name

2) Click "File -> Settings" or press Ctrl+Alt+S

3) Check "Enable Go Modules Integration" and Apply button project settings window

Now all test functionality in GoLand works well (including tests with coverage, etc.)

porfirion
  • 1,619
  • 1
  • 16
  • 23
  • In my casee I moved my project from under the *go* directory (Linux) to elsewhere because I was having permission issues there. Then I could no longer Run or Debug the project. – TheRealChx101 Feb 09 '22 at 23:26
2

The issue here is that you're pointing to a single file. All other files, even though they're in the package, aren't going to be imported. go build has the same behaviour, if you are targeting a single file to build and forget to include other files it uses in the command.

Change the test kind to "Directory" and files to "/project-root-dir". This will then include all files in the directory during build and should look for all _test.go files to execute tests.

xamgore
  • 1,723
  • 1
  • 15
  • 30
  • It works! Unfortunately every time I click on single test - IDEA creates new configuration and it is always has kind "File". Even if I change kind to "Directory" manually and click on method again - IDEA creates one more configuration with kind "File". Very disappointing( – porfirion Mar 12 '20 at 17:29
  • I found this answer did not work. You can't control the "Test kind" option when you try to run/debug a specific test using the green arrow button. If you change the "test kind" setting in the "Run/Debug Configurations" then GoLand creates a new config with "test kind" = file when you debug a single specific test. I found the only way is to (1) place your project in GOPATH or (2) use modules (as explained in the other answers) – Andrew W. Phillips Sep 25 '20 at 08:09