// Copyright 2024 The Cockroach Authors. // // Use of this software is governed by the Business Source License // included in the file licenses/BSL.txt. // // As of the Change Date specified in that file, in accordance with // the Business Source License, use of this software will be governed // by the Apache License, Version 2.0, included in the file // licenses/APL.txt. // // Package utils contains utility functions. package utils_test import ( "testing" "userapp/utils" ) // ✅ 表驱动测试:简洁清晰 func TestAdd(t *testing.T) { // 定义测试用例表 // 单测 一次使用,使用匿名结构体定义测试用例,适合简单的测试场景 tests := []struct { name string a int b int want int }{ {"正数相加", 1, 2, 3}, {"负数相加", -1, 1, 0}, {"零值相加", 0, 0, 0}, {"大数相加", 100, 200, 300}, } // 测试初始化和回收 // 遍历执行测试, 并发执行的 for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { got := utils.Add(tt.a, tt.b) if got != tt.want { t.Errorf("Add(%d, %d) = %d; want %d", tt.a, tt.b, got, tt.want) } }) } }