Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 411
William Yeung Avatar asked Nov 08 '08 04:11

William Yeung


People also ask

How do you run unit tests in Rails?

We can run all of our tests at once by using the bin/rails test command. Or we can run a single test file by passing the bin/rails test command the filename containing the test cases. This will run all test methods from the test case.

How do you run a Minitest?

To run a Minitest test, the only setup you really need is to require the autorun file at the beginning of a test file: require 'minitest/autorun' . This is good if you'd like to keep the code small. A better way to get started with Minitest is to have Bundler create a template project for you.

How do I run a specific test in RSpec?

Running tests by their file or directory names is the most familiar way to run tests with RSpec. RSpec can take a file name or directory name and run the file or the contents of the directory. So you can do: rspec spec/jobs to run the tests found in the jobs directory.

What is unit test Rails?

They make sure that a section of an application, or a “unit”, is behaving as intended. In a Rails context, unit tests are what you use to test your models. Although it is possible in Rails to run all tests simultaneously, each unit test case should be tested independently to isolate issues that may arise.


1 Answers

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 
like image 112
user664833 Avatar answered Sep 24 '22 11:09

user664833