美文网首页GO
Go test工具链

Go test工具链

作者: 会长__ | 来源:发表于2018-08-21 23:54 被阅读85次

1.简介

go test命令用于对Go语言编写的程序进行测试。这种测试是以代码包为单位的。


image.png

2. 简单介绍

package main
// 文件名以_test.go 结尾
import (
    "testing"
    "fmt"
)

// 方法名必须已Test开头,参数必须为*testing.T
func TestPrint(t *testing.T)  {
    t.SkipNow() // 跳过此Test函数,只能放到第一行
    res := Print1to20()
    fmt.Println("hey")
    if res != 210 {
        t.Errorf("ERR")
    }
}

// 1-20的和
func Print1to20() (sum int) {
    for a := 1; a<=20; a++ {
        sum += a
    }
    return sum
}

// 这个不会执行
func testPrint(t *testing.T)  {
    fmt.Println("I did it")
}

执行 (根据注释中的内容做相应的调整执行)

image.png

3.更合理的写法

image.png
package main
// 文件名以_test.go 结尾
import (
    "testing"
    "fmt"
)

// 子test
func testPrint(t *testing.T)  {
    res := Print1to20()
    fmt.Println("hey")
    if res != 210 {
        t.Errorf("ERR")
    }
}

// 子test
func testPrint2(t *testing.T)  {
    res := Print1to20()
    fmt.Println("hello")
    if res != 211 {
        t.Errorf("2 ERR")
    }
}

// 1-20的和
func Print1to20() (sum int) {
    for a := 1; a<=20; a++ {
        sum += a
    }
    return sum
}

func TestAll(t *testing.T)  {
    t.Run("testPrint", testPrint)
    t.Run("testPrint2", testPrint2)
}

func TestMain(m *testing.M)  {
    fmt.Println("Test begins....")
    m.Run() // 如果不加这句,只会执行Main
}

结果

image.png

4.PHP前沿学习群: 257948349 go也收

相关文章

  • Golang笔记之testing

    Go工具链 常用命令 build install get fmt test golang的test Go的test...

  • Go test工具链

    1.简介 go test命令用于对Go语言编写的程序进行测试。这种测试是以代码包为单位的。image.png 2....

  • 16.手撕Go语言-测试

    Go提供了test工具用于代码的单元测试,test工具会查找包下以_test.go结尾的文件,调用测试文件中以Te...

  • Go test和Testify组件

    Go test预判工具Testify go test golang编写测试用例时,首先需要建立以_test结尾的g...

  • GO学习笔记(17) -测试

    测试工具 —go test go test命令用于对Go语言编写的程序进行测试。 测试规范 测试文件必须为"_te...

  • Go test工具

    不写测试的开发不是好程序员。我个人非常崇尚TDD(Test Driven Development)的,然而可惜的是...

  • Golang压力测试

    Go Test工具 Go语言中的测试依赖go test命令。编写测试代码和编写普通的Go代码过程是类似的,并不需要...

  • go语言监控(内部使用)

    一、pprof go test -c go_test.go$ ./main.test -test.bench=. ...

  • Go学习日志:func、interface、test

    Map里装填func Test框架 go test -v converts_test.go converts.go...

  • 0.1.1 报错:go run: cannot run *_te

    ⚠️报错 -> go run: cannot run *_test.go files (abc_test.go)...

网友评论

    本文标题:Go test工具链

    本文链接:https://www.haomeiwen.com/subject/wpisiftx.html