How to color unit tests with lib minitest or Test:Unit?
Asked Answered
I

8

15

I would like to have unit tests output color in my dev environment. However, I can't make it work on Linux (Debian and Ubuntu). When I include the following libs:

require 'minitest/autorun'
require 'minitest/unit'
require 'minitest/pride'

I get:

/usr/local/rvm/gems/ruby-1.9.2-p136/gems/minitest-2.3.1/lib/minitest/pride.rb:35:in `<top (required)>': undefined method `output' for MiniTest::Unit:Class (NoMethodError)

caused by the code:

MiniTest::Unit.output = PrideIO.new(MiniTest::Unit.output)

I have seen a working Rspec variant. Unfortunately, my Ruby knowledge is not enough to see differences.

Interrelated answered 7/7, 2011 at 11:28 Comment(0)
Q
1

As of late 2023 the accepted answer no longer works. The following, however, does.

require "minitest"
Minitest.load_plugins
require "minitest/pride"

Include the above code in e.g. test/test_helper.rb, or in wherever you put code common to all of your tests. This will give you test runs that look similar to this:

Running minitest with the pride test output colorizer

Quillen answered 13/11, 2023 at 21:10 Comment(0)
J
14

Give turn a whirl.

Add this to your Gemfile:

group :test do
  gem 'turn', :require => false
end
Jampack answered 4/10, 2011 at 19:22 Comment(4)
It should be mentioned, that at this time, turn is not compatible with MiniTest version 5 and up.Antipole
You are correct, Matt. MiniTest v5 support is being tracked here: github.com/turn-project/turn/issues/127Jampack
For MiniTest 5+, only minitest-rg did it for me.Franciscka
Also, turn is no longer being maintained (see github.com/turn-project/turn)Indigestive
Z
13

step 1 : use the latest version of the gem (I think it will be fixed in Ruby 1.9.3)

gem install minitest

step 2 : require "minitest/pride" on the command line, not in your code

ruby -rminitest/pride your_ruby_script.rb

.. and in your code simply require 'minitest/autorun'

require 'minitest/autorun'

If you use Rubymine, just add

-rminitest

in the default configuration of the tests. => the configuration would like

-e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) -rminitest/pride
Zelmazelten answered 23/7, 2011 at 18:5 Comment(1)
Thanks. Requiring pride in my code rather than in the command-line worked fine though.Silurian
G
4

simply add these lines to your test_helper.rb file after require 'rails/test_help'

require "minitest/reporters"
Minitest::Reporters.use!

in your gemfile add

gem 'minitest-reporters', '~> 1.0.7'

this will get your rake test to be in red and green, but it also brings up the backtrace. to get rid of all those extra backtrace logs add this to your gemfile then bundle:

gem 'mini_backtrace'

then in config/initializers/backtrace_silencers.rb add this line to cut all the extra rvm stuff

Rails.backtrace_cleaner.add_silencer { |line| line =~ /rvm/ }

hope that works for you, let me know if you need more details.

Germicide answered 16/11, 2014 at 17:15 Comment(0)
M
3

See https://github.com/tenderlove/purdytest/issues/1. It seems to be a known bug with the minitest version shipped with 1.9.2. For the others, adding

gem "minitest"

at the very top of your file did the trick.

Muumuu answered 7/7, 2011 at 11:46 Comment(0)
R
1

I currently use purdytest with 1.9.2

Regulable answered 6/9, 2011 at 8:36 Comment(0)
Q
1

As of late 2023 the accepted answer no longer works. The following, however, does.

require "minitest"
Minitest.load_plugins
require "minitest/pride"

Include the above code in e.g. test/test_helper.rb, or in wherever you put code common to all of your tests. This will give you test runs that look similar to this:

Running minitest with the pride test output colorizer

Quillen answered 13/11, 2023 at 21:10 Comment(0)
S
0

Try look at this post about using turn gem for nice looking and configurable output for minitest. http://rawonrails.blogspot.com/2012/01/better-minitest-output-with-turn-gem.html

Sg answered 12/1, 2012 at 2:2 Comment(0)
S
-1
$ gem install mynyml-redgreen --source http://gemcutter.org

# in your test file
require 'redgreen'

redgreen and turn work nicely in conjunction with each other, by the way

Silverside answered 7/10, 2011 at 4:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.