import "testing" func TestAddition(t *testing.T) { result := addition(2, 3) if result != 5 { t.Errorf("Expected 5, but got %d", result) } } func addition(a, b int) int { return a + b }
import "testing" func TestEvenNumbers(t *testing.T) { input := []int{1, 2, 3, 4, 5, 6} expected := []int{2, 4, 6} result := EvenNumbers(input) if !reflect.DeepEqual(result, expected) { t.Errorf("Expected %v, but got %v", expected, result) } } func EvenNumbers(numbers []int) []int { var evens []int for _, n := range numbers { if n%2 == 0 { evens = append(evens, n) } } return evens }In this example, we're testing a function `EvenNumbers` that takes a slice of integers and returns a slice of only the even numbers. We're using the `reflect.DeepEqual` method to compare the expected and actual results. The `reflect` package is a standard library package in Go that allows us to perform various introspection operations like type checking, comparison, and manipulation. Overall, the `testing/TB` Go package provides a useful set of methods and interfaces for writing unit tests in Go. When coupled with other standard and third-party libraries, it can help create robust and reliable test suites for Go applications.