colorizing golang test run output

24,184

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:

screenshot

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.

enter image description here

Solution 4

You would still need a library to add color escape code like:

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
Share:
24,184

Related videos on Youtube

Elliot Larson
Author by

Elliot Larson

Updated on July 09, 2022

Comments

  • Elliot Larson
    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
    Elliot Larson over 9 years
    That's a good light weight solution. I didn't think of just piping the output to sed. It works. Thanks.
  • Farshid T
    Farshid T almost 9 years
    Currently, kortschak/ct doesn't support Windows.
  • Ivan Black
    Ivan Black over 7 years
    There is also github.com/logrusorgru/aurora for Unix and Win10+, that supports Printf/Sprintf formatting. For example fmt.Printf("value %d", Red(3))
  • VonC
    VonC over 7 years
    @IvanBlack Thank yo. I have added your link to the answer for more visibility.
  • IvRRimUm
    IvRRimUm over 7 years
    Thanks! I added -cover parameters and added it as alias. So everytime i want to run all tests i just run got
  • Fabio Russo
    Fabio Russo over 7 years
    Great answer. Works like a charm.
  • 0xbadbeef
    0xbadbeef over 6 years
    The above colour specifier in the configuration causes grcat to throw ValueError: Invalid keyword. grcat expects the key colours instead (see here)
  • Alexander Staubo
    Alexander Staubo over 6 years
    Thanks. Looks like a regression. colour works fine with grc 1.11.1.
  • Dimitris Baltas
    Dimitris Baltas over 6 years
    an 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
    Dimitris Baltas over 6 years
    on 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
    Jarmo Pertman over 5 years
    Beware 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 use pipefail.
  • Sarvnashak
    Sarvnashak about 4 years
    If anyone's using this command in their Makefile, please replace $ with $$ for it to work correctly.
  • Bruce Bigirwenkya
    Bruce Bigirwenkya over 2 years
    How do I apply emojis? I'm guessing direct input as emojis via the configuration files