Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging Go tests in Visual Studio Code

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?

like image 337
Vitalii Avatar asked Mar 29 '17 11:03

Vitalii


People also ask

How do I debug a go file?

To debug a program, execute the dlv debug command. Attach the filename at the end of this command, and debugging will start. For example, if you want to debug the main.go file, run the command dlv debug main.go . It is also known as “delve server” because this is a running process waiting for instructions.

How do I enable go definition in Visual Studio Code Golang?

Now Press F12 to go to definition.

Is VS Code good for go?

Using the Go extension for Visual Studio Code, you get features like IntelliSense, code navigation, symbol search, testing, debugging, and many more that will help you in Go development. You can install the Go extension from the VS Code Marketplace.


2 Answers

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 ... 
like image 183
Vitalii Avatar answered Sep 20 '22 00:09

Vitalii


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}"         }     ] } 
like image 28
Saeed Avatar answered Sep 24 '22 00:09

Saeed