- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要在 Go 中构建一个在执行时接受一些命令行参数的测试用例。
测试文件看起来很普通:
package somelogic_test
import (
sl "example.com/somelogic"
"flag"
"testing"
)
func TestSomeLogic(t *testing.T) {
flag.Parse()
strSlice := flag.Args()
sl.SomeLogic(strSlice)
}
当我以 go test -v somelogic_test.go -args arg1 arg2 arg3
运行测试时,它像 charm 一样工作,接受 arg1、arg2、arg3 作为字符串的一部分并将其传递给 SomeLogic功能。到目前为止,还不错。
现在我想在 VSCode 中调试执行。我找到了 this link这建议将所有命令行参数放在 launch.json 文件的“args”字段中。所以我按照建议做了,我的 launch.json 配置现在如下所示:
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch file",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}",
"env": {},
"args": ["-v","somelogic_test.go","-args","arg1","arg2","arg3"]
}
]
}
但是,这根本不起作用,因为当我在运行测试和调试测试模式下运行 somelogic_test.go 时,flag.Parse()
和 flag 不接受任何参数。 Args()
,结果 strSlice
只是一个空 slice 。
请建议如何修改 launch.json 以便 flag.Parse()
接受命令行参数并可用于进一步调试?
最佳答案
引用go help testflag
:
The 'go test' command takes both flags that apply to 'go test' itselfand flags that apply to the resulting test binary.
您需要区分这两种标志,因为在带有 Go 扩展的 VSCode 中调试测试时,它会先编译测试二进制文件,然后将参数传递给它。在你的 go test
命令行中,-v somelogic_test.go
应该是 go test
本身的标志,arg1 arg2 arg3
应该是生成的测试二进制文件的标志,不是吗?
试试这个,它在我的环境中有效:
{
"version": "0.2.0",
"configurations": [
{
"name": "Test file",
"type": "go",
"request": "launch",
"mode": "test",
// Indicate the target here
"program": "${fileDirname}/somelogic_test.go",
// or just use "The current opened file"
// "program": "${file}",
"env": {},
"args": ["-test.v", "--", "arg1","arg2","arg3"]
}
]
}
However, this does not work at all, cause when I run somelogic_test.goboth in Run Test and Debug Test modes no arguments are accepted withflag.Parse() and flag.Args(), and as a result strSlice is just anempty slice.
注意 run test
和 debug test
按钮出现在测试函数上方,不要使用 launch.json
。转到 Run and debug
侧边栏以启动您设置的特定配置。
关于go - 在 VSCode 中使用命令行参数调试 Go 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71767280/
要构建我的远程环境,我需要设置几个环境变量(它们在 docker-compose 文件中使用)。这些是在我的 ZSH 环境中设置的,因此从终端运行 docker-compose build 可以按预期
这是vscode在主机中安装vscode-server时的日志 我发现它得到了 vscode-server 提交 ID,如下日志: [13:07:27.334] Using commit id "f8
在 devContainer 实例中安装 LiveShare 扩展时出现错误。 例如使用:https://github.com/microsoft/vscode-remote-try-go/然后将扩展
在 devContainer 实例中安装 LiveShare 扩展时出现错误。 例如使用:https://github.com/microsoft/vscode-remote-try-go/然后将扩展
我尝试在代码中构建和调试扩展。 我从https://github.com/microsoft/vscode-wordcount下载了字数统计的样本. 当我单击 F5 时,未生成 ./out 文件夹,并
你好,我最近从 Atom 切换到 Vs-code,并尝试了一些解释如何在他们的网站上添加语言支持的教程,我知道语言支持应该有着色器 问题是如何为自定义语言添加颜色?当我基本上使用每种语言时,我习惯了自
为了为我的数据科学工作流提供足够的计算能力,我在远程机器上使用 Docker 容器。虽然我可以通过 vscode-remote 连接到我的远程机器,但我无法连接到运行在这台机器上的 Docker 容器
我有一个创建多个 Docker 镜像的项目。我想在 vscode-remote 中为每个图像设置一个 devcontainer,以便我可以为每个图像启动一个容器。 我一次只需要启动并连接到一个容器/图
我认为这是可以通过编辑实现的 keybindings.json .由于我似乎无法找到可用命令的列表,因此通过自动完成我已经做到了这一点: { "key": "SHORTCUT"
gopls 需要在工作区的根目录下有一个模块。 您可以通过将每个模块作为工作区文件夹打开来处理多个模块。 此工作流程的改进即将推出 ( https://github.com/golang/go/iss
我的电脑上已经安装了 vscode。不久前我注意到它现在包含在 anaconda 发行版中。与我已经安装的 vscode 相比,使用 anaconda 附带的 vscode 有什么好处吗? 仅供引用,
我的电脑上已经安装了 vscode。不久前我注意到它现在包含在 anaconda 发行版中。与我已经安装的 vscode 相比,使用 anaconda 附带的 vscode 有什么好处吗? 仅供引用,
我想为 VSCode 编写一个扩展,重用 vscode-python 的重构/重命名功能扩大。这样,当用户执行我的命令时,我的扩展程序将对 .py 文件的变量进行重命名。我不知道 vscode-pyt
环境 vscode 版本 1.19.1 (1.19.1) 鲁博科普 (0.52.1) Darwin mbp 16.7.0 Darwin 内核版本 16.7.0:2017 年 10 月 4 日星期三 0
我在写 Markdown 文本时尝试自动换行:我希望当我输入比 wrap line length 长的行时自动换行设置,由硬 中断(即 newline 字符)。 我已经设定 word wrap至 wo
我想制作一个涉及发布到我的 API 的 VSC 扩展,但是当我将我的 fetch 语法写出以发布到我的服务器时,它不起作用。所以我想也许我需要添加 node-fetch,所以我做了npm i --sa
我有一个 master 分支,它是生产分支,因此我创建了几个其他分支来修复和缺陷。我在这些分支中做了一些更改,所以我在这些分支中提交了很多次。根据政策,我必须为 master 分支中的缺陷创建单个提交
我正在使用 Visual Studio Code Insiders 版本 1.65.0-insider。直到 3 天前,我通过 VPN 从工作笔记本电脑连接到工作中的远程服务器时没有遇到任何问题。我有
在 Windows 上,我必须在打开的每个新终端 session 上运行命令 start-ssh-agent.cmd。我的开发环境是VSCode,每天都会打开十几个新终端。每次打开终端后,我都必须手动
我最近开始使用 VSCode,我真的很喜欢它。我用谷歌搜索了我的问题,并试图找到与 VSCode 相关的任何答案。 考虑我有以下代码: if (a === 'some condition') re
我是一名优秀的程序员,十分优秀!