在我的Windows计算机上,我安装了Visual Studio代码.要手动运行测试,我在控制台中转到Projects文件夹,然后输入

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

你知道为什么会发生这个错误吗?它在寻找什么可执行文件?

推荐答案

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

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

// +build unit
...

Go相关问答推荐

未对GoFr中的所有请求进行跟踪

减少在围棋中映射DTO时的重复代码量

从文件读取字节,将其保存到 struct 体并修改值

go-chi: 接受带有反斜杠的 url 路径参数

当我使用 CircleCI 构建 Go Image 时,我得到runtime/cgo: pthread_create failed: Operation not allowed

使用golang sqlc中的引用参数

golang gin 获取 cookie json

如何以干净的方式在中间件中注入 repo 或服务?

如何在 Golang 中打印 2 列表?

如何使用 go-git 将特定分支推送到远程

Go 中如何调用测试函数?

类型/ struct 函数的 GoDoc 示例函数

如何确定作为函数参数传递的指针是否正在被修改或副本是否正在被修改?

从 os.stdout 读取

函数超时和 goroutine 泄漏

Go GCP 同时模拟两个服务帐户

如何使用 httputil.ReverseProxy 设置 X-Forwarded-For

try 创建新的 etcdv3 客户端时出现pc error: code = Unavailable desc = error reading from server: EOF

有没有一种方法可以确保传递的值具有使用泛型的某些字段?

如何在接口上调用 len()?