porfirion
porfirion

Reputation: 1699

Can't run go tests from GoLand (Intellij Idea): compilation failed

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

Upvotes: 9

Views: 7782

Answers (3)

aprilpn
aprilpn

Reputation: 158

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.

Upvotes: 14

xamgore
xamgore

Reputation: 1872

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.

Upvotes: 3

porfirion
porfirion

Reputation: 1699

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.)

Upvotes: 10

Related Questions