[Go] Unit testing

file: utils/math.go

package utils

import (
  "fmt"
)

func printNum(num int) {
    fmt.Println("Current number is: ", num)
}

// Add multi int number together return total int
func Add(nums ...int) int {
    total := 0
    for _, num := range nums {
        printNum(num)
        total += num
    }
    return total
}

 

Create a test file: utils/math_test.go

package utils

import "testing"

func TestAdd(t *testing.T) {
    expected := 4
    actual := Add(2,2)
    
    if actual != expected {
        t.Errorf("Add function does not correct, expected: %d, but get value: %d", expected, actual)
    }
}

 

Run go test

posted @ 2022-09-05 14:57  Zhentiw  阅读(10)  评论(0编辑  收藏  举报