Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 626
porfirion Avatar asked Feb 18 '20 13:02

porfirion


People also ask

Does IntelliJ include GoLand?

Is GoLand available via the JetBrains Toolbox as part of the All Products Pack?  Yes, it is available both as a standalone IDE and as a part of the All Products Pack.

How do I open the Golang project in IntelliJ?

Open an existing projectIn the Welcome to IntelliJ IDEA dialog, click Open. Alternatively, click File | Open. In the file browser, navigate to a folder with project files and click Open…. Click OK.

How do I run a test file in Golang?

At the command line in the greetings directory, run the go test command to execute the test. The go test command executes test functions (whose names begin with Test ) in test files (whose names end with _test.go). You can add the -v flag to get verbose output that lists all of the tests and their results.


2 Answers

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.

like image 95
aprilpn Avatar answered Oct 13 '22 07:10

aprilpn


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

like image 20
porfirion Avatar answered Oct 13 '22 05:10

porfirion