Introduction
Rspec, one of the most popular unit testing frameworks for Ruby these days, gives you the possibility to tweak its default output via Formatters.
Formatters are Ruby classes that you can write to change the behaviour of rspec while it is printing test results in the terminal. Most formatters are packed in Ruby gems and are typically hosted at rubygems.org.
Background
I decided to start my own formatter because I discovered some interesting Unicode characters that I could use to simulate the characters of the classic game Pacman.
The characters I found are listed below:
Unicode character | Pacman equivalent |
https://www.compart.com/en/unicode/U+15E3 | ᗣ Ghosts |
https://www.compart.com/en/unicode/U+15E7 | ᗧ Pacman |
https://www.compart.com/en/unicode/U+2022 | • Pacdot |
Then if you add a bit of color to these characters, you can achieve something like:
Notice that when one of the specs fail, you will get a random ghost between Blinky, Pinky, Inky or Clyde.
Using the Code
The code is available here and all you need to do is:
- Adding the gem dependency in the Gemfile:
group :test do
gem 'rspec_pacman_formatter'
end
- Run:
bundle
- Run specs with the
--format
flag to use the formatter inside rspec_pacman_formatter
gem.
rspec --format RspecPacmanFormatter::Pacman
Points of Interest
This was my first gem that I published on rubygems.org and it has more than 2k downloads already.