0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

GolangのTestでブレークポイントを貼りデバッグする(VSCode編)

Posted at

はじめに

表題通り、GolangのTestでブレークポイントを貼りデバッグします。

成果物

go_test.gif

ソースコード

ディレクトリ構成
~/go/src/go_test_delve$ tree
.
├── go.mod
├── main.go
└── main_test.go

1 directory, 3 files
main.go(プロダクトコード)
package main

import "fmt"

func add(a, b int) int {
    return a + b
}

func main() {
    result := add(2, 3)
    fmt.Println("2 + 3 =", result)
}
main_test.go
package main

import (
	"fmt"
	"testing"
)

func TestAdd(t *testing.T) {
    result := add(2, 3)
    expected := 5
    fmt.Println("2 + 3 =", result)

    if result != expected {
        t.Errorf("Expected %d, but got %d", expected, result)
    }
}

デバッグする準備

VSCodeでテストをするには、.vscodeを作成する。

vsocde.json
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch Package Test",
            "type": "go",
            "request": "launch",
            "mode": "test",
            "program": "${fileDirname}",
            "args": [],
            "showLog": true,
            "trace": "log"
        }
    ]
}

→これで準備完了。テストを動かせば、デバッグできるようになります。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?