Skip to content

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