Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a single test from a Rails test suite?

People also ask

How do you run a single test?

Use the go test -run flag to run a specific test. The flag is documented in the testing flags section of the go tool documentation: -run regexp Run only those tests and examples matching the regular expression.

How do I run a specific spec test?

File & Directory Names 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.

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.


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
.
.
.

In rails 5,

I used this way to run single test file(all the tests in one file)

rails test -n /TopicsControllerTest/ -v

look here https://stackoverflow.com/a/41183694/3626659