03. Test的写法

2019-05-30  本文已影响0人  大鱼人Echo
package main

import (
    "testing"
)

func TestPrint(t *testing.T) {
    res := Print1to20()
    if res != 210 {
        t.Errorf("Wrong result of Print1to20")
    }
}
package main

import (
    "fmt"
    "testing"
)

func testPrint(t *testing.T) {
    // 暂时跳过当前测试
    // t.SkipNow()
    res := Print1to20()
    // testPrint(t)
    fmt.Println("hey")
    if res != 210 {
        t.Errorf("Wrong result of Print1to20")
    }
}

func testPrint2(t *testing.T) {
    res := Print1to20()
    res++
    if res != 211 {
        t.Errorf("Test Print2 Failed")
    }
}

func TestAll(t *testing.T) {
    // 按顺序执行test
    t.Run("test1", testPrint)
    t.Run("test2", testPrint2)
}

func TestMain(m *testing.M) {
    fmt.Println("Tests begins...")
    // 注意: 如果不调用Run(), 其他test用例都不会执行!
    // m.Run()
}

// 测试中间步骤
// func testPrint(t *testing.T) {
//  fmt.Println("test中间步骤")
// }

上一篇 下一篇

猜你喜欢

热点阅读