Elliot Larson
Elliot Larson

Reputation: 11039

colorizing golang test run output

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?

Upvotes: 66

Views: 35352

Answers (9)

Md. A. Apu
Md. A. Apu

Reputation: 1250

Note: Extension from @Makpoc's answer, Credit goes to him

This might be a bit too much but I'm using it :D


go test -v ./... | sed ''/---\ PASS:/s//"$(printf "\033[32m✅--- PASS:\033[0m")"/'' | sed ''/---\ FAIL:/s//"$(printf "\033[31m❌--- FAIL:\033[0m")"/''

Prints like this enter image description here

*If your custom log needs to be displayed, it must contain --- inside it. This is applicable for all the following commands as I've used --- with grep

This one hides ? ... [no test files] logs

go test -v ./... | grep -E "\-\-\-" | sed ''/---\ PASS:/s//"$(printf "\033[32m✅ PASS:\033[0m")"/'' | sed ''/---\ FAIL:/s//"$(printf "\033[31m❌ FAIL:\033[0m")"/''

This one is the most compact of all, only prints pass/fail lines

go test -v ./... | grep -E "\-\-\-" | sed ''/---\ PASS:/s//"$(printf "\033[32m✅ PASS:\033[0m")"/'' | sed ''/---\ FAIL:/s//"$(printf "\033[31m❌ FAIL:\033[0m")"/''

Prints like enter image description here

If you're inside docker container, you might need to enable xterm like

docker exec -it -e "TERM=xterm-256color" [container_name] /bin/[sh/bash]

Upvotes: 0

mattmc3
mattmc3

Reputation: 18335

I like piping to a simple awk script for this. That way, you can customize with whatever colors/patterns suit you. You want to colorize an output unique to your project? Go for it.

Simply save this awk script to ./bin/colorize in your project, chmod 755 ./bin/colorize, and customize to your needs:

#!/usr/bin/awk -f

# colorize - add color to go test output
# usage:
#   go test ./... | ./bin/colorize
#

BEGIN {
    RED="\033[31m"
    GREEN="\033[32m"
    CYAN="\033[36m"
    BRRED="\033[91m"
    BRGREEN="\033[92m"
    BRCYAN="\033[96m"
    NORMAL="\033[0m"
}
         { color=NORMAL }
/^ok /   { color=BRGREEN }
/^FAIL/  { color=BRRED }
/^SKIP/  { color=BRCYAN }
/PASS:/  { color=GREEN }
/FAIL:/  { color=RED }
/SKIP:/  { color=CYAN }
         { print color $0 NORMAL }

# vi: ft=awk

And then call your tests and pipe to colorize with:

go test ./... | ./bin/colorize

I find this method much easier to read and customize than the sed answers, and much more lightweight and simple than some external tool like grc.

Note: For the list of color codes, see here.

Upvotes: 1

Alexander Staubo
Alexander Staubo

Reputation: 3365

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
^([/\w\.]+\/)?go test\b
conf.gotest

Then create a Go test colourization config in ~/.grc/conf.gotest, such as:

# 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

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 ./..

Upvotes: 109

Mojtaba Hosseini
Mojtaba Hosseini

Reputation: 119310

Emoji

You can use colors for text as others mentioned in their answers to have colorful text with a color code or using a third-party library.

But you can use emojis instead! for example, you can use⚠️ for warning messages and 🛑 for error messages.

Or simply use these notebooks as a color:

📕: error message
📙: warning message
📗: ok status message
📘: action message
📓: canceled status message
📔: Or anything you like and want to recognize immediately by color

🎁 Bonus:

This method also helps you to quickly scan and find logs directly in the source code.

But some distributions of Linux default emoji font are not colorful by default and you may want to make them colorful, first.

Upvotes: 3

twifkak
twifkak

Reputation: 87

rakyll/gotest (screenshot) is a binary that does this.

Example:

$ gotest -v github.com/rakyll/hey

Upvotes: 7

Alexander Staubo
Alexander Staubo

Reputation: 3365

There's also a tool called richgo that does exactly this, in a user-friendly way.

enter image description here

Upvotes: 26

VonC
VonC

Reputation: 1324576

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)

Upvotes: 4

bentrevor
bentrevor

Reputation: 268

BoltDB has some test methods that look like this:

func assert(tb testing.TB, condition bool, msg string, v ...interface{}) {
    if !condition {
        _, file, line, _ := runtime.Caller(1)
        fmt.Printf("\033[31m%s:%d: "+msg+"\033[39m\n\n", append([]interface{}{filepath.Base(file), line}, v...)...)
        tb.FailNow()
    }
}

Here are the rest. I added the green dots here.

Upvotes: 3

Makpoc
Makpoc

Reputation: 4179

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")/''

Upvotes: 79

Related Questions