colorizing golang test run output
Solution 1
You can create a wrapper shell script for this and color it using color escape sequence. Here's a simple example on Linux (I'm not sure how this would look on windows, but I guess there is a way.. :) )
go test -v . | sed ''/PASS/s//$(printf "\033[32mPASS\033[0m")/'' | sed ''/FAIL/s//$(printf "\033[31mFAIL\033[0m")/''
Solution 2
You can use grc, a generic colourizer, to colourize anything.
On Debian/Ubuntu, install with apt-get install grc
. On a Mac with , brew install grc
.
Create a config directory in your home directory:
mkdir ~/.grc
Then create your personal grc config in ~/.grc/grc.conf
:
# Go
\bgo.* test\b
conf.gotest
Then create a Go test colourization config in ~/.grc/conf.gotest
, such as:
regexp==== RUN .*
colour=blue
-
regexp=--- PASS: .*
colour=green
-
regexp=^PASS$
colour=green
-
regexp=^(ok|\?) .*
colour=magenta
-
regexp=--- FAIL: .*
colour=red
-
regexp=[^\s]+\.go(:\d+)?
colour=cyan
Now you can run Go tests with:
grc go test -v ./..
Sample output:
To avoid typing grc
all the time, add an alias to your shell (if using Bash, either ~/.bashrc
or ~/.bash_profile
or both, depending on your OS):
alias go=grc go
Now you get colourization simply by running:
go test -v ./..
Solution 3
There's also a tool called richgo that does exactly this, in a user-friendly way.
Solution 4
You would still need a library to add color escape code like:
- for Windows:
mattn/go-colorable
orshiena/ansicolor
- for Unix or Windows:
fatih/color
orkortschak/ct
- for Unix or Windows:
logrusorgru/aurora
(mentioned by Ivan Black in the comments)
From there, you specify what you want to color (StdOut or StdErr, like in this example)
Solution 5
rakyll/gotest (screenshot) is a binary that does this.
Example:
$ gotest -v github.com/rakyll/hey
Related videos on Youtube
Elliot Larson
Updated on July 09, 2022Comments
-
Elliot Larson almost 2 years
I like it when terminal/console test runs actually show their output in either red or green text. It seems like a lot of the testing libraries available for Go have this. However, I'd like to just use the default testing package that comes with Go. Is there a way to colorize it's output with red and green?
-
Elliot Larson over 9 yearsThat's a good light weight solution. I didn't think of just piping the output to sed. It works. Thanks.
-
Farshid T almost 9 yearsCurrently,
kortschak/ct
doesn't support Windows. -
Ivan Black over 7 yearsThere is also github.com/logrusorgru/aurora for Unix and Win10+, that supports Printf/Sprintf formatting. For example
fmt.Printf("value %d", Red(3))
-
VonC over 7 years@IvanBlack Thank yo. I have added your link to the answer for more visibility.
-
IvRRimUm over 7 yearsThanks! I added -cover parameters and added it as alias. So everytime i want to run all tests i just run
got
-
Fabio Russo over 7 yearsGreat answer. Works like a charm.
-
0xbadbeef over 6 yearsThe above colour specifier in the configuration causes
grcat
to throwValueError: Invalid keyword
.grcat
expects the keycolours
instead (see here) -
Alexander Staubo over 6 yearsThanks. Looks like a regression.
colour
works fine with grc 1.11.1. -
Dimitris Baltas over 6 yearsan empty line is needed in the end of
~/.grc/conf.gotest
on mac os, with zsh and grc 1.11.1, otherwise it is trimming the last character of "cyan" and is looking for color "cya" -
Dimitris Baltas over 6 yearson mac os, with zsh and grc 1.11.1, I had to move grc.conf to /usr/local/etc/grc.conf (conf.gotest stays as is) and add this in .zshrc
[[ -s "/usr/local/etc/grc.zsh" ]] && source /usr/local/etc/grc.zsh
-
Jarmo Pertman over 5 yearsBeware that this solution loses exit code for
go test
which in turn means that if that command is used in some CI environment then build will not fail even if there are failing tests! One solution when using bash would be to usepipefail
. -
Sarvnashak about 4 yearsIf anyone's using this command in their Makefile, please replace $ with $$ for it to work correctly.
-
Bruce Bigirwenkya over 2 yearsHow do I apply emojis? I'm guessing direct input as emojis via the configuration files