给Golang测试运行输出着色化。

huangapple go评论74阅读模式
英文:

colorizing golang test run output

问题

我喜欢在终端/控制台测试运行时,实际上以红色或绿色文本显示输出。似乎很多可用于Go的测试库都有这个功能。然而,我想只使用Go自带的默认测试包。有没有办法将其输出着色为红色和绿色?

英文:

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?

答案1

得分: 94

你可以使用 grc,一个通用的着色工具,来给任何东西着色。

在 Debian/Ubuntu 上,使用 apt-get install grc 命令进行安装。在 Mac 上,使用 brew install grc 命令进行安装。

在你的主目录下创建一个配置目录:

mkdir ~/.grc

然后在 ~/.grc/grc.conf 中创建你的个人 grc 配置:

# Go
\bgo.* test\b
conf.gotest

然后在 ~/.grc/conf.gotest 中创建一个 Go 测试的着色配置,例如:

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

现在你可以使用以下命令运行 Go 测试:

grc go test -v ./..

示例输出:

给Golang测试运行输出着色化。

为了避免每次都输入 grc,你可以在你的 shell 中添加一个别名(如果使用 Bash,可以在 ~/.bashrc~/.bash_profile 中添加,具体取决于你的操作系统):

alias go=grc go

现在你只需运行以下命令即可获得着色效果:

go test -v ./..
英文:

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:

给Golang测试运行输出着色化。

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

答案2

得分: 75

你可以创建一个包装的shell脚本,并使用颜色转义序列进行着色。这里是一个在Linux上的简单示例(我不确定在Windows上会是什么样子,但我猜应该有一种方法.. :))

go test -v . | sed '/PASS/s/$(printf "3[32mPASS3[0m")/' | sed '/FAIL/s/$(printf "3[31mFAIL3[0m")/'
英文:

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.. 给Golang测试运行输出着色化。 )

go test -v . | sed ''/PASS/s//$(printf "3[32mPASS3[0m")/'' | sed ''/FAIL/s//$(printf "3[31mFAIL3[0m")/''

答案3

得分: 24

这里还有一个名为richgo的工具,以用户友好的方式实现了这一功能。

给Golang测试运行输出着色化。

英文:

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

给Golang测试运行输出着色化。

答案4

得分: 4

你仍然需要一个库来添加颜色转义代码,例如:

从那里,你可以指定你想要着色的内容(如此示例中的StdOut或StdErr)

英文:

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)

答案5

得分: 4

rakyll/gotest截图)是一个可以执行此操作的二进制文件。

示例:

$ gotest -v github.com/rakyll/hey
英文:

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

Example:

$ gotest -v github.com/rakyll/hey

答案6

得分: 3

BoltDB有一些测试方法,看起来像这样:

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

这里是其余的代码。我在这里添加了绿色的点。

英文:

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("3[31m%s:%d: "+msg+"3[39m\n\n", append([]interface{}{filepath.Base(file), line}, v...)...)
        tb.FailNow()
    }
}

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

答案7

得分: 3

表情符号

你可以像其他人在他们的答案中提到的那样使用颜色来为文本添加颜色,可以使用颜色代码或使用第三方库。

但是你也可以使用表情符号!例如,你可以使用⚠️表示警告消息,使用🛑表示错误消息。

或者简单地使用这些表情符号作为颜色的替代:

📕:错误消息
📙:警告消息
📗:正常状态消息
📘:操作消息
📓:取消状态消息
📔:或者任何你喜欢并希望通过颜色立即识别的内容

🎁 奖励:

这种方法还可以帮助你快速扫描并直接在源代码中找到日志。

<sub>但是,某些Linux发行版的默认表情符号字体默认情况下不是彩色的,你可能希望先将它们变成彩色。</sub>

英文:

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 &#128721; for error messages.

Or simply use these notebooks as a color:

&#128213;: error message
&#128217;: warning message
&#128215;: ok status message
&#128216;: action message
&#128211;: canceled status message
&#128212;: 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.

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

答案8

得分: 0

根据Alexander Staubo的回答建议,可以使用grc来获取带有颜色的输出。

在Linux上,配置文件存储在**/etc/grc.conf**<br>
相反,我们可以使用标志-c:grc -c .grc/conf.gotest go test ….<br>
因此,我们只需要conf.gotest文件。

英文:

As suggested from Alexander Staubo's answer, one can use grc for getting colorized output.

On linux the config file is stored in /etc/grc.conf<br>
Instead we can use the flag -c: grc -c .grc/conf.gotest go test ….<br>
So we only need the conf.gotest file.

答案9

得分: 0

我喜欢使用awk脚本来实现这个功能。这样,你可以根据自己的喜好自定义颜色和模式。你有一个特定于项目的测试输出,想要用特定的颜色突出显示吗?可以试试这个方法。

只需将以下awk脚本保存到项目的./bin/colorize文件中,然后运行chmod 755 ./bin/colorize,并根据需要进行自定义:

#!/usr/bin/awk -f

# colorize - 为go test输出添加颜色
# 用法:
#   go test ./... | ./bin/colorize
#

BEGIN {
    RED="3[31m"
    GREEN="3[32m"
    CYAN="3[36m"
    BRRED="3[91m"
    BRGREEN="3[92m"
    BRCYAN="3[96m"
    NORMAL="3[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

然后运行测试并将输出通过管道传递给colorize

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

我发现这种方法比使用sed命令更容易阅读和自定义,而且比使用grc等外部工具更轻量简单。

英文:

I like piping to an awk script for this. That way, you can customize with whatever colors/patterns suit you. You have a particular test output unique to your project you want to highlight with a certain color? 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=&quot;\033[31m&quot;
    GREEN=&quot;\033[32m&quot;
	CYAN=&quot;\033[36m&quot;
	BRRED=&quot;\033[91m&quot;
    BRGREEN=&quot;\033[92m&quot;
	BRCYAN=&quot;\033[96m&quot;
    NORMAL=&quot;\033[0m&quot;
}
         { 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.

huangapple
  • 本文由 发表于 2014年12月2日 13:43:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/27242652.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定