Running Unit Tests with Custom Flags
In Visual Studio Code (VS Code), you can encounter challenges running and debugging unit tests when you need to provide specific flags. Let's delve into the issue and provide a comprehensive solution.
Issue Overview
When running unit tests from VS Code, users may need to specify custom flags, such as -ldflags in the example provided. However, they have faced difficulties when integrating these flags into VS Code's test runner.
Working Configurations
Through experimentation, it has been discovered that two separate configurations are needed to achieve both run test and debug test functionality:
Run test:
"go.testFlags": [
"-ldflags",
"-X google.golang.org/protobuf/reflect/protoregistry.conflictPolicy=warn"
]
Debug test:
"go.testFlags": [
"-ldflags",
"'-X google.golang.org/protobuf/reflect/protoregistry.conflictPolicy=warn'"
]
Underlying Issue
The reason for the different configurations lies in how VS Code generates the test command. When debugging, VS Code adds additional parameters to the command, which affects the way the flags are interpreted. As such, the single quotes in the debug configuration are necessary to ensure that the flags are passed correctly.
Possible Solution
An alternative method suggested for debugging complex tests is to compile the test binary and start a dlv debugging session. This allows for finer control over the test execution and debugging experience.
Using dlv for Debugging
The following steps outline how to use dlv for debugging unit tests:
Compile the test binary with the necessary flags, such as:
go test -c -ldflags "-X google.golang.org/protobuf/reflect/protoregistry.conflictPolicy=warn" -gcflags="all=-N -l"
Start a headless dlv session:
dlv exec ./foo.test --headless --listen=:2345 --log --api-version=2 -- -count=1 -- $(pwd)/some/path
In VS Code, open the Launch Configuration file (Debug: Open launch.json). Create a configuration similar to the following:
{
"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"
}
]
}
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3