在Visual Studio代码中调试Go测试

时间:2017-03-29 11:35:59

标签: unit-testing go visual-studio-code

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

go test main_test.go

效果很好。

enter image description here

但我有一种情况需要调试我的测试以了解发生了什么。

为此,我打开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
    }

按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

为什么会出现此错误以及它正在寻找的可执行文件?

1 个答案:

答案 0 :(得分:8)

要启动测试调试器,我为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
        }       
    ]
}

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

 "program": "${workspaceRoot}/ordering/service_test.go",

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

// +build unit
...