Published 2026-03-01.
Time to read: 1 minutes.
golang collection.
Debugging Go Tests In Visual Studio Code
To debug Go tests using Visual Studio Code, use the Delve debugger.
Install the latest release of Delve:
$ go install github.com/go-delve/delve/cmd/dlv@latest
Create a new launch configuration in .vscode/launch.json
that resembles:
{
"args": [
"-test.run", "Test.*",
"-test.v"
],
"mode": "test",
"name": "Run core/cmd/sc_router tests",
"program": "${workspaceFolder}/core/cmd/sc_router",
"request": "launch",
"type": "go",
},
Comments on the above:
-
The names of
dlvcommand-line arguments such as-runand-vmust be prefaced withtest.as shown inlaunch.json. -
The
programargument is the directory path (not the file name) containing the tests. All source files in that package will be examined, including the_test.gofiles, so the-test.runoption can filter for the function(s) you want to test. -
If you set
programto a specific file path instead of a directory path, the Go test runner often fails to resolve the package context correctly because Go tests are compiled and executed at the package level, not the file level. -
The names of the tests to run is specified by the argument for
-test.run, which is a regex. The regexTest.*runs all tests whose names start withTest.
Press CTRL+Shift+D to open the Run and Debug view in the sidebar.
Select the new configuration (e.g., Run core/cmd/sc_router tests) from the dropdown menu at the top of the sidebar.
Set breakpoints in the Go code for the test that you want to debug.
Click the green play button or press F5 to start debugging.
Delve Help Message
The help message for Delve is:
Delve is a source level debugger for Go programs. Delve enables you to interact with your program by controlling the execution of the process, evaluating variables, and providing information of thread / goroutine state, CPU register state and more. The goal of this tool is to provide a simple yet powerful interface for debugging Go programs. Pass flags to the program you are debugging using `--`, for example: `dlv exec ./hello -- server --config conf/config.toml` Usage: dlv [command] Available Commands: attach Attach to running process and begin debugging. completion Generate the autocompletion script for the specified shell connect Connect to a headless debug server with a terminal client. core Examine a core dump. dap Starts a headless TCP server communicating via Debug Adaptor Protocol (DAP). debug Compile and begin debugging main package in current directory, or the package specified. exec Execute a precompiled binary, and begin a debug session. help Help about any command test Compile test binary and begin debugging program. trace Compile and begin tracing program. version Prints version. Additional help topcis: dlv backend Help about the --backend flag. dlv log Help about logging flags. dlv redirect Help about file redirection. Use "dlv [command] --help" for more information about a command.