How to Use assert in Go
This tutorial demonstrates the use of assert in GoLang.
Use assert
in GoLang
GoLang doesn’t provide any built-in support for assert
, but we can use a widely used third-party package assert
from the Testify API. The assert
package is used to test GoLang code, just like the assert
method of other languages.
The assert
package provides comprehensive tools for testing Go systems. It provides many methods for different testing types; you can find the info about assert
methods here.
Before using the assert
package, we must download and load it. Follow the steps below to get the assert
package:
-
Open the command prompt as Administrator.
-
Go to the directory of your Go project.
-
Make sure there is no
go.mod
file. If the directory contains ago.mod
file, delete it and then go to the next step. -
Now initialize a
go.mod
file for theassert
package and run the following command:go mod init github.com/stretchr/testify/assert
-
Now download and install the
assert
package. Run the following command:go get github.com/stretchr/testify/assert
-
The above code will pull the
assert
package, but it may also need other dependencies. Run the following command to download all other dependencies:go mod download
Once the package is downloaded and installed, we can run the tests. Let’s try an example:
package main
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestAssert(t *testing.T) {
assert := assert.New(t)
var Demo1 string = "Hello, This is delftstack.com!"
var Demo2 string = "Hello, This is delftstack.com!"
assert.Equal(Demo1, Demo2, "Both strings should be equal")
}
The code above will test if the two strings are equal or not. If the strings are equal, the test will be passed; otherwise, failed.
See the output:
=== RUN TestAssert
--- PASS: TestAssert (0.00s)
PASS
The above output shows the test is passed. But what if the test failed?
Let’s try an example in which the assert test will fail:
package main
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestAssert(t *testing.T) {
assert := assert.New(t)
var Demo1 string = "Hello, This is delftstack.com!"
var Demo2 string = "Hello, This is delftstack"
assert.Equal(Demo1, Demo2, "Both strings should be equal")
}
As we can see, both strings are not equal now, so the test should fail. See the output:
RUN TestAssert
prog.go:15:
Error Trace: /prog.go:15
Error: Not equal:
expected: "Hello, This is delftstack.com!"
actual : "Hello, This is delftstack"
Diff:
--- Expected
+++ Actual
@@ -1 +1 @@
-Hello, This is delftstack.com!
+Hello, This is delftstack
Test: TestAssert
Messages: Both strings should be equal
--- FAIL: TestAssert (0.00s)
FAIL
Program exited.
As we can see, the test failed now, and the program shows the complete information about the test. There are many methods like equal
, which can be used with assert
to run different tests.
Let’s try examples for the methods assert.Nil
and assert.NotNil
:
package main
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestAssert(t *testing.T) {
var demo = []string{
"delftstack.123@hotmail.com",
"Delftstack <demo@delftstack.com>",
"demo@delftstack.tv",
"demohotmail.com",
"demo@"}
// assert for nil
assert.Nil(t, demo)
}
The code above will check for the given object of value- if it is nil
. See the output:
=== RUN TestAssert
prog.go:19:
Error Trace: /prog.go:19
Error: Expected nil, but got: []string{"delftstack.123@hotmail.com", "Delftstack <demo@delftstack.com>", "demo@delftstack.tv", "demohotmail.com", "demo@"}
Test: TestAssert
--- FAIL: TestAssert (0.00s)
FAIL
Program exited.
Similarly,
package main
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestAssert(t *testing.T) {
var demo = []string{
"delftstack.123@hotmail.com",
"Delftstack <demo@delftstack.com>",
"demo@delftstack.tv",
"demohotmail.com",
"demo@"}
// assert for not nil
assert.NotNil(t, demo)
}
The code above will test for the not nil
object or value. See the output:
=== RUN TestAssert
--- PASS: TestAssert (0.00s)
PASS
Program exited.
Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.
LinkedIn Facebook