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测试单元测试

单元测试某个函数

基准测试

基准测试某个函数

相关推荐
侃侃_天下6 小时前
最终的信号类
开发语言·c++·算法
echoarts6 小时前
Rayon Rust中的数据并行库入门教程
开发语言·其他·算法·rust
Aomnitrix6 小时前
知识管理新范式——cpolar+Wiki.js打造企业级分布式知识库
开发语言·javascript·分布式
每天回答3个问题7 小时前
UE5C++编译遇到MSB3073
开发语言·c++·ue5
伍哥的传说7 小时前
Vite Plugin PWA – 零配置构建现代渐进式Web应用
开发语言·前端·javascript·web app·pwa·service worker·workbox
小莞尔7 小时前
【51单片机】【protues仿真】 基于51单片机八路抢答器系统
c语言·开发语言·单片机·嵌入式硬件·51单片机
我是菜鸟0713号8 小时前
Qt 中 OPC UA 通讯实战
开发语言·qt
JCBP_8 小时前
QT(4)
开发语言·汇编·c++·qt·算法
Brookty8 小时前
【JavaEE】线程安全-内存可见性、指令全排序
java·开发语言·后端·java-ee·线程安全·内存可见性·指令重排序
百锦再8 小时前
[特殊字符] Python在CentOS系统执行深度指南
开发语言·python·plotly·django·centos·virtualenv·pygame