Golang Unit Testing

seventech

Ega Prasetya

Posted on March 5, 2021

Golang Unit Testing

Okay so in the first write i gonna to show you how to implementation Unit Testing in Go, absolutely this is very ez to learn. So lets do it.

Create Package

so i created package name is helper, but if you wanna to create another package name it`s not a problem.

Alt Text

Like the examples above, i made function simple named Hello World. if we`re trying to test it, then it should be done with Test.go.

Next, Go has a method (t *testing.T) and then follow the code below.

Alt Text

Result

Alt Text

if we wanna to run this code, we got to the helper folder first.

In above, we create a logic which only contains an error by using the panic function.

In Go, we can perform an error other than panic, we can use t.Fail() and t.FailNow() .

It`s the difference when using t.Fail() and t.FailNow()

First, we change in Hello_World.go

Alt Text

Next, we create other function in Hello_World_test.go

Alt Text

Result :

Alt Text

So we got this, thx for read and sorry for the mistakes, Enjoy.

💖 💪 🙅 🚩
seventech
Ega Prasetya

Posted on March 5, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Golang Unit Testing
go Golang Unit Testing

March 5, 2021

Testing API Libraries
go Testing API Libraries

July 22, 2020