test

package
v0.0.0-...-95a312b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 16, 2023 License: MIT Imports: 3 Imported by: 0

README

golang单元测试整理

golang基本的单元测试
  • go语言的单元测试采用内置的测试框架,通过引入testing包以及go test来提供测试功能。
  • _test.go为后缀名的源文件被go test认定为测试文件,这些文件不包含在go build的代码构建中,而是单独通过 go test来编译,执行。
使用gotest生成表格驱动的测试用例
  • gotests详细介绍

  • gotests使用

    • 创建slice.go

    • 使用gotests根据slice.go的内容创建slice_test.go

      gotests -all slice.go -w slice_test.go
      

      生成代码类似如下内容:

      func TestAdd(t *testing.T) {
      	type args struct {
      		s []string
      		a string
      	}
      	tests := []struct {
      		name string
      		args args
      		want []string
      	}{
      	// TODO: Add test cases.
      	}
      	for _, tt := range tests {
      		t.Run(tt.name, func(t *testing.T) {
      			if got := Add(tt.args.s, tt.args.a); !reflect.DeepEqual(got, tt.want) {
      				t.Errorf("Add() = %v, want %v", got, tt.want)
      			}
      		})
      	}
      }
      
mock的使用实践
HTTP服务单元测试
代码覆盖率

参考资料

Documentation

Index

Constants

View Source
const (
	ADDRESS = "shanghai"
)

Variables

This section is empty.

Functions

func Add

func Add(s []string, a string) []string

如果string类型的a在切片中不存在,那么就添加到切片

func HealthCheckHandler

func HealthCheckHandler(w http.ResponseWriter, r *http.Request)

func Union

func Union(s []string, a []string) []string

求两个切片的并集

func Uniq

func Uniq(s []string) (r []string)

返回切片s中的元素(去掉重复部分)

Types

type Person

type Person struct {
	Name    string `json:"name"`
	Address string `json:"address"`
	Age     int    `json:"age"`
}

func GetInfo

func GetInfo(api string) ([]Person, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL