Debug Test Methods

Completed

GoLand 2021.1.3, Go 1.16.5 on Linux

I have a project originally built a few Go versions and several GoLand versions ago that I am now unable to launch the debugger on methods in my *_test.go files. I can successfully 'Run "Test..."' from the context menu but 'Debug "Test..."' gives me "No tests were found" in the Debug Console.

I created a new GoLand project using the Go modules template and added a simple method then used the Generate function to create a _test file and and a Test... method. I can successfully Run and Debug this Test... method.

Comparing the Run/Debug configurations between the two projects doesn't show any obvious differences. Both are set to use the Test kind of 'Package'. Package path, Patterns, Working directory and Module are pointing to the correct items/paths for the project.

What else should I be looking at?

0
10 comments

Hello Eric,

Please see the following code snippet:

package main

import (
    "os"
    "testing"
)

func TestMain(m *testing.M) {
    os.Exit(0)
}

func TestSomething(t *testing.T) {
}

Pay attention to TestMain() and os.Exit(0) functions. It's an issue with interaction between TestMain() and -json flag (test2json), please see https://github.com/golang/go/issues/31969

The TestMain() needs an m.Run() call.

Are you using TestMain() / init() function? Could you please try to isolate a code snippet from your main codebase to reproduce the issue? Also, an output of go test -json might help.

0

That particular project doesn't have a TestMain() or an init() function as there is no setup to do for the tests.

I forgot to mention that go test in a terminal window works just as well as Run "Test..." from the context menu in the code window.

The output of go test -json has mostly the same data as the Run console when I do the Run "Test..." from the context menu.

It appears that actual testing isn't a problem. I just can't debug.

0

Another behavior that may be relevant.

My machine is dual boot: a Windows 10 disk and an Arch Linux disk. I primarily use Linux as my daily driver but I use the Windows machine to test and compile those Go apps that are to be or can be used in either OS. I have a common data drive where I keep my git repos. Running GoLand in Windows on the same project, debugging a test method works as expected. It seems like it's only a problem in Linux.

Every time I open a project in GoLand after opening it in the other OS, I get prompted to set my GOROOT because the path to the Go SDK is obviously different on the two machines. Maybe something to do with that though builds have never had any issues.

0

Thanks for the update.

Unfortunately, it's quite hard to point the problem without deep investigating.

Could you please provide a code snippet? If you can't, please, reproduce the issue with additional logging:

  • Add #com.goide.dlv.DlvVm in Help | Diagnostic Tools | Debug Log Settings.
  • Restart GoLand.
  • Reproduce the issue with the debugger again to collect the necessary information about your environment.
  • Collect IDE logs via Help | Collect Logs and Diagnostic Data.
  • Upload IDE logs to https://uploads.jetbrains.com/ and provide its ID.
0

Not sure what good a code snippet will do. Debugging the same test methods in GoLand on Windows works fine so I can't see how it could be a code problem. But here's what I've got:

Built a new project using GoLand's Go modules template:

// main.go
package
main

import "fmt"

func main() {
showHello()
}

func showHello() {
// breakpoint set on the following line
fmt.Println("Hello world!")
}
// main_test.go
package
main

import "testing"

func Test_showHello(t *testing.T) {
tests := []struct {
name string
}{
{
name: "test_showHello",
},
}

for _, tt := range tests {
// breakpoint set on the following line
t.Run(tt.name, func(t *testing.T) {
})
}
}

 

In the new project selecting Debug Test_showHello() from the context menu works as expected, both breakpoints are hit.

Next copied the showHello() method to the main.go file of the problem project and copied the Test_showHello() method to the main_test.go file. In the project with the problem, selecting Debug Test_showHello(), I get "No tests were found" in the Debug Console.

I've also modified the Debug Log Settings per your instructions, restarted GoLand and tried to debug a test method again. I uploaded the IDE log and the ID is 2021_06_20_Fjc6U1u8DtZidrjo.

Thanks for your perseverance.

 

0

Oops. I provided the wrong test method above. That was one I was experimenting with. Here's the actual main_test.go that works in the test project but not in the problem project:

package main

import "testing"

func Test_showHello(t *testing.T) {
    // breakpoint set on following line
     showHello()
}
0

Could you please download the Nightly version of GoLand via Toolbox App and reproduce the issue again with the additional flag in Debug Log Settings?

0

Downloaded and installed the Nightly build. Added the Debug Log Settings flag, restarted GoLand and attempted the test method debug. I received an IDE crash notification and I clicked the submit to JetBrains button after logging in. Also I uploaded the idea.log file under ID 2021_06_21_AKiXrGgtXTE5b6uH.

After I closed the IDE crash notification dialog, the debug session did hit the breakpoints so that's progress.

0

Thanks, we'll take a look at your stacktraces and it seems a bit different issue.

After I closed the IDE crash notification dialog, the debug session did hit the breakpoints so that's progress.

Do I understand correctly that it works well in the Nightly version? Recently, we have several issues with the bundled Delve version and it should be solved in the latest version.

0

Correct. I can successfully debug test methods with the nightly build of GoLand.

1

Please sign in to leave a comment.