Skip to content

Colorizing test output#1467

Open
hugoleborso wants to merge 3 commits intostretchr:masterfrom
hugoleborso:master
Open

Colorizing test output#1467
hugoleborso wants to merge 3 commits intostretchr:masterfrom
hugoleborso:master

Conversation

@hugoleborso
Copy link

@hugoleborso hugoleborso commented Sep 6, 2023

made with @EwenQuim

Summary

We added colorizing to the terminal output of tests, while avoiding to print escaping characters when the output is outside a classic terminal.

Changes

more changes to come, for now only the Equal function has been colorized

  • Added one non exported global variable to know whether the program is ran in terminal
  • Added a colors.go file handling colorization in the assert package

Motivation

Users have been requesting to color the output of tests (#946)
Lots of PR to colorize the result do not take into account whether the output is in a terminal or not

Example usage

In terminal In a file
image image

Performance

Inside a terminal - small performance loss

Benchmark iterations time/op mem size alloc/op alloc/op
BenchmarkColored/benchMarkingString-10 24840926 47.39 ns/op 24 B/op 1 allocs/op
BenchmarkColored/benchMarkingStruct-10 8493196 140.9 ns/op 72 B/op 2 allocs/op
BenchmarkEqual/Base-10 5313129 225.9 ns/op 112 B/op 3 allocs/op
BenchmarkEqual/Colored_(test_with_and_without_terminal)-10 2945031 409.1 ns/op 288 B/op 7 allocs/op

Outside a terminal - no performance loss

Benchmark iterations time/op mem size alloc/op alloc/op
BenchmarkColored/benchMarkingString-10 1000000000 0.3080 ns/op 0 B/op 0 allocs/op
BenchmarkColored/benchMarkingStruct-10 59117062 20.46 ns/op 24 B/op 1 allocs/op
BenchmarkEqual/Base-10 5326148 225.5 ns/op 112 B/op 3 allocs/op
BenchmarkEqual/Colored_(test_with_and_without_terminal)-10 4611681 259.6 ns/op 128 B/op 3 allocs/op

Next steps

We only colorized the Equal function, but if this pleases the maintainers we can also colorized all calls to Fail, and are open to suggestions

Related issues

Closes #946

@hugoleborso hugoleborso changed the title WIP : colorizing test output Colorizing test output Sep 13, 2023
@dolmen dolmen added enhancement pkg-assert Change related to package testify/assert enhancement: colored output About adding colored output labels Oct 10, 2023
hugoleborso and others added 3 commits October 11, 2023 11:35
This allows the compiler to inline the ANSI codes
and to save 2 bytes/op, reduucing the perf at 7 bytes/op
instead of 9 bytes/op. Knowing that the original code only
uses 3 bytes/op, there is still space for improvement
fredbi added a commit to fredbi/testify that referenced this pull request Jan 12, 2026
This PR adds support for colorized output, with minimal extra
dependencies.

- Actual / Expected in equal colorized
- Diff output is colorized

It is enabled with the specific extra blank import:

_ "github.com/go-openapi/testify/enable/colors/v2"

When enabled, colorized output is:
* enabled by go test flag -testify.colorize
* or by environment variable TESTIFY_COLORIZE=true

By default, colors are chose to be rendered on a dark terminal.

You may use darker colors on a bright terminal with:

* go test flag -testify.colorize -testify.theme=light
* or by environment variable TESTIFY_THEME=light

This work is inspired and adapts the following PRs:

* github.com/stretchr#1467
* github.com/stretchr#1480
* github.com/stretchr#1232
* github.com/stretchr#994

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
fredbi added a commit to fredbi/testify that referenced this pull request Jan 12, 2026
This PR adds support for colorized output, with minimal extra
dependencies.

- Actual / Expected in equal colorized
- Diff output is colorized

It is enabled with the specific extra blank import:

_ "github.com/go-openapi/testify/enable/colors/v2"

When enabled, colorized output is:
* enabled by go test flag -testify.colorize
* or by environment variable TESTIFY_COLORIZE=true

By default, colors are chose to be rendered on a dark terminal.

You may use darker colors on a bright terminal with:

* go test flag -testify.colorize -testify.theme=light
* or by environment variable TESTIFY_THEME=light

This work is inspired and adapts the following PRs:

* github.com/stretchr#1467
* github.com/stretchr#1480
* github.com/stretchr#1232
* github.com/stretchr#994

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
fredbi added a commit to fredbi/testify that referenced this pull request Jan 12, 2026
This PR adds support for colorized output, with minimal extra
dependencies.

- Actual / Expected in equal colorized
- Diff output is colorized

It is enabled with the specific extra blank import:

_ "github.com/go-openapi/testify/enable/colors/v2"

When enabled, colorized output is:
* enabled by go test flag -testify.colorized
* or by environment variable TESTIFY_COLORIZED=true

By default, colors are chose to be rendered on a dark terminal.

You may use darker colors on a bright terminal with:

* go test flag -testify.colorized -testify.theme=light
* or by environment variable TESTIFY_THEME=light

This work is inspired and adapts the following PRs:

* github.com/stretchr#1467
* github.com/stretchr#1480
* github.com/stretchr#1232
* github.com/stretchr#994

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
fredbi added a commit to fredbi/testify that referenced this pull request Jan 12, 2026
This PR adds support for colorized output, with minimal extra
dependencies.

- Actual / Expected in equal colorized
- Diff output is colorized

It is enabled with the specific extra blank import:

_ "github.com/go-openapi/testify/enable/colors/v2"

When enabled, colorized output is:
* enabled by go test flag -testify.colorized
* or by environment variable TESTIFY_COLORIZED=true

By default, colors are chose to be rendered on a dark terminal.

You may use darker colors on a bright terminal with:

* go test flag -testify.colorized -testify.theme=light
* or by environment variable TESTIFY_THEME=light

This work is inspired and adapts the following PRs:

* github.com/stretchr#1467
* github.com/stretchr#1480
* github.com/stretchr#1232
* github.com/stretchr#994

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
fredbi added a commit to go-openapi/testify that referenced this pull request Jan 12, 2026
This PR adds support for colorized output, with minimal extra
dependencies.

- Actual / Expected in equal colorized
- Diff output is colorized

It is enabled with the specific extra blank import:

_ "github.com/go-openapi/testify/enable/colors/v2"

When enabled, colorized output is:
* enabled by go test flag -testify.colorized
* or by environment variable TESTIFY_COLORIZED=true

By default, colors are chose to be rendered on a dark terminal.

You may use darker colors on a bright terminal with:

* go test flag -testify.colorized -testify.theme=light
* or by environment variable TESTIFY_THEME=light

This work is inspired and adapts the following PRs:

* github.com/stretchr#1467
* github.com/stretchr#1480
* github.com/stretchr#1232
* github.com/stretchr#994

Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement: colored output About adding colored output enhancement pkg-assert Change related to package testify/assert

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Colorize "expected" and "actual"

5 participants