36

On my Windows machine, I have Visual Studio Code installed. To run tests manually, I go in console to projects folder and enter

go test main_test.go

It works perfectly.

enter image description here

But I have a situation in which I need to debug my test to understand what's going on.

For this I open launch.json and add a configuration

  {
        "name": "Tests",
        "type": "go",
        "request": "launch",
        "mode": "test",
        "remotePath": "",
        "port": 2346,
        "host": "127.0.0.1",
        "program": "${workspaceRoot}",
        "env": {},
        "args": [
           "main_test.go"
            ],
        "showLog": true
    }

After I press F5 I have

2017/03/29 13:28:11 server.go:73: Using API v1
2017/03/29 13:28:11 debugger.go:68: launching process with args: [./debug.test main_test.go main_go]
not an executable file
Process exiting with code: 1

Any ideas why this error occurs and what executable it's looking for?

Zoyd
  • 3,449
  • 1
  • 18
  • 27
Vitalii
  • 10,091
  • 18
  • 83
  • 151

3 Answers3

57

To launch debugger for test I added one more configuration for launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Code",
            "type": "go",
            "request": "launch",
            "mode": "debug",
            "remotePath": "",
            "port": 2345,
            "host": "127.0.0.1",
            "program": "${workspaceRoot}",
            "env": {},
            "args": [],
            "showLog": true
        },
        {
            "name": "Test Current File",
            "type": "go",
            "request": "launch",
            "mode": "test",
            "remotePath": "",
            "port": 2345,
            "host": "127.0.0.1",
            "program": "${file}",
            "env": {},
            "args": [],
            "showLog": true
        }       
    ]
}

Also this configuration does not support tags. All tags in test files have to be disabled

// +build unit
...
Community
  • 1
  • 1
Vitalii
  • 10,091
  • 18
  • 83
  • 151
  • 4
    Mode test is the key thing here and you saved my life! Thanks a lot as it's so difficult to find proper solution when I tried to debug unit test in vscode, what a combination! – Calvin Zhou Oct 03 '19 at 05:11
  • 2
    Does anyone know what the change would be to debug a "go test" case remotely? – learning2learn May 04 '20 at 16:19
  • 7
    This solution used to work for me, now it doesn't. I'm not sure what changed but I had to change `"program": "${file}"` to `"program": "${relativeFileDirname}"` because VSCode was not finding the other files in the package. – Jerinaw Sep 08 '21 at 01:49
  • In fact MODE = TEST was the answer I did no found in any other place! Thank you for share it with us. – KpsLok Nov 02 '21 at 15:36
  • I am pleased with this package to handle multiple conf: https://marketplace.visualstudio.com/items?itemName=DarrenLevine.auto-debug You can configure to run "Test Current File" for "*_test.go" and "Code" for other go files to run the main program. – Eric Burel Sep 30 '22 at 13:50
4

For the mode, you can select auto which would choose either debug or test depending on active editor window.

All options for mode are auto, debug, test, exec, replay, core.

The resulting launch.json would look like:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch file",
            "type": "go",
            "request": "launch",
            "mode": "auto",
            "program": "${file}"
        }
    ]
}
Saeed
  • 550
  • 1
  • 7
  • 12
1

In my case it was not working because I named my file main_tests.go instead of main_test.go

Tono Nam
  • 34,064
  • 78
  • 298
  • 470