Ready. Test. Go. - Go Testing for everyone.

Tests might save you by a brink!


3 min read

Welcome aboard, fellow Gopher! Whether you're embarking on your Go journey or looking to solidify your testing skills, you've come to the right place. In this post, we'll explore testing in Golang, a language that embraces simplicity and verbosity. So, fasten your seatbelt, and let's dive in.

Rather than drowning developers in a myriad of testing frameworks, Go provides the absolute base, allowing developers to construct on top of it.

The Bare Essential: testing.T

At the heart of Golang testing, lies the testing package, and the indispensable testing.T type. This combination is your trusty sidekick when it comes to writing tests.

To illustrate, let's consider a simple example. Imagine a utility function that performs division, and now, we want to put it to the test.

package utilities

import (

func divide(a, b int) (int, error) {
    if b == 0 {
        return 0, errors.New("division by zero. method not allowed")

    return a / b, nil

Testing this atomic function is as simple as given below. This is not a tutorial stripped-down version. This is the idiomatic way to write tests. Simple right?

package utilities_test

import (

func TestDivide(t *testing.T) {
    // Test A: Valid division
    result, err := divide(10, 2)
    if err != nil {
        t.Errorf("Unexpected error: %v", err)
    if result != 5 {
        t.Errorf("Expected result to be 5, got %v", result)

    // Test B: Division by zero is notified
    result, err = divide(10, 0)
    if err == nil {
        t.Error("Expected an error for division by zero, but got none")
    if result != 0 {
        t.Errorf("Expected result to be 0, got %v", result)
    expectedErrorMsg := "division by zero. method not allowed"
    if err.Error() != expectedErrorMsg {
        t.Errorf("Expected error message '%v', got '%v'", expectedErrorMsg, err.Error())

Here, testing in Go doesn't involve convoluted patterns or arcane conventions. It's just regular programming but with a sprinkle of t.Fatal() and the like to indicate the test runner test has failed.

You can read the exhaustive list here.

Running the tests

In the Golang realm, running tests is a breeze. The go binary has a trusty companion, test. Execute your tests effortlessly with go test, and watch the magic unfold.

  • All files with names containing the test are executed.

  • Test files can access private methods using *_test as the package name, allowing you to test even the most clandestine functions.

What's Next?

This is the absolute bare bones you need to get started with golang testing. However, it may occur to you that things are WET, that is repetitive. You need to know Table-driven testing. Fortunately, I already wrote about it here.

Read the newsletter to know about table driven test

"Testing is an infinite process of comparing the invisible to the ambiguous in order to avoid the unthinkable happening to the anonymous." - James Bach


Thank you for exploring the world of Go testing with me! I hope this comprehensive guide has enriched your understanding of testing strategies in Go.

Your support is crucial in fueling our mission, and by subscribing to my newsletter(it's free btw), you are encouraging me to continue creating quality educational content.

Subscribe now, stay ahead in your Go development journey, and let's code for a brighter future together! ๐Ÿš€โœจ