How to run a single unit test in Goland?
I have a very small project (trying to solve this year's advent of code in Go), and I would like to run the unit tests from the IDE.
Running the unit test in the directory itself (just `go test`) works fine. But running a single test with GoLand gives me this error:
/usr/lib/go-1.14/bin/go test -c -o /tmp/___TestDay1Main_in_day1_test_go /home/paul/src/go/adventOfCode/day1/day1_test.go #gosetup
# command-line-arguments [command-line-arguments.test]
./day1_test.go:6:2: undefined: day1main
...
I found two similar topics:
* https://intellij-support.jetbrains.com/hc/en-us/community/posts/360009391699-Why-can-t-test-go-files-find-functions-defined-in-different-files-within-the-same-package- (this does not solve anything, this does not apply to single tests, and I have the same package name)
* https://stackoverflow.com/questions/60281645/cant-run-go-tests-from-goland-intellij-idea-compilation-failed (If I enable "Go modules integration" I am not able to import "../common" in my project any more)
I even tried to add the main file in the test configuration under "Files", too. But I did not find out how to add more than one file there, despite that it says "Files".
I have the code here: https://github.com/paulwellnerbou/advent-of-code-2020/tree/main/day1#
Is it somehow possible to run single tests with GoLand? If yes, what do I have to do to do this?
请先登录再写评论。
Hello,
First of all, I would like to point out that specifying the relative import path is not a good idea since it depends on each user's GOPATH variable set ("Importing packages from your module").
So, let's start with a simple example. Take as an example that GOPATH is set to /Users/daniil.maslov/go.
Project structure:
You can see that I'm specifying the import path relative to the GOPATH variable and the current project. If you organize your code this way, then there should be no problem running unit tests.
Another way is to use Go Modules in the project. Execute go mod init command in a terminal and enable integration (Preferences / Settings | Go | Go Modules). Also, disable GOPATH indexing (Preferences / Settings | Go | GOPATH | Index entire GOPATH).
Project structure:
The principle is almost the same, but this approach has the main advantage: you can place the project anywhere in your system and not depend on GOPATH variable. Nowadays, it's a more preferred way.
I hope it helps.
Yes, this helps, thank you.
At first I tried the first approach, just to learn it, without any success. I used the module version then, which works perfect for me.
Thanks, Paul
I tried various ways, command line builds was successful but GoLand failed, and "Enable Go modules integration" option solved it. Cheers!