go单元测试和基准测试

1、单元测试和基准测试

单元测试和基准测试代码开发中的重要环节,良好的单元测试和基准测试,能提升开发质量,对整体开发有非常重要的重要,下面介绍单元测试和基准测试的写法。

2、单元测试和基准测试写法

以排序基本排序算法,选择和插入为例介绍,整体代码目录如图所示

创建sort.go和对象sort_test.go

在sort.go中增加函数InsertSort和SelectSort

go 复制代码
package main

func SeletSort(a []int) {
	for i := 0; i < len(a)-1; i++ {
		for j := i + 1; j < len(a); j++ {
			if a[j] < a[i] {
				a[j], a[i] = a[i], a[j]
			}
		}
	}
}

func InsertSort(a []int) {
	for j := 1; j < len(a); j++ {
		for i := j; i > 0 && a[i] < a[i-1]; i-- {
			a[i], a[i-1] = a[i-1], a[i]
		}
	}

}

单元测试需要以Test为前缀+待测试函数,在sort_test.go增加TestSelectSort函数:

go 复制代码
func TestSelectSort(t *testing.T) {
	testCases := []struct {
		input    []int
		expected []int
	}{
		{
			input:    []int{8, 12, 3, 1, 4, 5},
			expected: []int{1, 3, 4, 5, 8, 12},
		},
		{
			input:    []int{8, 13, 0, 9, 8, 7, 6},
			expected: []int{0, 6, 7, 8, 8, 9, 13},
		},
		{
			input:    []int{10, 9, 8, 7, 6, 5, 4},
			expected: []int{4, 5, 6, 7, 8, 9, 10},
		},
		{
			input:    []int{1, 2, 3, 6, 5, 4},
			expected: []int{1, 2, 3, 4, 5, 6},
		},
		{
			input:    []int{7, 7, 7, 9, 9, 9, 9, 6, 6, 6},
			expected: []int{6, 6, 6, 7, 7, 7, 9, 9, 9, 9},
		},
	}

	for i := 0; i < len(testCases); i++ {
		SeletSort(testCases[i].input)

		if !compareSlice(testCases[i].input, testCases[i].expected) {
			t.Errorf("Test case %d: Expected slice %v but received error %v", i+1, testCases[i].expected, testCases[i].input)
		}
	}

}

基准测试以Benchmark+待测函数,分别添加BenchmarkSelectSort和BenchmarkInsertSort

go 复制代码
func BenchmarkInsertSort(b *testing.B) {
	for i := 0; i < b.N; i++ {
		testCases := []struct {
			input    []int
			expected []int
		}{
			{
				input:    []int{8, 12, 3, 1, 4, 5},
				expected: []int{1, 3, 4, 5, 8, 12},
			},
			{
				input:    []int{8, 13, 0, 9, 8, 7, 6},
				expected: []int{0, 6, 7, 8, 8, 9, 13},
			},
			{
				input:    []int{10, 9, 8, 7, 6, 5, 4},
				expected: []int{4, 5, 6, 7, 8, 9, 10},
			},
			{
				input:    []int{1, 2, 3, 6, 5, 4},
				expected: []int{1, 2, 3, 4, 5, 6},
			},
			{
				input:    []int{7, 7, 7, 9, 9, 9, 9, 6, 6, 6},
				expected: []int{6, 6, 6, 7, 7, 7, 9, 9, 9, 9},
			},
		}

		for i := 0; i < len(testCases); i++ {
			InsertSort(testCases[i].input)
		}
	}
}

func BenchmarkSelectSort(b *testing.B) {
	for i := 0; i < b.N; i++ {
		testCases := []struct {
			input    []int
			expected []int
		}{
			{
				input:    []int{8, 12, 3, 1, 4, 5},
				expected: []int{1, 3, 4, 5, 8, 12},
			},
			{
				input:    []int{8, 13, 0, 9, 8, 7, 6},
				expected: []int{0, 6, 7, 8, 8, 9, 13},
			},
			{
				input:    []int{10, 9, 8, 7, 6, 5, 4},
				expected: []int{4, 5, 6, 7, 8, 9, 10},
			},
			{
				input:    []int{1, 2, 3, 6, 5, 4},
				expected: []int{1, 2, 3, 4, 5, 6},
			},
			{
				input:    []int{7, 7, 7, 9, 9, 9, 9, 6, 6, 6},
				expected: []int{6, 6, 6, 7, 7, 7, 9, 9, 9, 9},
			},
		}

		for i := 0; i < len(testCases); i++ {
			SeletSort(testCases[i].input)
		}
	}
}

3.测试

go test 或者go test -v测试单元测试

单元测试某个函数

基准测试

基准测试某个函数

相关推荐
小哈里19 分钟前
【pypi镜像源】使用devpi实现python镜像源代理(缓存加速,私有仓库,版本控制)
开发语言·python·缓存·镜像源·pypi
努力学习的小廉23 分钟前
【C++】 —— 笔试刷题day_29
开发语言·c++·算法
电商数据girl32 分钟前
酒店旅游类数据采集API接口之携程数据获取地方美食品列表 获取地方美餐馆列表 景点评论
java·大数据·开发语言·python·json·旅游
天天打码32 分钟前
python版本管理工具-pyenv轻松切换多个Python版本
开发语言·python
CircleMouse32 分钟前
基于 RedisTemplate 的分页缓存设计
java·开发语言·后端·spring·缓存
ktkiko1139 分钟前
顶层架构 - 消息集群推送方案
java·开发语言·架构
楠奕39 分钟前
python中使用neo4j
开发语言·python·neo4j
南斯拉夫的铁托1 小时前
labelimg安装及使用指南(yolo)
开发语言·python·yolo
六bring个六1 小时前
文件系统交互实现
开发语言·c++·qt·交互
jingyu飞鸟1 小时前
Centos7系统(最小化安装)安装zabbix7版本详细文章、nginx源代码配置、php源代码、mysql-yum安装
开发语言·php