go 单元测试 Vscode(vscode)

launch.json 配置

{
            "name": "go_course_ut",
            "type": "go",
            "request": "launch",
            "mode": "test",
            "port": 2346,
            "host": "127.0.0.1",
            "program": "${workerspace}/gotour/course/course2_test.go",
            "args": [],
        },

文件目录

屏幕快照

文件测试

  • course2.go
package course

// ForStudy  for 循环
func ForStudyNormal(num int) int {
    sum := 0
    for i := 0; i <= num; i++ {
        sum += i
    }
    return sum
}
  • course2_test.go
package course_test

import (
    "testing"

    "github.com/zs1621/gotour/course"
)

func TestFor(t *testing.T) {
    var shouldSuccess = []struct {
        input  int
        except int
    }{
        {1, 1},
        {10, 10 * 11 / 2},
    }
    for _, td := range shouldSuccess {
        if course.ForStudyNormal(td.input) == td.except {
            t.Errorf("should success")
        } else {
            t.Errorf("error")
        }
    }
}

附命令行测试

go test -v

你可能感兴趣的:(go 单元测试 Vscode(vscode))