Go (Golang) / Testing and Debugging in Go

Getting Started with Unit Testing in Go

This tutorial will introduce you to the basics of writing and running unit tests in Go. You'll learn how to test various components of your Go code using the 'testing' package.

Tutorial 2 of 5 5 resources in this section

Section overview

5 resources

Teaches unit testing, benchmarking, and debugging techniques in Go.

Getting Started with Unit Testing in Go

1. Introduction

In this tutorial, we aim to familiarize you with the basics of writing and running unit tests in Go. Unit testing is a critical aspect of software development that ensures the correct functioning of individual pieces of code (units). It plays a pivotal role in maintaining code quality and refactoring.

You will learn:
- The basic concepts of unit testing.
- How to write and run unit tests in Go.
- Using the testing package in Go.

Prerequisites:
- Basic understanding of Go programming language.
- Go environment installed on your computer.

2. Step-by-Step Guide

Go has a built-in testing tool called testing. This package provides support for automated testing of Go packages.

Creating a Test File:
Every test file in Go should have _test appended to the original file name and should be in the same package as the original file.

Writing a Test Function:
Test functions should start with the word Test followed by a word or phrase that starts with a capital letter.

Running a Test:
You can run the test file using go test command in the terminal.

Here is a simple example of a unit test in Go:

package main

import (
    "testing"
)

func TestAdd(t *testing.T) {
    sum := Add(2, 3)
    if sum != 5 {
        t.Errorf("Expected 5, but got %d", sum)
    }
}

In this code, we have a test function TestAdd which tests the function Add.

3. Code Examples

Here are some practical examples:

Example 1: Function Test

package main

import (
    "testing"
)

func Add(x, y int) int {
    return x + y
}

func TestAdd(t *testing.T) {
    sum := Add(2, 3)
    if sum != 5 {
        t.Errorf("Expected 5, but got %d", sum)
    }
}

In this code, we are testing the Add function. The expected output is 5. If the function returns anything other than 5, the test fails.

Example 2: Table-Driven Test

package main

import (
    "testing"
)

func Add(x, y int) int {
    return x + y
}

func TestTableAdd(t *testing.T) {
    var tests = []struct {
        input1   int
        input2   int
        expected int
    }{
        {2, 3, 5},
        {-1, 1, 0},
        {100, 200, 300},
    }

    for _, test := range tests {
        if output := Add(test.input1, test.input2); output != test.expected {
            t.Error("Test Failed: {} inputted, {} expected, recieved: {}", test.input1, test.input2, test.expected, output)
        }
    }
}

In this code, we are performing table-driven testing. It's a way to build structured test cases in Go.

4. Summary

We covered the basics of unit testing in Go. We learned how to write and run tests using the testing package. We also explored table-driven testing.

For further learning, familiarize yourself with more advanced features of the testing package such as benchmarks and test suites.

5. Practice Exercises

Exercise 1: Write a test for a function that multiplies two numbers.

Solution:

package main

import (
    "testing"
)

func Multiply(x, y int) int {
    return x * y
}

func TestMultiply(t *testing.T) {
    product := Multiply(2, 3)
    if product != 6 {
        t.Errorf("Expected 6, but got %d", product)
    }
}

Exercise 2: Write a table-driven test for a function that returns the maximum of two numbers.

Solution:

package main

import (
    "testing"
)

func Max(x, y int) int {
    if x > y {
        return x
    }
    return y
}

func TestTableMax(t *testing.T) {
    var tests = []struct {
        input1   int
        input2   int
        expected int
    }{
        {2, 3, 3},
        {-1, 1, 1},
        {100, 200, 200},
    }

    for _, test := range tests {
        if output := Max(test.input1, test.input2); output != test.expected {
            t.Error("Test Failed: {} inputted, {} expected, recieved: {}", test.input1, test.input2, test.expected, output)
        }
    }
}

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

URL Encoder/Decoder

Encode or decode URLs easily for web applications.

Use tool

Random Name Generator

Generate realistic names with customizable options.

Use tool

AES Encryption/Decryption

Encrypt and decrypt text using AES encryption.

Use tool

Keyword Density Checker

Analyze keyword density for SEO optimization.

Use tool

Random Password Generator

Create secure, complex passwords with custom length and character options.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help