How to run a single test from a Rails test suite? - ruby-on-rails

How can I run a single test from a Rails test suite?
rake test ANYTHING seems to not help.

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

Run a test file:
rake test TEST=tests/functional/accounts_test.rb
Run a single test in a test file:
rake test TEST=tests/functional/accounts_test.rb TESTOPTS="-n /paid accounts/"
(From #Puhlze 's comment.)

For rails 5:
rails test test/models/my_model.rb

Thanks to #James, the answer seems to be:
rails test test/models/my_model.rb:22
Assuming 22 is the line number of the given test. According to rails help:
$ rails test --help
You can run a single test by appending a line number to a filename:
bin/rails test test/models/user_test.rb:27
Also, please note that your test should inherit from ActionDispatch::IntegrationTest for this to work (That was my mistake):
class NexApiTest < ActionDispatch::IntegrationTest
.
.
.

Rails 5
I used this way to run single test file (all the tests in one file)
rails test -n /TopicsControllerTest/ -v
Another option is to use the line number (which is printed below a failing test):
rails test test/model/my_model.rb:15

In my situation for rake only works TESTOPTS="-n='/your_test_name/'":
bundle exec rake test TEST=test/system/example_test.rb TESTOPTS="-n='/your_test_name/'"

To run a single test in the actual Rails suite:
bundle exec ruby -I"railties/test" actionpack/test/template/form_options_helper_test.rb

That was a silly midnight question of mine. Rails kindly prints the command it is executing upon rake test. The rest is a cut and paste exercise.
~/projects/rails/actionpack (my2.3.4)$ ruby -I"lib:test" "/usr/lib/ruby/gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb" "test/controller/base_test.rb"

The best way is to look directly into the guides: http://guides.rubyonrails.org/contributing_to_ruby_on_rails.html#running-tests
cd actionmailer
bundle exec ruby -w -Itest test/mail_layout_test.rb -n test_explicit_class_layout

If you want to run a single test, you can just run them as a regular Ruby script
ruby actionmailer/test/mail_layout_test.rb
You can also run a whole suite (eg. ActiveRecord or ActionMailer) by cd-ing into the directory and running rake test inside there.

To re-run a test that just failed, copy-n-paste the failed test name into
rails test -n [test-name]
EXAMPLE
When your test suite reports this:
> rails test
...
Error:
PlayersControllerTest#test_should_show_player:
ActionView::Template::Error: no implicit conversion from nil to integer
you rerun the failing test with this:
rails test -n PlayersControllerTest#test_should_show_player

If rake is running MiniTest, the option is --name instead of -n.
rake test TEST=test/unit/progress_test.rb TESTOPTS="--name=testCreate"

First, access the folder of the lib you want to test(this is important) and then run:
~/Projects/rails/actionview (master)$ ruby -I test test/template/number_helper_test.rb

Rails folder
bundle install
bundle exec ruby -I"activerecord/test" activerecord/test/cases/relation/where_test.rb
Note you need to load appropriate folder: "activerecord/test" (where you have test)

Related

How to run a custom list of minitest tests in Rails

Say I have a file containing name of test files to run tests from and It can contain specific test names too. If test file contains that specific test, run only that test from the file containing the test and run all tests from other test files.
I use Codebuild to run tests for our application but Codebuild does not provide a way to run only specific tests. So I am replacing bin/rails test command on codebuild with our custom rake task. That rake task will check for a file in our system containing list of tests to run and If it finds the file it run only those tests other normal bin/rails test
You could copy how rails is already defining their tasks like test:system here:
namespace :test do
desc "Run tests from file"
task from_file: "test:prepare" do
$: << "test"
Rails::TestUnit::Runner.rake_run(
File.read(Rails.root.join("tests_to_run.txt")).lines.map(&:chomp)
)
end
end
$ bundle exec rails test:from_file

Shortcut to run all rails tests?

I have to type
bundle exec rspec spec lib/crucible_kit/spec
every time I want to run all 700 of my rspec tests for my rails application. Is there anyway I could shorten down this to just typing "rr" to run all tests?
If so, where would I put this file in my rails application? And would I be able to push it to git branch so my teammates can use it?
Enter the code below in your command-line.
alias rr="bundle exec rspec"
It will be appended to this file ~/.bash_rc

Rails run all tests except one

I want to run all my unit tests on my ruby on rails project except one. This is because i have pdf generation tests that don't work when ran by travic CI. So on travis I want to run all tests except the pdf generation tests. Is there any check i can do? Is there a way to check if the application is running locally or on travis CI? Both are test environments.
Here is my travis.yml
language: ruby
rvm:
- '2.1.5'
script:
- 'bundle exec rake'
- 'bundle exec rubocop'
- 'bundle exec rails_best_practices'
You can add this to your travis.yml
env:
- SKIP_PDF_TESTS=true
and the modify your test to
either test or skip something
def the_pdf_test
if ENV['SKIP_PDF_TEST']
skip("the pdf test")
else
... the actual test
end
end
You can skip a test in minitest using MiniTest::Assertions#skip
From inside your test, just call the skip() method, like this:
skip("Its generating PDF")

How do you write a task to run tests in Rails 3?

I would like to write rake tasks to customize tests. For example, to run unit tests, I created a file with the following code and saved it as lib/tasks/test.rake:
task :do_unit_tests do
cd #{Rails.root}
rake test:units
end
Running rake do_unit_tests throws an error: can't convert Hash into String.
Working in Rails 3.0.7 and using built-in unit test framework.
Thanks.
There is no need to cd. You can simply...
task :do_unit_tests do
Rake::Task['test:units'].invoke
end
But if you really want to cd, that's how you call shell instructions:
task :do_unit_tests do
sh "cd #{Rails.root}"
Rake::Task['test:units'].invoke
end
Well, in fact there is a shorter version. The cd instruction have a special alias as Chris mentioned in the other answer, so you can just...
task :do_unit_tests do
cd Rails.root
Rake::Task['test:units'].invoke
end
If you want to go further, I recommend Jason Seifer's Rake Tutorial and Martin Fowler's Using the Rake Build Language articles. Both are great.
You're trying to interpolate a value that's not in a string, and you're also treating rake test:units like it were a method call with arguments, which it's not.
Change the cd line so you're calling the method with the value of Rails.root, and change the second line to be a shell instruction.
task :do_unit_tests do
cd Rails.root
`rake test:units`
end

running single rails unit/functional test

As title.
ruby test/functionals/whatevertest.rb doesn't work, that requires me to replace all require 'test_helper' to require File.dirname(__FILE__) + '/../test_helper'. For some reason most of those test templates have such issue, so I rather to see if there is a hack I could get around it.
The following answer is based on: How to run single test from rails test suite? (stackoverflow)
But very briefly, here's the answer:
ruby -I test test/functional/whatevertest.rb
For a specific functional test, run:
ruby -I test test/functional/whatevertest.rb -n test_should_get_index
Just put underscores in places of spaces in test names (as above), or quote the title as follows:
ruby -I test test/functional/whatevertest.rb -n 'test should get index'
Note that for unit tests just replace functional with unit in the examples above. And if you're using bundler to manage your application's gem dependencies, you'll have to execute the tests with bundle exec as follows:
bundle exec ruby -I test test/unit/specific_model_test.rb
bundle exec ruby -I test test/unit/specific_model_test.rb -n test_divide_by_zero
bundle exec ruby -I test test/unit/specific_model_test.rb -n 'test divide by zero'
Most importantly, note that the argument to the -n switch is the name of the test, and the word "test" prepended to it, with spaces or underscores depending on whether you're quoting the name or not. The reason is that test is a convenience method. The following two methods are equivalent:
test "should get high" do
assert true
end
def test_should_get_high
assert true
end
...and can be executed as either of the following (they are equivalent):
bundle exec ruby -I test test/integration/misc_test.rb -n 'test should get high'
bundle exec ruby -I test test/integration/misc_test.rb -n test_should_get_high
Try this:
ruby -Ilib:test test/functionals/whatevertest.rb
On Linux? why not try (cd test && ruby functionals/whatevertest.rb). Note, the parentheses are important as otherwise your current directory will change to the subdirectory. What it does is fork another shell, change to the subdirectory in it, and run the test.
If you are on Rails 4, then rake supports file / directory arguments. Example:
rake test test/unit/user_test.rb
rake test test/unit
The answer for the title question would be:
ruby unit/post_test.rb -n selected_test # use to run only one selected test
but for the body of the question tvanfosson gave a good answer.
After spending endless hours on this, I finally found the solution (in Rails 3.0.7) :
ruby -I test test/functional/users_controller_test.rb -n "/the_test_name/"
Note, this only works with underbars (_) in the command. It does not work with spaces!
Define the test with spaces as:
test "the test name" do
This solution uses pattern matching, so you can use part of the test name. If the test is "should do foo", then either of the following will work as well.
ruby -I test test/functional/alerts_controller_test.rb -n "/foo/"
ruby -I test test/functional/alerts_controller_test.rb -n "/do_foo/"
The following (with spaces) will not work:
ruby -I test test/functional/alerts_controller_test.rb -n "/do foo/"
most conventional method for 2 and 3 is:
ruby -I test test/functional/your_test_file.rb

Resources