美文网首页go语言系统教程golang
使用Go Test测试单个文件和单个方法

使用Go Test测试单个文件和单个方法

作者: 豆瓣奶茶 | 来源:发表于2018-07-19 17:29 被阅读1次

    前置条件:

    1、文件名须以"_test.go"结尾

    2、方法名须以"Test"打头,并且形参为 (t *testing.T)

    举例:/hello_test.go

    package main
     
    import (
        "testing"
        "fmt"
    )
     
    func TestHello(t *testing.T) {
        fmt.Println("TestHello")
    }
     
    func TestWorld(t *testing.T) {
        fmt.Println("TestWorld")
    }
    

    测试整个文件:$ go test -v hello_test.go

    === RUN   TestHello
    TestHello
    --- PASS: TestHello (0.00s)
    === RUN   TestWorld
    TestWorld
    --- PASS: TestWorld (0.00s)
    PASS
    ok      command-line-arguments  0.007s
    

    测试单个函数:$ go test -v hello_test.go -test.run TestHello

    === RUN   TestHello
    TestHello
    --- PASS: TestHello (0.00s)
    PASS
    ok      command-line-arguments  0.008s
    

    相关文章

      网友评论

        本文标题:使用Go Test测试单个文件和单个方法

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