日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

一文帶你了解Go語言中的單元測試_Golang

作者:孫琦Ray ? 更新時間: 2022-09-13 編程語言

基本概念

上一節(jié)提到,代碼完成的標(biāo)準(zhǔn)之一還包含了單元測試,這部分也是很多開發(fā)流程中不規(guī)范的地方。寫過單元測試的開發(fā)人員應(yīng)該理解,單元測試最核心的價值是為了證明:為什么我寫的代碼是正確的?也就是從邏輯角度幫你檢查你的代碼。但是另外一方面,如果從單元測試覆蓋率角度來看,單元測試也是非常耗時的,幾乎是三倍于你代碼的開發(fā)時間,所以在很多迭代速度非常快的項(xiàng)目中,單元測試就幾乎沒人要求了。但是單元測試真的能夠從根本上提高代碼質(zhì)量,降低低級錯誤出現(xiàn)的概率。

示例一:取整函數(shù)基本測試

前置條件

Go語言內(nèi)置了單元測試執(zhí)行的指令,由于尚未使用Go Modules方法,我們?nèi)匀灰O(shè)置環(huán)境變量,才能正確進(jìn)行測試

export GO111MODULE=off
go test

代碼

假設(shè)我們對以下函數(shù)進(jìn)行測試

package even

func Even(i int) bool {
    return i % 2 == 0
}

單元測試建立步驟

創(chuàng)建一個單元測試,包括如下步驟:

  • 在相同目錄下創(chuàng)建一個名為*_test.go的文件
  • 執(zhí)行g(shù)o test進(jìn)行測試,將自動識別這些文件
  • 引入testing包
  • 每一個Case的命名都是以func TestXxx(t *testing.T)

編寫單元測試

這里分別對兩種場景進(jìn)行測試,一種是為偶數(shù)的情況,一種是為奇數(shù)的情況,來檢查我們的程序是否按照預(yù)期返回,如果不是則拋出異常信息

package even

import "testing"

func TestEven(t *testing.T) {
    if !Even(2) {
        t.Log("2 should be even!")
        t.Fail()
    }
}

func TestNotEven(t *testing.T) {
    if Even(3) {
        t.Log("3 should not be even!")
        t.Fail()
    }
}

執(zhí)行g(shù)o test后

PASS
ok ??? ?_/root/workspace/go/test_unittest?? ?0.003s

示例二:Fail()函數(shù)

func (t *T) Fail() 讓測試失敗,同一個測試用例中的測試?yán)^續(xù)執(zhí)行,后續(xù)的測試也會繼續(xù)執(zhí)行

package even

import "testing"

func TestTestingFail(t *testing.T) {
    // Let create a fake case, we will call FailNow
    if Even(2) {
        t.Log("All test cases after Fail will still run")
        t.Fail()
    }

    if Even(2) {
        t.Log("The test after Fail will still run")
        t.Fail()
    }
}

func TestAfterFailCase(t *testing.T) {
    if Even(2) {
        t.Log("This test case after Fail will still run")
        t.Fail()
    }
}

執(zhí)行測試后,TestTestingFail中的第二部分也可以繼續(xù)執(zhí)行。

--- FAIL: TestTestingFail (0.00s)
? ? even_fail_test.go:8: All test cases after Fail will still run
? ? even_fail_test.go:13: The test after Fail will still run
--- FAIL: TestAfterFailCase (0.00s)
? ? even_fail_test.go:20: This test case after Fail will still run
FAIL
exit status 1
FAIL?? ?_/root/workspace/go/test_unittest?? ?0.004s

示例三:FailNow函數(shù)

func (t *T) FailNow() 讓測試失敗,同一個測試用例中的測試不再執(zhí)行,后續(xù)的測試也會繼續(xù)執(zhí)行

package even

import "testing"

func TestTestingFailNow(t *testing.T) {
    // Let create a fake case, we will call FailNow
    if Even(2) {
        t.Log("All test cases after FailNow will not run")
        t.FailNow()
    }

    if Even(2) {
        t.Log("The test after FailNow will be skipped")
        t.FailNow()
    }
}

func TestAfterFailNowCase(t *testing.T) {
    if Even(2) {
        t.Log("This test case after FailNow will still run")
        t.FailNow()
    }
}

執(zhí)行后TestTestingFailNow中的第二段測試不再執(zhí)行,而后面的TestAfterFailNowCase繼續(xù)執(zhí)行

--- FAIL: TestTestingFailNow (0.00s)
? ? even_failnow_test.go:8: All test cases after FailNow will not run
--- FAIL: TestAfterFailNowCase (0.00s)
? ? even_failnow_test.go:20: This test case after FailNow will still run
FAIL
exit status 1
FAIL?? ?_/root/workspace/go/test_unittest?? ?0.003s

實(shí)例四:Log和Fetal函數(shù)

func (t *T) Log(args …interface{}) 使用默認(rèn)格式記錄日志,等同于Print(),記錄錯誤日志

func (t *T) Fatal(args …interface{}) 與Log功能相似,但是輸出日志后會調(diào)用FailNow

package even

import "testing"

func TestTestingFatal(t *testing.T) {
    // Let create a fake case, we will call FailNow
    if Even(2) {
        t.Fatal("All test cases after FailNow will not run")
    }

    if Even(2) {
        t.Fatal("The test after Fatal will not run")
    }
}

func TestAfterFatalCase(t *testing.T) {
    if Even(2) {
        t.Fatal("This test case after Fatal will still run")
    }
}

Fatal的執(zhí)行過程與FailNow相似

--- FAIL: TestTestingFatal (0.00s)
? ? even_fatal_test.go:8: All test cases after FailNow will not run
--- FAIL: TestAfterFatalCase (0.00s)
? ? even_fatal_test.go:18: This test case after Fatal will still run
FAIL
exit status 1
FAIL?? ?_/root/workspace/go/test_unittest?? ?0.005s

原文鏈接:https://blog.csdn.net/xiaoquqi/article/details/125816871

欄目分類
最近更新