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?
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.
Create a configuration directory in your home directory:
mkdir ~/.grc
Create a personal grc config in ~/.grc/grc.conf:
# Go ^([/\w\.]+\/)?go test\b conf.gotest
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
Now you can run Go tests with colorization using:
grc go test -v ./..
To avoid typing grc every time, add an alias to your shell:
alias go=grc go
This will enable you to run tests with colorization simply by typing:
go test -v ./..
Enjoy the convenience of color-coded test results in your terminal!
Atas ialah kandungan terperinci Bagaimana untuk Menambah Pewarnaan ke Keluaran Ujian menggunakan `grc`?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!