The purpose of this tutorial is to teach beginners how to use the testing package in Go for assertions. Assertions are statements in code that assert or state that something must be true. In Go, we use the testing package to write and run tests. These tests can include assertions to verify the program's correctness.
During the tutorial, you will learn:
- About the Go testing package
- How to write and run tests
- How to use assertions in tests
Prerequisites: Basic knowledge of Go programming. If you are a complete beginner, you might want to have a look at Go's official tutorial first.
In Go, a test is just a function with a name starting with Test
followed by a word or phrase starting with a capital letter. It takes one argument, a pointer to testing.T. If the function calls a failure function such as t.Error or t.Fail, the test is considered failed.
func TestAdd(t *testing.T) {
result := add(2, 2)
if result != 4 {
t.Fail()
}
}
To run tests in Go, you use the go test
command followed by the package name.
go test mypackage
The testing package does not have built-in assertion capabilities. For this, we often use a third-party package like github.com/stretchr/testify/assert
. Let's add it to our project.
go get github.com/stretchr/testify/assert
Now, we can use the assert
package to make assertions in our tests.
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestAdd(t *testing.T) {
result := add(2, 2)
assert.Equal(t, 4, result, "they should be equal")
}
Let's look at a practical example.
package main
import (
"testing"
"github.com/stretchr/testify/assert"
)
func add(a int, b int) int {
return a + b
}
func TestAdd(t *testing.T) {
result := add(2, 2)
assert.Equal(t, 4, result, "they should be equal")
}
Here we have a function add
that takes two integers and returns their sum. The TestAdd
function tests whether the add
function works correctly. assert.Equal
checks if the result equals 4.
In this tutorial, you've learned how to use the testing package in Go and how to use the testify package to add assertions to your tests. Now you can write tests for your functions and verify that they are working as expected.
Next steps: Learn more about test-driven development (TDD) and how it can improve your code. Also, learn about other types of tests (like benchmark tests and table-driven tests) in Go.
Additional resources:
- Go's official testing documentation
- Testify's GitHub page
Solutions:
func subtract(a int, b int) int {
return a - b
}
func TestSubtract(t *testing.T) {
result := subtract(5, 3)
assert.Equal(t, 2, result, "they should be equal")
}
func concatenate(a string, b string) string {
return a + b
}
func TestConcatenate(t *testing.T) {
result := concatenate("Hello, ", "World!")
assert.Equal(t, "Hello, World!", result, "they should be equal")
}
func divide(a float64, b float64) (float64, error) {
if b == 0 {
return 0, errors.New("Cannot divide by zero")
}
return a / b, nil
}
func TestDivide(t *testing.T) {
result, err := divide(10.0, 2.0)
assert.NoError(t, err)
assert.Equal(t, 5.0, result, "they should be equal")
}
Tips for further practice: Try to write tests for more complex functions. Also, learn about mocking to be able to write tests for functions that depend on other functions.