1. 简介

我们知道单元测试函数需要传递一个testing.T类型的参数,而性能测试函数需要传递一个testing.B类型的参数,该参数可用于控制测试的流程,比如标记测试失败等。

testing.Ttesting.B属于testing包中的两个数据类型,该类型提供一系列的方法用于控制函数执行流程,考虑到二者有一定的相似性,所以Go实现时抽象出一个testing.common作为一个基础类型,而testing.Ttesting.B则属于testing.common的扩展。

本节,我们重点看testing.common,通过其成员及方法,来了解其实现原理。

2. 数据结构

// common holds the elements common between T and B and
// captures common methods such as Errorf.
type common struct {
    mu      sync.RWMutex        // guards this group of fields
    output  []byte              // Output generated by test or benchmark.
    w       io.Writer           // For flushToParent.
    ran     bool                // Test or benchmark (or one of its subtests) was executed.
    failed  bool                // Test or benchmark has failed.
    skipped bool                // Test of benchmark has been skipped.
    done    bool                // Test is finished and all subtests have completed.
    helpers map[string]struct{} // functions to be skipped when writing file/line info

    chatty     bool   // A copy of the chatty flag.
    finished   bool   // Test function has completed.
    hasSub     int32  // written atomically
    raceErrors int    // number of races detected during test
    runner     string // function name of tRunner running the test

    parent   *common
    level    int       // Nesting depth of test or benchmark.
    creator  []uintptr // If level > 0, the stack trace at the point where the parent called t.Run.
    name     string    // Name of test or benchmark.
    start    time.Time // Time test or benchmark started
    duration time.Duration
    barrier  chan bool // To signal parallel subtests they may start.
    signal   chan bool // To signal a test is done.
    sub      []*T      // Queue of subtests to be run in parallel.
}

2.1. common.mu

读写锁,仅用于控制本数据内的成员访问。

2.2. common.output

存储当前测试产生的日志,每产生一条日志则追加到该切片中,待测试结束后再一并输出。

2.3. common.w

子测试执行结束需要把产生的日志输送到父测试中的output切片中,传递时需要考虑缩进等格式调整,通过w把日志传递到父测试。

2.4. common.ran

仅表示是否已执行过。比如,根据某个规范筛选测试,如果没有测试被匹配到的话,则common.ran为false,表示没有测试运行过。

2.5. common.failed

如果当前测试执行失败,则置为true。

2.6. common.skipped

标记当前测试是否已跳过。

2.7. common.done

表示当前测试及其子测试已结束,此状态下再执行Fail()之类的方法标记测试状态会产生panic。

2.8. common.helpers

标记当前为函数为help函数,其中打印的日志,在记录日志时不会显示其文件名及行号。

2.9. common.chatty

对应命令行中的-v参数,默认为false,true则打印更多详细日志。

2.10. common.finished

如果当前测试结束,则置为true。

2.11. common.hasSub

标记当前测试是否包含子测试,当测试使用t.Run()方法启动子测试时,t.hasSub则置为1。

2.12. common.raceErrors

竞态检测错误数。

2.13. common.runner

执行当前测试的函数名。

2.14. common.parent

如果当前测试为子测试,则置为父测试的指针。

2.15. common.level

测试嵌套层数,比如创建子测试时,子测试嵌套层数就会加1。

2.16. common.creator

测试函数调用栈。

2.17. common.name

记录每个测试函数名,比如测试函数TestAdd(t *testing.T), 其中t.name即“TestAdd”。 测试结束,打印测试结果会用到该成员。

2.18. common.start

记录测试开始的时间。

2.19. common.duration

记录测试所花费的时间。

2.20. common.barrier

用于控制父测试和子测试执行的channel,如果测试为Parallel,则会阻塞等待父测试结束后再继续。

2.21. common.signal

通知当前测试结束。

2.22. common.sub

子测试列表。

3. 成员方法

3.1. common.Name()

// Name returns the name of the running test or benchmark.
func (c *common) Name() string {
    return c.name
}

该方法直接返回common结构体中存储的名称。

3.2. common.Fail()


// Fail marks the function as having failed but continues execution.
func (c *common) Fail() {
    if c.parent != nil {
        c.parent.Fail()
    }
    c.mu.Lock()
    defer c.mu.Unlock()
    // c.done needs to be locked to synchronize checks to c.done in parent tests.
    if c.done {
        panic("Fail in goroutine after " + c.name + " has completed")
    }
    c.failed = true
}

Fail()方法会标记当前测试为失败,然后继续运行,并不会立即退出当前测试。如果是子测试,则除了标记当前测试结果外还通过c.parent.Fail()来标记父测试失败。

3.3. common.FailNow()

func (c *common) FailNow() {
    c.Fail()
    c.finished = true
    runtime.Goexit()
}

FailNow()内部会调用Fail()标记测试失败,还会标记测试结束并退出当前测试协程。 可以简单的把一个测试理解为一个协程,FailNow()只会退出当前协程,并不会影响其他测试协程,但要保证在当前测试协程中调用FailNow()才有效,不可以在当前测试创建的协程中调用该方法。

3.4. common.log()

func (c *common) log(s string) {
    c.mu.Lock()
    defer c.mu.Unlock()
    c.output = append(c.output, c.decorate(s)...)
}

common.log()为内部记录日志入口,日志会统一记录到common.output切片中,测试结束时再统一打印出来。 日志记录时会调用common.decorate()进行装饰,即加上文件名和行号,还会做一些其他格式化处理。 调用common.log()的方法,有Log()、Logf()、Error()、Errorf()、Fatal()、Fatalf()、Skip()、Skipf()等。

注意:单元测试中记录的日志只有在执行失败或指定了-v参数才会打印,否则不会打印。而在性能测试中则总是被打印出来,因为是否打印日志有可能影响性能测试结果。

3.5. common.Log(args ...interface{})

func (c *common) Log(args ...interface{}) {
    c.log(fmt.Sprintln(args...))
}

common.Log()方法用于记录简单日志,通过fmt.Sprintln()方法生成日志字符串后记录。

3.6. common.Logf(format string, args ...interface{})

func (c *common) Logf(format string, args ...interface{}) {
    c.log(fmt.Sprintf(format, args...))
}

common.Logf()方法用于格式化记录日志,通过fmt.Sprintf()生成字符串后记录。

3.7. common.Error(args ...interface{})

// Error is equivalent to Log followed by Fail.
func (c *common) Error(args ...interface{}) {
    c.log(fmt.Sprintln(args...))
    c.Fail()
}

common.Error()方法等同于common.Log()+common.Fail(),即记录日志并标记失败,但测试继续进行。

3.8. common.Errorf(format string, args ...interface{})

// Errorf is equivalent to Logf followed by Fail.
func (c *common) Errorf(format string, args ...interface{}) {
    c.log(fmt.Sprintf(format, args...))
    c.Fail()
}

common.Errorf()方法等同于common.Logf()+common.Fail(),即记录日志并标记失败,但测试继续进行。

3.9. common.Fatal(args ...interface{})

// Fatal is equivalent to Log followed by FailNow.
func (c *common) Fatal(args ...interface{}) {
    c.log(fmt.Sprintln(args...))
    c.FailNow()
}

common.Fatal()方法等同于common.Log()+common.FailNow(),即记录日志、标记失败并退出当前测试。

3.10. common.Fatalf(format string, args ...interface{})

// Fatalf is equivalent to Logf followed by FailNow.
func (c *common) Fatalf(format string, args ...interface{}) {
    c.log(fmt.Sprintf(format, args...))
    c.FailNow()
}

common.Fatalf()方法等同于common.Logf()+common.FailNow(),即记录日志、标记失败并退出当前测试。

3.11. common.skip()

func (c *common) skip() {
    c.mu.Lock()
    defer c.mu.Unlock()
    c.skipped = true
}

common.skip()方法标记当前测试为已跳过状态,比如测试中检测到某种条件,不再继续测试。该函数仅标记测试跳过,与测试结果无关。测试结果仍然取决于common.failed。

3.12. common.SkipNow()

func (c *common) SkipNow() {
    c.skip()
    c.finished = true
    runtime.Goexit()
}

common.SkipNow()方法标记测试跳过,并标记测试结束,最后退出当前测试。

3.13. common.Skip(args ...interface{})

// Skip is equivalent to Log followed by SkipNow.
func (c *common) Skip(args ...interface{}) {
    c.log(fmt.Sprintln(args...))
    c.SkipNow()
}

common.Skip()方法等同于common.Log()+common.SkipNow()。

3.14. common.Skipf(format string, args ...interface{})

// Skipf is equivalent to Logf followed by SkipNow.
func (c *common) Skipf(format string, args ...interface{}) {
    c.log(fmt.Sprintf(format, args...))
    c.SkipNow()
}

common.Skipf()方法等同于common.Logf() + common.SkipNow()。

3.15. common.Helper()

// Helper marks the calling function as a test helper function.
// When printing file and line information, that function will be skipped.
// Helper may be called simultaneously from multiple goroutines.
func (c *common) Helper() {
    c.mu.Lock()
    defer c.mu.Unlock()
    if c.helpers == nil {
        c.helpers = make(map[string]struct{})
    }
    c.helpers[callerName(1)] = struct{}{}
}

common.Helper()方法标记当前函数为help函数,所谓help函数,即其中打印的日志,不记录help函数的函数名及行号,而是记录上一层函数的函数名和行号。

Copyright © Kilvn 2022. all right reserved,powered by Gitbook最后更新时间: 2022-02-16 15:57:37

results matching ""

    No results matching ""