In Rails 3.2, there are predefined tasks to run Test::Unit tests like rake test:units
, rake test:functionals
etc. But when you are developing, you may need to run a single test or tests from a file. None of the rake tasks provide this in Rails 3.2.
I like to show you how you can do this with zeus.
What’s this zeus?
Zeus is rails application preloader. It preload your rails environment so that you run rake tasks, server and console with in 1-2 seconds. It reloads itself when ever you make any changes to initializers or do a bundle. It will saves you from rails booting.
zeus test
is coupled with some handy hacks which will let you run your tests easily. You can run all tests in a file or a single test.
Run tests in a single file
You can run tests from a single file by just passing the file path to zeus test
Run a single test
Consider this as your test case.
1
2
3
4
5
6
7
8
9
10
# test/functional/some_controller_test.rb
class SomeControllerTest < ActionController::TestCase
def setup
end
test 'should create' do
# your test
end
end
Think that in the above case you need to run only should create. With zeus there are multiple ways to run single test.
#1
You can use pass the test case as underscored and prepend with test_ which makes test_should_create and pass to -n
option.
#2
Another similar method is passing the test case in double quotes to -n
option. Don’t forget to prepend test before test case.
#3
The third method felt more comfortable for me. Just append :<line_no of test case> to the file path.
Here 7 is the line number of test "should create"
.
In Rails 4, you can pass the filename to the rake tasks and Rails 4.1 will ship with spring preloader. But still I believe zeus will be my favorite to run tests.
Happy testing.