Ad space (banner)
๐ŸนGo Lessons
Lesson 28 / 68

Testing with Your Own assert Function

In this lesson you will learn to write a simple test with your own assert function in Go, taking your first step towards checking that a program is correct. This is for anyone searching for "Go assert testing how to write".

Go has no built-in assert, but you can write a small checking helper yourself. Checking automatically that a function returns the expected result is the basic idea behind test code.

The sample code has assertEqual(expected, actual int) compare two values, printing "Test passed" when they match and "Test failed" when they do not. Checking the result of add(2, 3) with assertEqual(5, ...) gives you a first taste of how testing works.

A common stumbling block for beginners is checking behaviour manually every time and missing cases. In practice you would use Go's standard testing package with the go test command, but the underlying idea is exactly what you learn here.

In real-world development it is standard practice to re-run tests automatically after every change, to confirm you have not broken something else by accident.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run Go directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)