Implementing Testing in Golang

Hello dear readers, being a Junior to Mid Developer myself I’ve been of course faced to this weird “fear” of testing my code. Maybe it isn’t fear, maybe we just talk much about it.

But either way I’ve been recently learning to program in Golang and I want it to do it seriously, in the right way. That’s why this time I did not skip the tests subject.

In this article I want to show how easily testing our code can be, it is not an effort, specially with the testing standard package incorporated in Go.

As an example I am going to implement and test a method that checks if a phrase is a heterogram or not.

An heterogram is basically a word that doesn’t contain a repeating letter. However it can contain multiple spaces or symbols like hyphens (-).

Let’s get into it! Writing our first test

In our heterogram_test.gofile let’s import the golang testing package and create a function TestIsHeterogram, passing in parameter a pointer to the struct T of that package to manage test state and support formatted test logs. We’re going to define an input that is the string we want to check and also what we excepect of it.

For example, the string (input) toto is expected to return false because the “o” letter is present 2 times.

So if IsHeterogram(“toto”) returns false the test passes and if it returns true the tests fails.

Now to start using this very simple test we need to actually start implementing our heterogram method.

Implementing Heterogram algorithm

Now we need to check if the following character (rune) of our loop is present in our slice exists, if it is then we return false. For that we are going to write another helper function that we are going to call simply contains(). This method is going to loop trough a slice of runes and check if a rune is present or not.

Now back to IsHeterogram() we can check if a character of our string is already in the exists slice. If it does exists then we return false, if not at the end of our loop our method is going to return true.

Let’s go back to our test function and run it. To run a test simply run go test inside of the heterogram package (cd into that folder) or use your IDE to run it.

We can see that our test has passed. Now let’s try something, let’s see if our method is well tested, change the input from “toto” to “Alpha” and run the test again. The world “Alpha” has two “a” so the test should pass right ? It should return false.

Well as we can see it has failed, the result for “Alpha” is true. And this shows that our method IsHeterogram is not finished, we didn’t handled the uppercase letters. See how testing can improve our development ?
Let’s quickly fix our method by lowercase all of our inputs using the package strings.

Okay perfect, what if we wanted to test the case of “six-years-old” for example, remember that hyphens (-) are allowed. Well we could change again the input variable but let me show a better way to handle multiple test cases 🙂.

Add multiple test cases

We are going to do this by defining a struct with 3 fields: description, input and expected. We are going to pass in as many cases as we want. Then all we have to do is go back to our TestHeterogram function and replace the previous code by a for loop that ranges our struct of cases.

See how simple our test file looks now. We can run all our tests cases in a single time.

Before running go test one last time let’s fix our IsHeterogram method to handle double hyphens. To do this before before checking if the exists slice contains our letter I am going to check if it is actually a letter by using the unicode package.

Okay now we are ready hit that go test one final time and all our tests should pass 🙂.

Conclusion

See you soon ! 🙂

Web Developer