go语言学习---用例单测写的函数的方法

 

 

divison.go

package mytest

import "errors"

func Division(a, b float64) (float64, error)  {
    if b == 0{
        return 0, errors.New("除数不能为0")
    }
    return a / b, nil
}

divison_test.go

package mytest

import "testing"

func Test_Division_1(t *testing.T)  {
    if i, e := Division(6, 2); i != 3 || e!= nil{
        t.Error("除法函数没有测试通过")
    }else{
        t.Log("第一个测试通过了")
    }
}

func Test_Division_2(t *testing.T)  {
    if _, e := Division(6, 0); e == nil {
        t.Error("Division did not work as expected")
    }else{
        t.Log("one test is passed", e)
    }
}

 

posted @ 2020-12-01 20:39  威威后花园  阅读(80)  评论(0编辑  收藏  举报