Running Ruby unit tests with Rake
Use Rake::TestTask http://rake.rubyforge.org/classes/Rake/TestTask.html . Put this into your Rake file and then run rake test
:
require 'rake/testtask'
Rake::TestTask.new do |t|
t.libs << "test"
t.test_files = FileList['test/test*.rb']
t.verbose = true
end
How do I execute a single test using Ruby test/unit?
you can pass the -n option on the command line to run a single test:
ruby my_test.rb -n test_my_method
where 'test_my_method' is the name of the test method you would like to run.
How to run a single test from a Rails test suite?
NOTE: This doesn't run the test via rake
. So any code you have in Rakefile
will NOT get executed.
To run a single test, use the following command from your rails project's main directory:
ruby -I test test/unit/my_model_test.rb -n test_name
This runs a single test named "name", defined in the MyModelTest class in the specified file. The test_name is formed by taking the test name, prepending it with the word "test", then separating the words with underscores. For example:
class MyModelTest < ActiveSupport::TestCase
test 'valid with good attributes' do
# do whatever you do
end
test 'invalid with bad attributes' do
# do whatever you do
end
end
You can run both tests via:
ruby -I test test/unit/my_model_test.rb
and just the second test via
ruby -I test test/unit/my_model_test.rb -n test_invalid_with_bad_attributes
Test a specific group of testcases with rake testtask
I've figured out how to associate the different testtask with different rake task. The trick is adding a task name while creating a new testtask. Like this,
Rake::TestTask.new(:default) do |t|
t.test_files = FileList['test/test_*.rb']
end
Rake::TestTask.new(:internet) do |t|
t.test_files = FileList['test/itest_*.rb']
end
desc "No internet connection required"
task :default => :test
desc "Needs internet connection"
task :internet => :test
So rake internet
will just run the test cases that need internet connections.
How to run multiple Rails unit tests at once
I ended up hacking this into my RakeFile myself like so:
Rake::TestTask.new(:fast) do |t|
files = if ENV['TEST_FILES']
ENV['TEST_FILES'].split(',')
else
FileList["test/unit/**/*_test.rb", "test/functional/**/*_test.rb", "test/integration/**/*_test.rb"]
end
t.libs << 'test'
t.verbose = true
t.test_files = files
end
Rake::Task['test:fast'].comment = "Runs unit/functional/integration tests (or a list of files in TEST_FILES) in one block"
Then I whipped up this bash function that allows you to call rt
with an arbitrary list of test files. If there's just one file it runs it as ruby directly (this saves 8 seconds for my 50k loc app), otherwise it runs the rake task.
function rt {
if [ $# -le 1 ] ; then
ruby -Itest $1
else
test_files = ""
while [ "$1" != "" ]; do
if [ "$test_files" == "" ]; then
test_files=$1
else
test_files="$test_files,$1"
fi
shift
done
rake test:fast TEST_FILES=$test_files
fi
}
Run all models tests using rake task
The pattern you are looking for is "test/models/**/*_test.rb". The "**" will match subdirectories as well.
If you are using minitest-rails then you have lots of tasks added for you. To run all Model tests run:
rake minitest:models
To see all the rake tasks creates for you, run:
rake -T
Related Topics
Switching Between Web and Touch Interfaces on Facebook Login Using Omniauth and Rails 3
Testing Whether String Starts with or End with Another String
How to Reflect in the Database a New Belongs_To and Has_Many Relationship in Ruby on Rails
Cucumber Not Showing Coloured Output in Windows
How to Call Applicationcontroller Methods from Applicationhelper
Creating an Md5 Hash of a Number, String, Array, or Hash in Ruby
Converting an Array of Keys and an Array of Values into a Hash in Ruby
Ruby on Rails, Including a Module with Arguments
How to Run a Ruby Script Within Bundler Context
Bundle Install Could Not Fetch Specs from Https://Rubygems.Org/
How to Force Ruby to Show a Full Stack Trace
How to Run Ruby Tasks That Use My Rails Models
Shared Variable Among Ruby Processes
Convert JSON String to JSON Array in Rails
Calling/Applying Lambda VS. Function Call - the Syntax in Ruby Is Different. Why
What Are the Ruby's Object#Taint and Object#Trust Methods
How to Sort a Hash by Value in Descending Order and Output a Hash in Ruby