Home  >  Article  >  Backend Development  >  How to Add Colorization to Go Test Output using `grc`?

How to Add Colorization to Go Test Output using `grc`?

Linda Hamilton
Linda HamiltonOriginal
2024-11-15 14:22:02979browse

How to Add Colorization to Go Test Output using `grc`?

How to Add Colorization to Go Test Run Output

When running terminal/console tests, it can be useful to have the output displayed in red or green text to indicate failure or success. Many Go testing libraries offer this feature, but what if you want to use the default Go testing package?

Utilizing grc for Colorization

The solution lies in grc, a generic colorizer that can be applied to any output. To install it on Debian/Ubuntu, use apt-get install grc. On a Mac with Homebrew, use brew install grc.

Setting Up Your Configuration

Create a configuration directory in your home directory:

mkdir ~/.grc

Create a personal grc config in ~/.grc/grc.conf:

# Go
^([/\w\.]+\/)?go test\b
conf.gotest

Finally, create a Go test colourization config in ~/.grc/conf.gotest:

# go-test grc colorizer configuration
regexp==== RUN .*
colour=bright_blue
-
regexp=--- PASS: .* (\(\d+\.\d+s\))
colour=green, yellow
-
regexp=^PASS$
colour=bold white on_green
-
regexp=^(ok|FAIL)\s+.*
colour=default, magenta
-
regexp=--- FAIL: .* (\(\d+\.\d+s\))
colour=red, yellow
-
regexp=^FAIL$
colour=bold white on_red
-
regexp=[^\s]+\.go(:\d+)?
colour=cyan

Running Tests with Colorization

Now you can run Go tests with colorization using:

grc go test -v ./..

To avoid typing grc every time, add an alias to your shell:

alias go=grc go

This will enable you to run tests with colorization simply by typing:

go test -v ./..

Enjoy the convenience of color-coded test results in your terminal!

The above is the detailed content of How to Add Colorization to Go Test Output using `grc`?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn