在Visual Studio代码中调试Go测试

Vit*_*lii 5 unit-testing go visual-studio-code

在我的Windows机器上,我安装了Visual Studio代码.要手动运行测试,我将进入项目文件夹并输入

go test main_test.go
Run Code Online (Sandbox Code Playgroud)

它完美地运作.

在此输入图像描述

但我有一种情况需要调试我的测试以了解正在发生的事情.

为此,我打开launch.json并添加配置

  {
        "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
    }
Run Code Online (Sandbox Code Playgroud)

按完F5后,我有

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
Run Code Online (Sandbox Code Playgroud)

任何想法为什么会出现这个错误以及它正在寻找什么可执行文件?

Vit*_*lii 10

要启动测试调试器,我为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",
            "type": "go",
            "request": "launch",
            "mode": "test",
            "remotePath": "",
            "port": 2345,
            "host": "127.0.0.1",
            "program": "${workspaceRoot}/ordering/service_test.go",
            "env": {},
            "args": [],
            "showLog": true
        }       
    ]
}
Run Code Online (Sandbox Code Playgroud)

需要手动更改文件路径以调试另一个测试

 "program": "${workspaceRoot}/ordering/service_test.go",
Run Code Online (Sandbox Code Playgroud)

此配置也不支持标签.必须禁用测试文件中的所有标记

// +build unit
...
Run Code Online (Sandbox Code Playgroud)

  • 这个解决方案曾经对我有用,但现在不行了。我不确定发生了什么变化,但我必须将 `"program": "${file}"` 更改为 `"program": "${relativeFileDirname}"` 因为 VSCode 找不到包中的其他文件。 (7认同)
  • 模式测试是这里的关键,你救了我的命!非常感谢,当我尝试在 vscode 中调试单元测试时,很难找到合适的解决方案,真是一个组合! (3认同)
  • 有谁知道远程调试“go test”案例会发生什么变化? (2认同)