9

When running test/unit using the rake test command from the terminal within a rails 3 project directory, the test result output is not coloured. Hence, it cannot be interpreted at a glance.

Is there a way of getting colourised output for the results, as you can get in rspec?

>rspec --colour
notapatch
  • 6,569
  • 6
  • 41
  • 45
John Kane
  • 3,601
  • 5
  • 23
  • 18
  • why just use the rspec command ? – shingara Oct 21 '10 at 21:30
  • I went with the defaults and am using test/unit. – John Kane Oct 21 '10 at 21:40
  • [The answers with the highest votes are abandoned projects the only answer with active project is minitest-reporters below](https://stackoverflow.com/questions/3992083/in-rails-is-it-possible-to-get-colour-highlighting-for-the-rake-test-command/54043367#54043367) – notapatch Jul 03 '21 at 07:29

3 Answers3

20

I discovered that redgreen was abandoned years ago, and found this solution which works well and requires no script hacking. The output, however, shows which test is being run in real time. So it is a lot longer than built in test output. It does have nice colors.

http://rubygems.org/gems/turn

In my Gemfile:

group :test do
  gem 'turn'
end

Then run:

$ bundle install
$ rake test

The gem 'turn' works great. The caveat is that it doesn't seem to work with Mocha, due to monkey-patching issues. If you are using Mocha, you can use the redgreen gem. See instructions above in the approved answer for this question.

wpp
  • 7,093
  • 4
  • 33
  • 65
B Seven
  • 44,484
  • 66
  • 240
  • 385
  • 2
    `gem turn` helps a lot. it works with Rails 4.0 + Ruby 2.0. I just tested it right now. – Tonny Xu Jul 21 '13 at 14:22
  • Awesome thanks, I like the fact that with 3 lines of code you get decent colors :-) – patm Feb 05 '14 at 08:54
  • This looked promising but the author decided to deprecate it and doesn't work with ruby 4.2.0 out of the box anymore – markdrake Jan 04 '15 at 19:27
  • 1
    @markdrake - Wow, this answer is old. I don't use Test Unit anymore, but perhaps a gem is no longer required? Check this SO question: http://stackoverflow.com/questions/15029071/test-unit-results-in-color-using-console-but-not-tmux-unless-use-color-is-spec – B Seven Jan 04 '15 at 19:51
10

Yes, you can use the redgreen gem. Include it in your gemfile:

group :development, :test do
  gem 'redgreen'
end

And that's all you need for ruby 1.8. If you're using 1.9, there's a workaround. add the test-unit gem:

group :development, :test do
  gem 'redgreen'
  gem 'test-unit', '1.2.3
end

It's not perfect with 1.9 - test-unit seems to run an empty test suite after every rake task or generator call, which is harmless but annoying.

Jaime Bellmyer
  • 23,051
  • 7
  • 53
  • 50
  • 1
    OK, finally got this working! Thanks for the answer. You need that second gem (test-unit) for 1.9 (.2 in my case). Still prefer the output from gem turn, but this looks good too. Why do you put them in the development group? – B Seven Aug 27 '11 at 01:33
  • 1
    In my case, removing the redgreen and test-unit gem from the development group in the Gemfile avoids the unnecesary test suite run. – e3matheus Feb 14 '12 at 18:36
3

I am working on Rails 5.1 / minitest and I was also searching for a solution to make the reporting color. None of these test::unit solutions are working, so I googled and saw this solution. Just add the following:

# Gemfile
gem 'minitest-reporters'

# test/test_helper.rb
require "minitest/reporters"
Minitest::Reporters.use!

Github: minitest-reporters

notapatch
  • 6,569
  • 6
  • 41
  • 45
Kamal Panhwar
  • 2,345
  • 3
  • 21
  • 37
  • Here is some more info on changing default reporters: https://github.com/kern/minitest-reporters – A13X Mar 29 '20 at 17:43