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

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

Linda Hamilton
Release: 2024-11-15 14:22:02
Original
1111 people have browsed it

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
Copy after login

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

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

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
Copy after login

Running Tests with Colorization

Now you can run Go tests with colorization using:

grc go test -v ./..
Copy after login

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

alias go=grc go
Copy after login

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

go test -v ./..
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template