运行和调试带有标志的单元测试。

huangapple go评论98阅读模式
英文:

Run and Debug unit tests with flags

问题

我想要能够在VS Code中使用测试资源管理器或代码镜头来运行和调试单元测试。
但是为了运行我的测试,我需要添加这个标志:

-ldflags "-X google.golang.org/protobuf/reflect/protoregistry.conflictPolicy=warn"

因此,在我的vscode settings.json文件中,我添加了以下json:

"go.testFlags": [
    "-ldflags",
    "\"-X google.golang.org/protobuf/reflect/protoregistry.conflictPolicy=warn\""
]

现在,当我点击测试资源管理器或代码镜头中的运行测试按钮时,VS Code会生成以下命令:

/opt/homebrew/bin/go test -timeout 30s -run ^TestCreateNamespace$ github.com/SomePath/SomeRepo/internal/models/v2 -ldflags "-X google.golang.org/protobuf/reflect/protoregistry.conflictPolicy=warn"

但是测试用例失败,并显示以下错误:

panic: proto: extension number 1042 is already registered on message google.protobuf.FileOptions
See https://developers.google.com/protocol-buffers/docs/reference/go/faq#namespace-conflict

这正是我在不提供-ldflags的go test命令时所期望的错误。但令人惊讶的是,当我复制上述由vs code生成的完全相同的测试命令并在终端中运行时,测试用例通过了。
除了从VS Code中运行测试之外,我还希望能够通过设置断点和逐步执行代码来调试测试。

**开发环境:**我使用的是arm64 apple M1 Mac。

**更新:**在调整go.testFlags值后,我发现:

  1. 这个配置适用于vs code的运行测试功能:
"go.testFlags": [
    "-ldflags",
    "-X google.golang.org/protobuf/reflect/protoregistry.conflictPolicy=warn"
]
  1. 这个配置适用于vs code的调试测试功能:
"go.testFlags": [
    "-ldflags",
    "'-X google.golang.org/protobuf/reflect/protoregistry.conflictPolicy=warn'"
]

(注意调试配置中的额外单引号)。

现在我需要找到一个适用于运行测试调试测试功能的单一配置,或者在vs code的settings.json文件中指定两个不同的配置,以便我可以在不每次更改settings.json文件的情况下使用这两个功能。(我怀疑这可能是一个深入的问题)

英文:

I want to be able to run and debug unit tests within VS Code using the test explorer or code lens.
But in order to run my tests, I need to add this flag:

-ldflags "-X google.golang.org/protobuf/reflect/protoregistry.conflictPolicy=warn" 

Therefore, in my vscode settings.json file, I have added this json:

"go.testFlags": [        
    "-ldflags",
    "\"-X google.golang.org/protobuf/reflect/protoregistry.conflictPolicy=warn\""
]

Now when I click the Run Test button in test explorer or in the code lens, VS Code generates this command:

/opt/homebrew/bin/go test -timeout 30s -run ^TestCreateNamespace$ github.com/SomePath/SomeRepo/internal/models/v2 -ldflags "-X google.golang.org/protobuf/reflect/protoregistry.conflictPolicy=warn"

but the test case fails with this error:

panic: proto: extension number 1042 is already registered on message google.protobuf.FileOptions
See https://developers.google.com/protocol-buffers/docs/reference/go/faq#namespace-conflict

And this is the exact error that I am expecting if I dont suply the -ldflags in my go test command. But the surprising thing is that when I copy the exact same vs code generated test command mentioned above and run that in my terminal then the test case passes.
Along with running the tests from Vs Code, I would also like to be able to debug them by setting breakpoints and stepping through code.

Dev Environment: I am on an arm64 apple M1 Mac if that matters.

UPDATE: After fiddling around with the go.testFlags values, I have found that:

  1. This configuration works for vs code run test functionality:
"go.testFlags": [        
    "-ldflags",
    "-X google.golang.org/protobuf/reflect/protoregistry.conflictPolicy=warn"
]
  1. This configuration works for vs code debug test functionality:
"go.testFlags": [        
    "-ldflags",
    "'-X google.golang.org/protobuf/reflect/protoregistry.conflictPolicy=warn'"
]

(Notice the extra single quotes in debug configuration).

Now I need to find a single configuration that works for both run test as well as debug test functionalities, Or somehow specify 2 different configs for run test and debug test in settings.json file of vs code so that I can use both functionalities without making changes to the settings.json file every-time. (This might be a delve thing I suspect)

答案1

得分: 2

我不能代表VS Code发言,但是当涉及到调试一些较复杂的测试(例如godog cucumber-style测试)时,我通常会像这样编译测试二进制文件:

go test -c -gcflags="all=-N -l"

你可以将go test -c替换为go build,所以我不明白为什么你不能简单地使用:

go test -c -ldflags "-X google.golang.org/protobuf/reflect/protoregistry.conflictPolicy=warn" -gcflags="all=-N -l"

然后,你可以以无头模式启动一个dlv会话,供你的编辑器连接。假设go test -c生成了一个名为"foo.test"的二进制文件:

dlv exec ./foo.test --headless --listen=:2345 --log --api-version=2 -- -count=1 -- $(pwd)/some/path

其中-count=1是一个占位符,用于传递给go test的标志,-- $(pwd)/some/path可以是(如果你有cucumber风格的测试)一个.feature文件的路径。现在你可以连接你的编辑器到dlv会话并开始调试。询问我的一些同事在VSCode中如何操作,他们提到了一个命令面板和运行Debug: Open launch.json,它应该看起来像这样:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug Test",
            "type": "go",
            "request": "attach",
            "mode": "remote",
            "port": 2345,
            "host": "127.0.0.1",
            "showLog": true,
            "trace": "log"
        }
    ]
}

设置断点,然后你应该能够打开调试面板并运行Debug test


如果有人想知道,Vim的流程基本相同,只是不需要json文件。使用vim-go插件,一旦dlv会话运行起来,你可以设置断点(:GoDebugBreakpoint),然后连接到dlv::GoDebugConnect 127.0.0.1:2345

从那时起,它基本上与任何调试会话一样,:GoDebugContinue:GoDebugStep:GoDebugStepOut:GoDebugPrint varname等等。

英文:

I can't speak for VS Code, but when it comes to debugging tests that are a bit more complex to step through (e.g. godog cucumber-style tests) I usually compile the test binary like so:

go test -c -gcflags="all=-N -l"

You can replace go test -c with go build, so I don't see why you wouldn't be able to simply use

go test -c -ldflags "-X google.golang.org/protobuf/reflect/protoregistry.conflictPolicy=warn" -gcflags="all=-N -l"

Then, you can start a dlv session in headless mode for your editor to connect to. Let's say go test -c generated a binary called "foo.test":

dlv exec ./foo.test  --headless --listen=:2345 --log --api-version=2    -- -count=1 -- $(pwd)/some/path

where -count=1 is a placeholder for the flags you would normally pass to go test, and -- $(pwd)/some/path could be (in case you have cucumber style tests) a path to a .feature file. You can now connect your editor to the dlv session and start debugging. Asking some of my colleagues how that works in VSCode, they said something about a command palette and running Debug: Open launch.json, which should look something like this:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Debug Test",
            "type": "go",
            "request": "attach",
            "mode": "remote",
            "port": 2345,
            "host": "127.0.0.1",
            "showLog":true,
            "trace":"log"
        }
    ]
}

Set the breakpoints, and you should be able to open the debug panel and run Debug test.


In case anyone wonders, the flow for Vim is pretty much the same, except that there is no need for a json file. Using the vim-go plugin, once the dlv session is running, you set your breakpoints (:GoDebugBreakpoint), and connect to dlv: :GoDebugConnect 127.0.0.1:2345.

From that point on, it's pretty much the same as any debug session :GoDebugContinue, :GoDebugStep, :GoDebugStepOut, :GoDebugPrint varname and so on)

答案2

得分: 1

我建议使用启动配置而不是在settings.json中进行更改。

所以你需要按照这里的指示创建一个文件.vscode/launch.json,并在文件中添加以下行:

"buildFlags": "-ldflags='-X google.golang.org/protobuf/reflect/protoregistry.conflictPolicy=warn'",

这似乎适用于运行和调试项目。

英文:

I recommend using Launch Configuration instead of making changes in settings.json.

So you need to create a file at .vscode/launch.json as instructed here and add the following line to the file:

"buildFlags": "-ldflags='-X google.golang.org/protobuf/reflect/protoregistry.conflictPolicy=warn'",

It seems to work for both running and debugging the project.

答案3

得分: 0

你试过这个吗?

"go.testFlags": [        
    "-ldflags",
    "-X",
    "google.golang.org/protobuf/reflect/protoregistry.conflictPolicy=warn"
]

当你在VSCode中创建新任务时,你需要将每个以空格分隔的单词/字符写成不同的参数。

英文:

Have you tried this?

"go.testFlags": [        
    "-ldflags",
    "-X",
    "google.golang.org/protobuf/reflect/protoregistry.conflictPolicy=warn"
]

When you create new tasks in VSCode you need to write each space-separated word/character as different parameters.

答案4

得分: 0

我遇到了同样的问题,并通过在settings.json中设置环境变量来解决,像这样:

    "go.toolsEnvVars": {
        "GOLANG_PROTOBUF_REGISTRATION_CONFLICT": "warn"
    },
英文:

I met the same problem and solved by setting env vars in settings.json like this:

    "go.toolsEnvVars": {
        "GOLANG_PROTOBUF_REGISTRATION_CONFLICT": "warn"
    },

huangapple
  • 本文由 发表于 2022年9月8日 18:45:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/73647814.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定