在VS Code 中運行和調試帶有標誌的單元測試
要在VS Code 中運行帶有標誌的單元測試,可以修改go. vscode settings.json 檔案中的testFlags 值。然而,這裡遇到的問題是運行和調試測試所需的不同配置。
運行測試
要使用required 標誌運行測試,可以使用以下配置使用:
<code class="json">"go.testFlags": [ "-ldflags", "-X google.golang.org/protobuf/reflect/protoregistry.conflictPolicy=warn" ]</code>
調試測試
對於調試測試,配置應在標誌周圍包含單引號:
<code class="json">"go.testFlags": [ "-ldflags", "'-X google.golang.org/protobuf/reflect/protoregistry.conflictPolicy=warn'" ]</code>
組合配置
為了避免在配置之間切換,為了避免在配置之間切換,可以嘗試以下組合配置:
<code class="json">"go.testFlags": [ "-ldflags", "'-X google.golang.org/protobuf/reflect/protoregistry.conflictPolicy=warn'", "-ldflags", "-X google.golang.org/protobuf/reflect/protoregistry.conflictPolicy=warn"" ]</code>
使用Dlv 進行調試
或者,可以使用dlv 來調試測試。要在停用最佳化的情況下編譯測試二進位檔案:
go test -c -ldflags "-X google.golang.org/protobuf/reflect/protoregistry.conflictPolicy=warn" -gcflags="all=-N -l"
啟動無頭dlv 會話:
dlv exec ./foo.test --headless --listen=:2345 --log --api-version=2 -- -count=1 -- $(pwd)/some/path
透過建立launch.json 檔案將VS Code 連接到dlv 會話:
<code class="json">{ ... "configurations": [ { "name": "Debug Test", "type": "go", "request": "attach", "mode": "remote", "port": 2345, "host": "127.0.0.1", "showLog": true, "trace": "log" } ] }</code>
以上是如何在 VS Code 中使用標誌來執行和偵錯單元測試?的詳細內容。更多資訊請關注PHP中文網其他相關文章!