美文网首页
Golang单测

Golang单测

作者: HueyYao | 来源:发表于2022-03-02 23:23 被阅读0次

表格驱动测试:

tests := []struct{
    a, b, c int32
}{
    {1, 2, 3},
    {0, 2, 2},
    {1, 3, 4},
    {math.MaxInt32, 1, math.MinInt32},
}
for _, test := range tests {
    if actual := add(test.a, test.b); actual!= test.c{
        //todo
    }
}
.......

写一个简单的测试用例

需要被测试得代码:

package basic

import (
    "fmt"
    "math"
)
func tryTriangle(a, b int) int{
    var c int
    c = int(math.Sqrt(float64(a*a + b*b)))
    return c
}

func triangle(){
    var a, b int = 3, 4
    fmt.Println(tryTriangle(a, b))
}

测试代码:

package basic

import "testing"

func TestTriangle(t *testing.T){
    tests := []struct{a, b, c int} {
        {3, 4, 5},
        {5, 12, 13},
        {8, 15, 17},
    }
    for _, tt := range tests {
        if actual := tryTriangle(tt.a, tt.b); actual != tt.c {
            t.Errorf("tryTriangle(%d, %d);" + "got %d no %d", tt.a, tt.b, actual, tt.c)
        }
    }
}

验证代码覆盖率

image-20220302224959264.png

测算运行速度

image-20220302225806393.png

如何通过命令进行单侧:

image-20220302225114308.png

通过 go tool cover -html=c.out 来查看覆盖率

测试程序耗时位置:

// 生成性能数据文件
go test -bench . -cpuprofile cpu.out
//打开cpu.out文件 查看性能数据
go tool pprof cpu.out
    web

相关文章

  • Golang单测

    表格驱动测试: 写一个简单的测试用例 需要被测试得代码: 测试代码: 验证代码覆盖率 测算运行速度 如何通过命令进...

  • golang写单测

    要测试的函数,利用Goland可以自动生成对应的测试文件和单测方法 我们只需在todo那里写上测试用例就好

  • golang单元测试

    简介 golang单测,有一些约定,例如文件名是xxx.go,那么对应的测试文件就是xxx_test.go,单测的...

  • golang mock interface 写单测

    1 使用github.com/vektra/mockery https://github.com/vektra/m...

  • {C#-05C} 单测.复合方法

    背景 合成多个子方法时传参多,责任不清时难单测 代码例 环境类 环境类单测 子函数 子函数单测 复合函数 复合函数单测

  • Android studio里单测覆盖率报告生成

    在Android studio里,单测分为本地单测和Android单测两种,区别在于是否需要使用dvm虚拟机,前者...

  • 徒手撸一个Mock框架(一)——如何创建一个mock对象

    自从老夫换了一个新厂之后,单测就写个不停,因为新厂对单测的要求还是比较高的。 在撸单测的过程中,用Mockito,...

  • iOS-单元测试

    单测在比较大的项目中会使用到,下面的几个东西是单测常用的工具,供大家参考。LCOV - 单测覆盖率报告生成工具;O...

  • curl 命令行 文件上传

    本案例使用golang版本 golang后端实现代码上传 curl 命令行模拟 单文件上传 返回结果 '637be...

  • 实验的变量与设计(四)

    (四)真实验设计 1单因素完全随机两等组设计 (1)单因素完全随机等组前测后测设计 统计方法1: 两组前测后测差值...

网友评论

      本文标题:Golang单测

      本文链接:https://www.haomeiwen.com/subject/krzerrtx.html