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.
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
.
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.
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.
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)
}
}
}