Demystifying Go (Golang), Google’s Programming Language: Unit Tests - Part 2

Demystifying Go (Golang), Google’s Programming Language: Unit Tests - Part 2

Introduction

In the first part of this article, we explored some basic functions for unit testing in Go. Now, it’s time to level up and dive into three powerful features that will make your life as a developer much easier when writing tests. So, grab your Gopher helmet, and let’s uncover these amazing tools!




SetEnv: Master Environment Variables Like a Jedi

Who hasn’t faced a test that depended on an environment variable to run? A classic example is database connections that require credentials stored in these variables. In the code below, we have the Sum function from the arithmetic package, which relies on the PLUS_ONE environment variable. If the variable is not set, the function fails and returns an error.


Without the variable set, attempting to convert an empty string to a boolean results in a conversion error. Now, let’s see what happens when we use SetEnv from the testing library to set the variable with the value false:


Voilà! The test passes successfully. But what if we change the value to true? Let’s see:


Success again! The function added 1 because the variable was set to true.

IMPORTANT: The SetEnv function clears all variables it sets after the test finishes, ensuring the environment remains unchanged for subsequent tests.




TempDir: A Safe Space to Test Your Files

Testing functions that deal with files can be tricky, especially when the code depends on directory paths that don’t exist in the test environment. Luckily, TempDir is the perfect solution for creating temporary directories during tests.

Let’s analyze the WriteFile function, which takes a path and some text (txt) to write to a file. What happens if we pass a nonexistent path?


Error! Since the directory doesn’t exist, the function fails. But now, let’s use TempDir:


Ta-da! The test succeeds. The best part? Just like SetEnv, TempDir ensures the temporary directory is deleted after the test runs, keeping your environment clean and organized.




Parallel: Speeding Up Your Tests

Large projects often contain many tests, which can take a long time to execute. But who has time to waste?

Let’s revisit the Sum function. We’ll create three distinct tests, each with a 3-second delay. Here’s what happens when they’re executed sequentially:


The tests took over 9 seconds to complete. Now, let’s see what happens when we use t.Parallel to run the tests simultaneously:


Much better! Now the tests finish in about 3 seconds, making the most of parallel processing.




Conclusion

Unit tests are essential for ensuring code quality, and Go provides powerful tools to make this process more efficient and organized. With SetEnv, you can easily manage environment variables. With TempDir, testing file operations in temporary directories becomes straightforward. And with t.Parallel, your tests gain speed without sacrificing quality.

Now it’s your turn to put these features into practice and take your tests to the next level. Testing may not be the most glamorous part of programming, but with these tools, it can certainly be more practical and even enjoyable! Let’s code and test like true Gophers!

Victor Vieira

Mobile Engineer | iOS Engineer | iOS Developer | Swift | SwiftUI | Objective - C | AWS

2 个月

Love this

回复
Jéssica Garcia

Desenvolvedora de Software | Full Stack | JavaScript | TypeScript | React | Node.js | Next.js | React Native

3 个月

Great content! Congratulations, keep sharing.

回复
Paulo Henrique De Araujo Gerchon

Software Engineer | Full Stack Developer | C# | Go | React | Angular | Azure

3 个月

Very informative

回复
Leandro Veiga

Senior Software Engineer | Full Stack Developer | C# | .NET | .NET Core | React | Amazon Web Service (AWS)

3 个月

Interesting

回复
Diego Pinto da Jornada

Senior Front-End developer | Fullstack Engineer | Technical Lead | React | Next.js | Typescript | Node | Azure

3 个月

Your series is great! I always save it to have as a reference when I study Go. Thx a lot Vagner Nascimento

回复

要查看或添加评论,请登录

Vagner Nascimento的更多文章

社区洞察

其他会员也浏览了