DEV Community

Romulo Gatto
Romulo Gatto

Posted on

Testing in Go: Writing and Running Tests

Testing in Go: Writing and Running Tests

Welcome to the wonderful world of testing in Go! Testing is an essential part of software development that ensures your code behaves as expected. In this guide, we will explore how to write effective tests in Go and run them efficiently.

Setting Up Your Workspace

Before diving into writing tests, let's make sure you have the necessary tools set up on your machine:

  1. Go: Make sure you have Go installed on your system. You can verify by running go version in your terminal.
  2. IDE or Text Editor: Choose a comfortable IDE or text editor suitable for Go development, such as Visual Studio Code with the Go extension installed.

Once you have everything set up, it's time to start writing some tests!

Understanding the Test Structure

In Go, creating test files is straightforward. Simply create a new file ending with _test.go, which tells the compiler that these are testing files.

package mypackage_test

import (
    "testing"
)

func TestMyFunction(t *testing.T) {
    // Add test cases here
}
Enter fullscreen mode Exit fullscreen mode

In this example, we import the testing package and define a function named TestMyFunction. The function's name must start with the word "Test" followed by any descriptive name you choose.

Writing Effective Tests

Writing clear and concise test cases is crucial for reliable test suites. Here are some best practices to follow while writing tests:

1. Use Asserts:

Leverage built-in functions like t.Errorf() or panic-based ones like t.Fail() to signal failed assertions within your tests.

func TestAddition(t *testing.T) {
    result := add(2, 3)
    expected := 5

    if result != expected {
        t.Errorf("Expected %d, but got %d", expected, result)
    }
}
Enter fullscreen mode Exit fullscreen mode

2. Table-Driven Testing:

Use table-driven testing to run multiple test cases with different inputs and expected results efficiently. This approach avoids duplication and improves maintainability.

func TestIsEven(t *testing.T) {
    testCases := []struct {
        input    int
        expected bool
    }{
        {2, true},
        {7, false},
        {-4, true},
    }

    for _, tc := range testCases {
        result := isEven(tc.input)

        if result != tc.expected {
            t.Errorf("For input %d: Expected %t, but got %t", tc.input, tc.expected,result)
         }
     }
}
Enter fullscreen mode Exit fullscreen mode

3. Test Helper Functions:

Create helper functions to avoid code redundancy within your tests. These helper functions can be reused across multiple tests within the same package.

func assertEqual(t *testing.T, a interface{}, b interface{}) {
    if a != b {
            t.Errorf("Expected [%v] but got [%v]", b,a )
       }
}

// Usage:
assertEqual(t,result ,expected )

Enter fullscreen mode Exit fullscreen mode

Running Tests

Now that we have our tests in place let's execute them!

In the terminal navigate to the root directory of your Go project containing the test files and run the following command:

$ go test ./...
Enter fullscreen mode Exit fullscreen mode

The go test command recursively runs all tests inside packages starting from the current directory onward (./...).

Conclusion

Congratulations! You now have a solid understanding of how to write effective tests in Go using best practices such as asserts and table-driven testing. Remember that writing well-tested code leads to more reliable software.

As you continue your journey with Go, keep experimenting and exploring different ways to enhance your testing skills. Happy coding!

Top comments (0)