DEV Community

Cover image for Golang Unit Testing
Ega Prasetya
Ega Prasetya

Posted on

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.

Top comments (4)

Collapse
 
ceoshikhar profile image
Shikhar

Out of context, but what font is used in the VS Code screenshot? Looks very vintage and would love to use it.

Collapse
 
seventech profile image
Ega Prasetya

parsinta theme

Collapse
 
ceoshikhar profile image
Shikhar

I think that's the VSCode theme, I was asking about the Font

Thread Thread
 
seventech profile image
Ega Prasetya

That’s a package deal