Ready. Test. Go. — Go Testing for everyone. - 11/12/2023

Tests might save you by a brink!

Site under construction! Migrated content might be displayed ugly!

medium-to-markdown@0.0.3 convert node index.js https://medium.com/stackademic/ready-test-go-go-testing-for-everyone-7cd06027a0c0

Ready. Test. Go. — Go Testing for everyone.

[Enigma Bits



Enigma Bits



Published in[


](https://blog.stackademic.com/?source=post_page-----7cd06027a0c0--------------------------------)·3 min read·Dec 11, 2023



from unsplash.com

Welcome aboard, fellow Gopher! Whether you’re embarking on your Go journey or looking to solidify your testing skills, you’ve come to the right place.

In this post, we’ll explore testing in Golang, a language that embraces simplicity and verbosity. So, fasten your seatbelt, and let’s dive in.

Rather than drowning developers in a myriad of testing frameworks, Go provides the absolute base, allowing developers to construct on top of it.

Download source code sample here.

The Bare Essential: testing.T

At the heart of Golang testing, lies the testing package, and the indispensable testing.T type. This combination is your trusty sidekick when it comes to writing tests.

a go sample function as test

To illustrate, let’s consider a simple example. Imagine a utility function that performs division, and now, we want to put it to the test.

a test to — well test the divide function

Testing this atomic function is as simple as given below. This is not a tutorial stripped-down version. This is the idiomatic way to write tests. Simple right?

Here, testing in Go doesn’t involve convoluted patterns or arcane conventions. It’s just regular programming but with a sprinkle of t.Fatal() and the like to indicate the test runner test has failed.

You can read the exhaustive list here.

Running the tests

In the Golang realm, running tests is a breeze. The go binary has a trusty companion, test. Execute your tests effortlessly with go test, and watch the magic unfold.

go test   
\# use this to run the tests in current directory and children

All files with names containing the test are executed.

Test files can access private methods using *_test as the package name, allowing you to test even the most clandestine functions.

What’s Next?

This is the absolute bare bones you need to get started with golang testing. However, it may occur to you that things are WET, that is repetitive. You need to know Table-driven testing. Fortunately, I already wrote about it here.

a trailer of newsletter

“Testing is an infinite process of comparing the invisible to the ambiguous in order to avoid the unthinkable happening to the anonymous.” — James Bach


Thank you for exploring the world of Go testing with me! I hope this comprehensive guide has enriched your understanding of testing strategies in Go.

Your support is crucial in fueling our mission, and by subscribing to my newsletter(it’s free btw), you are encouraging me to continue creating quality educational content.

Subscribe now, stay ahead in your Go development journey, and let’s code for a brighter future together! 🚀✨


Thank you for reading until the end. Before you go: