Corporate Training
Request Demo
Click me
Menu
Let's Talk
Request Demo

Tutorials

Unit Testing in Go

22. Unit Testing in Go

Unit testing is an essential part of software development that ensures the correctness of individual units of code. In Go, the built-in testing framework makes it easy to write and run tests for your functions and packages. Let's explore unit testing in Go in detail, along with examples.

Writing Unit Tests in Go:

Unit tests in Go are written in separate files with a '_test'suffix in the package directory. Each test file should import the 'testing' package.

Example Unit Test:

Let's say you have a function named 'add' that you want to test:

// math.go

package math

func Add(a, b int) int {
return a + b
}

 

You can create a test file named 'math_test.go' in the same package directory:

// math_test.go

package math

import (
"testing"
)

func TestAdd(t *testing.T) {
result := Add(2, 3)
if result != 5 {
t.Errorf("Add(2, 3) = %d; want 5", result)
}
}

 

Running Tests:

To run tests, use the go 'test'command followed by the package name. Go's testing framework automatically discovers and runs tests in files with '_test' suffixes.

$ go test math

 

Table-Driven Tests:

Table-driven tests allow you to test multiple inputs and expected outputs with less duplication.

func TestAdd(t *testing.T) {
tests := []struct {
a, b, expected int
}{
{2, 3, 5},
{0, 0, 0},
{-1, 1, 0},
}

for _, test := range tests {
result := Add(test.a, test.b)
if result != test.expected {
t.Errorf("Add(%d, %d) = %d; want %d", test.a, test.b, result, test.expected)
}
}
}

 

Subtests:

Subtests allow you to group related tests within a test function and see individual test results.

func TestAdd(t *testing.T) {
t.Run("positive numbers", func(t *testing.T) {
result := Add(2, 3)
if result != 5 {
t.Errorf("Add(2, 3) = %d; want 5", result)
}
})

t.Run("negative numbers", func(t *testing.T) {
result := Add(-2, -3)
if result != -5 {
t.Errorf("Add(-2, -3) = %d; want -5", result)
}
})
}

 

Mocking Dependencies:

When testing functions that use external dependencies, you can use interfaces and dependency injection to mock those dependencies for testing.

Summary:

  • Unit tests in Go are written in files with '_test' suffixes in the same package directory.
  • The 'testing' package provides functions for writing tests and assertions.
  • Use the 'go test' command to run tests.
  • Table-driven tests allow testing multiple inputs and expected outputs.
  • Subtests help organize related tests and provide individual results.
  • Interfaces and dependency injection can be used to mock dependencies for testing.

Writing thorough unit tests helps you catch bugs early and maintain the reliability of your codebase. By utilizing Go's testing framework and best practices, you can ensure that your functions and packages behave as expected under various scenarios.