Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Colorizing test output #1467

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion assert/assertions.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ func Equal(t TestingT, expected, actual interface{}, msgAndArgs ...interface{})
expected, actual = formatUnequalValues(expected, actual)
return Fail(t, fmt.Sprintf("Not equal: \n"+
"expected: %s\n"+
"actual : %s%s", expected, actual, diff), msgAndArgs...)
"actual : %s%s", greenColored(expected), redColored(actual), diff), msgAndArgs...)
}

return true
Expand Down
30 changes: 30 additions & 0 deletions assert/colors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package assert

import (
"fmt"
"os"

"golang.org/x/term"
)

const (
redMark = "\033[0;31m"
greenMark = "\033[0;32m"
endMark = "\033[0m"
)

var isTerminal = term.IsTerminal(int(os.Stdout.Fd()))

func redColored(i interface{}) interface{} {
if isTerminal {
return fmt.Sprintf(redMark+"%s"+endMark, i)
}
return i
}

func greenColored(i interface{}) interface{} {
if isTerminal {
return fmt.Sprintf(greenMark+"%s"+endMark, i)
}
return i
}
53 changes: 53 additions & 0 deletions assert/colors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package assert

import (
"fmt"
"testing"
)

func BenchmarkColored(b *testing.B) {
b.Run("benchMarkingString", func(b *testing.B) {
for i := 0; i < b.N; i++ {
greenColored("helloWorld")
}
})
b.Run("benchMarkingStruct", func(b *testing.B) {
s := struct {
a int
b string
}{3, "helloWorld"}
for i := 0; i < b.N; i++ {
greenColored(s)
}
})
}

// Not benchmarking `Equal` but the string formatting
// because it will make the benchmark fail.
func BenchmarkEqual(b *testing.B) {

s := struct {
a int
b string
}{3, "helloWorld"}

b.Run("Base", func(b *testing.B) {

for i := 0; i < b.N; i++ {
_ = fmt.Sprintf("Not equal: \n"+
"expected: %v\n"+
"actual : %v%s", s, s, "")
}
})

b.Run("Colored (test with and without terminal)", func(b *testing.B) {
// This benchmark give very different results whether the output is a terminal or not.

for i := 0; i < b.N; i++ {
_ = fmt.Sprintf("Not equal: \n"+
"expected: %s\n"+
"actual : %s%s", greenColored(s), redColored(s), "")
}
})

}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@ require (
github.com/davecgh/go-spew v1.1.1
github.com/pmezard/go-difflib v1.0.0
github.com/stretchr/objx v0.5.0
golang.org/x/term v0.12.0
gopkg.in/yaml.v3 v3.0.1
)

require golang.org/x/sys v0.12.0 // indirect
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.12.0 h1:/ZfYdc3zq+q02Rv9vGqTeSItdzZTSNDmfTi0mBAuidU=
golang.org/x/term v0.12.0/go.mod h1:owVbMEjm3cBLCHdkQu9b1opXd4ETQWc3BhuQGKgXgvU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Expand Down