Golang unit testing introduction — PART 1

Sayed Khaidir Ali
3 min readMar 8, 2023

--

Source: https://blog.autify.com/what-is-unit-testing

Unit testing is an essential part of software development, and Go developers have access to a powerful built-in testing framework that makes writing and running unit tests a breeze. In this article, we’ll explore the basics of unit testing in Go and provide some tips for writing effective unit tests.

What is unit testing?

Unit testing is a software testing technique that involves writing automated tests for small, isolated units of code. These tests are designed to verify that the unit of code being tested behaves as expected in different scenarios, and to catch any errors or bugs before they cause problems in the larger system.

In Go, unit testing is done using the “testing” package, which provides a simple and flexible framework for writing and running tests. Each test is defined as a function that starts with the word “Test” and takes a single argument of type “testing.T”. The “testing.T” argument provides methods for reporting test failures and logging information.

Writing unit tests in Go

To write a unit test in Go, first create a new file with a “_test” suffix in the same package as the code you want to test. For example, if you have a file called “calculator.go” in a package called “math”, you would create a new file called “calculator_test.go” in the same directory.

Next, define your test function using the “Test” prefix, followed by a descriptive name that reflects what the test is checking. For example:

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

In this example, we’re testing a function called “Add” that takes two integers and returns their sum. We call the function with arguments 2 and 3 and then check that the result is 5 using the “if” statement. If the result is not 5, we use the “t.Errorf” method to report a test failure with a message that includes the expected and actual results.

Running unit tests in Go

To run unit tests in Go, use the “go test” command in the terminal. By default, this command runs all the tests in the current directory and any subdirectories that end with “_test.go”. For example, to run the tests in our “calculator_test.go” file, we would run:

$ go test

The output of the “go test” command will show the results of each test, including any failures or errors. For example:

$ go test
PASS
ok math 0.004s

Tips for effective unit testing in Go

Here are some tips for writing effective unit tests in Go:

  1. Write tests early and often: Unit tests should be written as soon as possible during the development process, ideally before the code being tested is written. This ensures that the tests cover all the necessary scenarios and catch any errors early on.
  2. Test all code paths: Make sure your tests cover all possible code paths, including error handling and edge cases.
  3. Keep tests independent: Each test should be independent of the others and should not rely on any global state. This makes it easier to debug and maintain tests.
  4. Use table-driven tests: Table-driven tests are a powerful technique for testing functions that take a range of inputs and produce a range of outputs. Instead of writing multiple test functions, you can define a table of inputs and expected outputs and loop through them in a single test function.
  5. Keep tests readable: Write tests that are easy to read and understand, using descriptive names and clear code. This makes it easier for other developers to understand what

To be continue…

--

--

Sayed Khaidir Ali

Software Engineer at Shopee, start to make a change like a bit of processor