How to color unit tests with lib minitest or Test:Unit?
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
Can minitest do something like rspec --color --format doc?
"How to color unit tests with lib minitest or Test:Unit? is a similar question. Several options are provided there:
- pride
- turn
purdytest
minitest-reporters
Creating a test suite using Ruby minitest
There is no Test::Unit::TestSuite
in minitest. You have several options, assuming your tests look something like this:
require 'minitest/unit'
require 'minitest/autorun'
class FSSessionTest < MiniTest::Unit::TestCase
def test_the_truth
assert true
end
end
The vital part here is require 'minitest/autorun'
which uses at_exit
to run all tests it can find, just before the enclosing script exits. I find this to be the easiest way for running my test suites.
Run tests with Rake
For example, you can create a Rakefile
using Rake::TestTask
which runs all the tests in your test/
directory:
require 'rake'
require 'rake/testtask'
Rake::TestTask.new do |t|
t.pattern = 'tests/**/*_test.rb'
end
Run the tests with
$ rake test
Require tests in a Ruby file
If you frequently only need certain tests, you can also write a test script, something like
require './tests/fs_session_test'
require './tests/resource_test'
require './tests/rest_session_test'
require './tests/server_test'
You could also include require 'minitest/autorun'
at the top of this file to ensure, the tests are run, but i do this at the top of every test file, anyway. Run the suite with
$ ruby test.rb
Result
Both methods give you the same output, for example something like
Run options: --seed 5559
# Running tests:
....
Finished tests in 0.001909s, 2095.3379 tests/s, 2095.3379 assertions/s.
4 tests, 4 assertions, 0 failures, 0 errors, 0 skips
Because mintiest makes use of at_exit
, there is really no need to group the tests before you run them. You never get the output of only one test. Unless, of course you run a test on its own, for example with
$ ruby tests/fs_session_test.rb
Run options: --seed 43007
# Running tests:
.
Finished tests in 0.000672s, 1488.0952 tests/s, 1488.0952 assertions/s.
1 tests, 1 assertions, 0 failures, 0 errors, 0 skips
Domain object extraction with MiniTest
The answer to your first question is "no, not correct". Minitest is not a "drop-in replacement" for Test::Unit. Minitest provides a simpler implementation for most of Test::Unit, but not all of it. In Ruby 1.9 the Test::Unit library is built on top of Minitest's TestCase class, and fills the gaps between the two libraries.
Minitest provides two modes for writing tests: the classic TestCase approach, and a separate Spec DSL. Like Test::Unit, the Spec DSL is built on top of Minitest's TestCase. So while 1.9's Test::Unit and Minitest's Spec DSL are both are built on Minitest's TestCase, you can't use the Spec DSL in Test::Unit because its not part of its ancestry.
The answer to your second question is you cannot avoid test_helper.rb if you want to use ActiveSupport::TestCase. You will need to require minitest/autorun and have your test class inherit from MiniTest::Unit::TestCase.
Using the turn gem with Test::Unit
gem 'redgreen'
worked great for me (on Ruby 1.9.2 and Rails 3.0.7) with Test::Unit.
Add the above to your Gemfile.
On RubyGems.
How to include unit tests in a ruby module?
Personally I've never heard of anyone trying to do this in Ruby. It's definitely not a standard practice. That said you may be able to leverage this trick:
if __FILE__ == $0
# Do something.. run tests, call a method, etc. We're direct.
end
The code in the if
block will only execute if the file is executed directly, not if it's required by another library or application.
More ruby tricks here: http://www.rubyinside.com/21-ruby-tricks-902.html
Rails minitest devise error
Only include the Devise helpers in your controller tests. Your test_helper.rb
file should look like this:
ENV["RAILS_ENV"] = "test"
require File.expand_path("../../config/environment", __FILE__)
require "rails/test_help"
# To add Capybara feature tests add `gem "minitest-rails-capybara"`
# to the test group in the Gemfile and uncomment the following:
#require "minitest/rails/capybara"
# Uncomment for awesome colorful output
#require "minitest/pride"
require 'minitest/autorun'
class ActiveSupport::TestCase
# Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
fixtures :all
# Add more helper methods to be used by all tests here...
end
class ActionController::TestCase
include Devise::TestHelpers
end
Empty test suite - Test-Unit RubyMine
To solve it I firstly restructured my project in the following way
/lib
greed.rb
/tests
test_greed.rb
Gemfile
Then I moved all of my tests to the new file
require './lib/greed.rb'
require 'test/unit'
class Test_Greed < Test::Unit::TestCase
# code for the tests
end
Finally I went to File/Setting/Project/Project Structure
and marked the tests folder as Test
(this should change the color of the folder icon).
Now I can run the tests using the testing framework
Related Topics
Example of a Prepared Insert Statement Using Ruby Pg Gem
How to Properly Chain Custom Methods in Ruby
Ruby, Run Linux Commands One by One, by Ssh and Log Everything
How to Push Keys and Values into an Empty Hash W/ Ruby
Good Explanation of Ruby Object Model -- Mainly, 'Classes Are Objects'
How to Set the Rails Environment for My Somewhat Stand Alone Ruby Script
How Does Ruby Handle Bytes/Binary
Rails - Paperclip Validating Attachment Size When It Shouldn't Be
How to Rescue Timeout Issues (Ruby, Rails)
"Autotest/Rails [...] Doesn't [...] Exist. Aborting"
Installing Native Ruby Extensions on Windows for Jekyll
Sinatra and Session Variables Which Are Not Being Set
Accessing One Controller Variable in Another Controller in Rails
How to Use Rails 5.1.0 and Jquery