Unit Testing in Go: 3 Practical Examples
Introduction to Unit Testing in Go
Unit testing is a crucial practice in software development that ensures individual components of a program work as intended. In Go, unit tests are typically written in the same package as the code they test, making it easy to structure and maintain your tests. Go’s built-in testing package provides a straightforward way to create and execute tests. Below are three diverse examples of unit testing in Go, each demonstrating different aspects of this essential practice.
Example 1: Testing a Simple Function
Context
This example demonstrates how to unit test a simple function that adds two integers. It serves as a foundational illustration of unit testing in Go.
package mathutil
import "testing"
// Add function sums two integers.
func Add(a int, b int) int {
return a + b
}
// TestAdd tests the Add function.
func TestAdd(t *testing.T) {
tests := []struct {
a, b, expected int
}{
{1, 2, 3},
{0, 0, 0},
{-1, 1, 0},
}
for _, tt := range tests {
t.Run(fmt.Sprintf("%d+%d", tt.a, tt.b), func(t *testing.T) {
result := Add(tt.a, tt.b)
if result != tt.expected {
t.Errorf("Add(%d, %d) = %d; expected %d", tt.a, tt.b, result, tt.expected)
}
})
}
}
Notes
- The use of table-driven tests allows for easy addition of new test cases.
- You can run this test using the command:
go test.
Example 2: Testing a Struct Method
Context
In this example, we will unit test a method of a struct that calculates the area of a rectangle. This illustrates how to test methods associated with structs in Go.
package geometry
import "testing"
// Rectangle struct represents a rectangle.
type Rectangle struct {
Width float64
Height float64
}
// Area method calculates the area of the rectangle.
func (r Rectangle) Area() float64 {
return r.Width * r.Height
}
// TestArea tests the Area method of Rectangle.
func TestArea(t *testing.T) {
rect := Rectangle{Width: 5, Height: 10}
expected := 50.0
result := rect.Area()
if result != expected {
t.Errorf("Area() = %f; expected %f", result, expected)
}
}
Notes
- This example highlights object-oriented programming in Go with a struct and its method.
- Ensure to check edge cases, such as rectangles with zero dimensions.
Example 3: Testing Error Handling
Context
This example focuses on unit testing a function that divides two numbers, including handling division by zero. It shows how to test for expected errors.
package mathutil
import (
"errors"
"testing"
)
// Divide function performs division and handles errors.
func Divide(a float64, b float64) (float64, error) {
if b == 0 {
return 0, errors.New("cannot divide by zero")
}
return a / b, nil
}
// TestDivide tests the Divide function for normal and error cases.
func TestDivide(t *testing.T) {
tests := []struct {
a, b float64
expected float64
err bool
}{
{10, 2, 5, false},
{10, 0, 0, true},
}
for _, tt := range tests {
result, err := Divide(tt.a, tt.b)
if (err != nil) != tt.err {
t.Errorf("Divide(%f, %f) error = %v; expected error? %v", tt.a, tt.b, err, tt.err)
}
if result != tt.expected {
t.Errorf("Divide(%f, %f) = %f; expected %f", tt.a, tt.b, result, tt.expected)
}
}
}
Notes
- This example showcases how to test both successful and erroneous function outputs.
- Always ensure that edge cases, such as division by zero, are thoroughly tested.
By incorporating these examples of unit testing in Go into your programming toolkit, you will enhance your ability to write reliable and maintainable code.
Related Topics
Explore More Go Code Snippets
Discover more examples and insights in this category.
View All Go Code Snippets