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

单元测试某个函数

基准测试

基准测试某个函数

相关推荐
若行若冲13 分钟前
Idea中 lombok 在“测试类中-单元测试”运行失败及解决方法
单元测试·log4j·maven·intellij-idea·lombok
鱼鱼说测试1 小时前
postman接口自动化测试
开发语言·lua
從南走到北1 小时前
JAVA国际版东郊到家同城按摩服务美容美发私教到店服务系统源码支持Android+IOS+H5
android·java·开发语言·ios·微信·微信小程序·小程序
_不会dp不改名_1 小时前
C++ 20: Concepts 与Requires
开发语言·c++20
韭菜钟2 小时前
Qt从qmake迁移到cmake的记录
开发语言·qt
少陵野小Tommy2 小时前
Python能用古诗词数据库做什么7:根据标题、诗句查找诗歌
开发语言·数据库·python
长城20242 小时前
PHP如何使用JpGraph生成3D饼形图?
开发语言·php·jpgraph·3d饼形图
우리帅杰4 小时前
【golang】ORM框架操作数据库
golang
秦禹辰4 小时前
本地Docker部署开源Web相册图库Piwigo与在线远程访问实战方案
开发语言·后端·golang
the beard4 小时前
深入理解Java多线程:状态、安全、同步与通信
java·开发语言