文章目录

文件名须以”_test.go”结尾

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

举例:/hello_test.go

1
2
3
4
5
6
7
8
9
10
11
12
13
14

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

1
2
3
4
5
6
7
8
=== 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 -run TestHello

1
2
3
4
5
6

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

默认是正则表达式的形式,如果精确某个函数: $ go test -run '^TestLong$' -v

文章目录