go单元测试和基准测试

news/2025/2/3 18:10:18/

1、单元测试和基准测试

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

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

以排序基本排序算法,选择和插入为例介绍,整体代码目录如图所示
创建sort.go和对象sort_test.go
在这里插入图片描述
在sort.go中增加函数InsertSort和SelectSort

package mainfunc 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函数:

func TestSelectSort(t *testing.T) {testCases := []struct {input    []intexpected []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

func BenchmarkInsertSort(b *testing.B) {for i := 0; i < b.N; i++ {testCases := []struct {input    []intexpected []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    []intexpected []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测试单元测试
在这里插入图片描述
单元测试某个函数
在这里插入图片描述
基准测试
在这里插入图片描述
基准测试某个函数
在这里插入图片描述


http://www.ppmy.cn/news/1569024.html

相关文章

LeetCode - #196 删除重复的电子邮件并保留最小 ID 的唯一电子邮件

网罗开发 &#xff08;小红书、快手、视频号同名&#xff09; 大家好&#xff0c;我是 展菲&#xff0c;目前在上市企业从事人工智能项目研发管理工作&#xff0c;平时热衷于分享各种编程领域的软硬技能知识以及前沿技术&#xff0c;包括iOS、前端、Harmony OS、Java、Python等…

基于SpringBoot电脑组装系统平台系统功能实现五

一、前言介绍&#xff1a; 1.1 项目摘要 随着科技的进步&#xff0c;计算机硬件技术日新月异&#xff0c;包括处理器&#xff08;CPU&#xff09;、主板、内存、显卡等关键部件的性能不断提升&#xff0c;为电脑组装提供了更多的选择和可能性。不同的硬件组合可以构建出不同类…

第 1 章 服务架构演进史

1.1 原始分布式时代 调用远程方法面临的问题与解决方案&#xff1a; 远程的服务在哪里——服务发现有多少个——负载均衡网络出现分区、超时或服务出错怎么办——熔断、隔离、降级方法的参数与返回结果如何表示——序列化协议信息如何传输——传输协议服务权限如何管理——认…

【使用Apache Flink 实现滑动窗口流式计算】

什么是Flink&#xff1f; Apache Flink是一个用于分布式流式处理和批处理的开源实时计算引擎。它具备低延迟、高吞吐量和 exactly-once 语义的特点&#xff0c;适用于各种实时数据处理场景。 Flink的核心概念 作业&#xff08;Job&#xff09;&#xff1a;Flink程序的执行单…

四、jQuery笔记

(一)jQuery概述 jQuery本身是js的一个轻量级的库,封装了一个对象jQuery,jquery的所有语法都在jQuery对象中 浏览器不认识jquery,只渲染html、css和js代码,需要先导入jQuery文件,官网下载即可 jQuery中文说明文档:https://hemin.cn/jq/ (二)jQuery要点 1、jQuery对象 …

Josephus Problem II CSES - 2163

有3种方法 Solution 1 - ordered_set Utilizing the ordered_set This data structure is an extension of the general set in C. It allows searching for the K-th smallest element in O(log n) time complexity. #include <iostream> using namespace std; #…

【apt源】RK3588 平台ubuntu20.04更换apt源

RK3588芯片使用的是aarch64架构&#xff0c;因此在Ubuntu 20.04上更换apt源时需要使用针对aarch64架构的源地址。以下是针对RK3588芯片在Ubuntu 20.04上更换apt源到清华源的正确步骤&#xff1a; 步骤一&#xff1a;打开终端 在Ubuntu 20.04中&#xff0c;按下Ctrl Alt T打…

智能小区物业管理系统推动数字化转型与提升用户居住体验

内容概要 在当今快速发展的社会中&#xff0c;智能小区物业管理系统的出现正在改变传统的物业管理方式。这种系统不仅仅是一种工具&#xff0c;更是一种推动数字化转型的重要力量。它通过高效的技术手段&#xff0c;将物业管理与用户居住体验紧密结合&#xff0c;无疑为社区带…