Home > Backend Development > Golang > How can I colorize Go test output using the default testing package?

How can I colorize Go test output using the default testing package?

Linda Hamilton
Release: 2024-11-20 00:16:02
Original
1015 people have browsed it

How can I colorize Go test output using the default testing package?

Colorizing Output in Go Tests with Default Testing Package

While many Go testing libraries offer colored output, colorizing the output of the default testing package can be achieved using a generic colourizer called grc.

Installation:

On Debian/Ubuntu:

apt-get install grc
Copy after login

On macOS with Homebrew:

brew install grc
Copy after login

Configuration:

Create a configuration directory:

mkdir ~/.grc
Copy after login

Configure your grc settings in ~/.grc/grc.conf:

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

Create a Go test colourization config in ~/.grc/conf.gotest:

# Example 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

Usage:

To colourize Go test runs, use the following command:

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

Optional Alias:

For convenience, you can add an alias to your shell:

alias go=grc go
Copy after login

Now, you can simply run go test -v ./.. to get colorized output.

The above is the detailed content of How can I colorize Go test output using the default testing package?. 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