ruby - tests - rspec specific line

How do you run a single test/spec file in RSpec? (10)

I want to be able to run a single spec file's tests — for the one file I'm editing, for example. rake spec executes all the specs. My project is not a Rails project, so rake spec:doc doesn't work.

Don't know if this matters, but here is my directory structure.

./spec/db_spec.rb lets you do stuff like..

rake spec:user          #run spec/model/user_spec.rb (searches for user*_spec.rb)
rake test:users_c       #run test/functional/users_controller_test.rb
rake spec:user:token    #run the first spec in user_spec.rb that matches /token/
rake test:user:token    #run all tests in user_test.rb that match /token/
rake test:last
rake spec:last

Alternatively, have a look at autotest.

Running autotest in a command window will mean that the spec file will be executed whenever you save it. Also, it will be run whenever the file you are speccing is run.

For instance, if you have a model spec file called person_spec.rb, and a model file that it is speccing called person.rb, then whenever you save either of these files from your editor, the spec file will be executed.

I was having trouble getting any of these examples to work, maybe because the post is old and the commands have changed?

After some poking around I found this works:

rspec spec/models/user_spec.rb

That will run just the single file and provides useful output in the terminal.

If you installed rspec as a plugin rather than as a gem, then you won't have the spec executable.

At any rate, All you need to do is run the file using ruby. The rspec code is clever enough to run the tests for you.


ruby myclass_spec.rb

Or you can skip rake and use the 'rspec' command:

rspec path/to/spec/file.rb

In your case I think as long as your ./spec/db_spec.rb file includes the appropriate helpers, it should work fine.

If you're using an older version of rspec it is:

spec path/to/spec/file.rb

Ruby 1.9.2 and Rails 3 have an easy way to run one spec file:

  ruby -I spec spec/models/user_spec.rb


  • ruby command tends to be faster than the rake command
  • -I spec means "include the 'spec' directory when looking for files"
  • spec/models/user_spec.rb is the file we want to run.

This question is an old one, but it shows up at the top of Google when searching for how to run a single test. I don't know if it's a recent addition, but to run a single test out of a spec you can do the following:

rspec path/to/spec:<line number>

where -line number- is a line number that contains part of your test. For example, if you had a spec like:

2: it "should be awesome" do
3:   foo = 3
4:   foo.should eq(3)
5: end

Let's say it's saved in spec/models/foo_spec.rb. Then you would run:

rspec spec/models/foo_spec.rb:2

and it would just run that one spec. In fact, that number could be anything from 2 to 5.

Hope this helps!

To run all of your rspec files: rspec

note: you must be in the root of your project

To run one rspec file: rspec 'path_to/spec.rb'

note: replace 'path_to/spec.rb' with your path. Quotation marks optional.

To run one rspec test from one file: rspec 'path_to/spec.rb:7'

note: :7 is the line number where the test starts

from help (spec -h):

-l, --line LINE_NUMBER           Execute example group or example at given line.
                                 (does not work for dynamically generated examples)

Example: spec spec/runner_spec.rb -l 162