DEV Community

Cover image for Tame Testing Chaos with Gotestsum
Guiyomh
Guiyomh

Posted on

Tame Testing Chaos with Gotestsum

Testing is now a crucial part of the software development process. However, managing results can rapidly become chaotic when you have a lot of tests. This is where the potent open-source utility Gotestsum comes in, which is made to make managing test results in Go easier and better. By combining the outcomes of several tests into a single, understandable report, this developer companion helps you to improve the readability and comprehension of the status of your tests.

Gotestsum is a useful tool that can help developers of all experience levels manage their tests more efficiently and identify possible problems more rapidly. Let's take a look!

Ready in a Blink

To install Gotestsum, you can use the following command:

go install gotest.tools/gotestsum@latest
Enter fullscreen mode Exit fullscreen mode

Gotestsum is designed to be user-friendly and work without additional configuration.

Gotestsum's Super Powers

Gotestsum offers a set of powerful features to improve the management of your Go test results.

Personalize Your Tests

It allows you to customize the output format of test results
with the option --format. You can choose from different formats, such as:

  • dots prints one character for each test
  • dots-v2 experimental dots format, one packet per line
  • pkgname prints one line for each packet
  • pkgname-and-test-fails prints one line for each packet and failed test results
  • testname prints one line for each test and each packet
  • testdox prints a sentence for each test using gotestdox
  • github-actions test name format with github actions log grouping
  • standard-quiet standard format go test
  • standard-verbose standard format go test -v

A demonstration of --format option

Technicolor tests

The reports generated by Gotestsum are colored for better readability. Passed tests are displayed in green, failed tests in red, and unexecuted tests in yellow. This coloring makes the status of your tests immediately visible, which is particularly useful when running large tests.

The Ideal Companion for your CI/CD

An important feature is the ability to produce results in Junit or Json format, which makes it a valuable tool for CIs such as Gitlab, Circle CI, Github-action,...

When the --junitfile option specifies a file path, gotestsum will write a test report, in JUnit XML format, to the file. This file can be used to integrate CI systems.

gotestsum --junitfile unit-tests.xml
Enter fullscreen mode Exit fullscreen mode

Your Local Test Assistant

When working on your local development project, Gotestsum offers features that simplify the testing process. The --watch option allows you to automatically run tests of the package that has changed every time a .go file is saved. This ensures that your tests are always up to date and helps you quickly spot any issues introduced during your changes.

gotestsum --watch
Enter fullscreen mode Exit fullscreen mode

Additionally, you can use the --post-run-command option to run a command after running the tests. This can be particularly useful for displaying notifications on your desktop about test results. This way, you can stay informed in real time about the status of your tests during your development process.

gotestsum --post-run-command notify
Enter fullscreen mode Exit fullscreen mode

A Powerful Ally

These capabilities transform Gotestsum into a powerful tool for Go developers, streamlining the administration of test results, improving visibility of test status and accelerating the debugging process.

Gotestsum adapts to your needs, whether you're working on a large team project or a small personal one. It can be tailored to your needs, including interaction with your favorite CI/CD systems, desktop notifications at the end of tests and customized output formats.

I hope I've convinced you that Gotestsum is an effective tool that can help you move forward with your Go development projects.

Top comments (0)