327

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.

./Rakefile
./lib
./lib/cushion.rb
./lib/cushion
./lib/cushion/doc.rb
./lib/cushion/db.rb
./spec
./spec/spec.opts
./spec/spec_helper.rb
./spec/db_spec.rb

15 Answers 15

446

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

bundle exec 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:

bundle exec spec path/to/spec/file.rb
Sign up to request clarification or add additional context in comments.

6 Comments

with the new rspec you have to use "rspec path/to/file.rb"
Maybe I'm doing it wrong, but I have to do "bundle exec rspec path/to/spec/file.rb"
If you're using Rails you'll probably want to execute your specs using Rake in order to ensure you are starting from a blank database.
If you want to run only one test you append the line number of the start of the test to the file. Example: rspec path/to/spec/file.rb:455
add f before the it so fit "expects something ... will only run the specific test
|
215

The raw invocation:

rake spec SPEC=spec/controllers/sessions_controller_spec.rb \
          SPEC_OPTS="-e \"should log in with cookie\""

Now figure out how to embed this into your editor.

7 Comments

As I said, I'm not in rails so there's no controllers. But yes, "rake spec SPEC=file.rb" works. Thanks! I'm working on the Emacs integration now.
Don't launch the rakefile if you can avoid it. It takes frickin ages
I changed the accepted answer, but the SPEC_OPTS param was helpful to me also.
Now that's what I call a good answer. Why isn't it marked as "Answer"?
Running spec directly is often the best choice, but there are many times when you have to run rake for various magic bits in a custom Rakefile
|
138

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:

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

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.

6 Comments

This is interesting, although it looks like it doesn't work once you refactor your spec into multiple files (i.e. you can only use it with the main _spec.rb file, but if none of your examples are in it, it will run nothing. Also if you try to use it for a file which isn't a top-level spec file, that will also run nothing.)
I don't quite follow. What do you mean by "top-level spec file?" My test suite is pretty much always divided across several files, and I can always invoke tests in a single file using this method. Do you mean having a file that requires your other spec files?
I can use this on any file at any level under the spec/ directory. I believe Trejkaz is incorrect.
This colon-notation did not work for me. I got: a No such file or directory error.
I use this all the time, but sometimes you want to run several tests from one file. Then you can type all examples like this: rspec spec/models/foo_spec.rb:2:12:49
|
68

You can also use the actual text of the *e*xample test case with -e !

So for:

it "shows the plane arrival time"

you can use

rspec path/to/spec/file.rb -e 'shows the plane arrival time'
./scripts/spec path/to/spec/file.rb -e 'shows the plane arrival time'

no need for rake here.

1 Comment

This is all you need, except that "spec" should be replaced by "rspec" as stated @bandhunt
19

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

1 Comment

In newer Rspec versions you append the line number after a colon : per the documentation and a popular answer.
18

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

Comments

13

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.

eg:

ruby myclass_spec.rb

1 Comment

Is it possible to run one test within the file like this?
12

http://github.com/grosser/single_test 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

1 Comment

It appears that the gem hasn't been maintained, last commit was 5 years ago!
10

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

  ruby -I spec spec/models/user_spec.rb

Explanation:

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

1 Comment

Can you run one test within that file with some extra options?
7

Although many great answers were written to this question, none of them uses the Rspec tags approach.

I use tags to run one or more specs in different files -- only those related to my current development task.

For example, I add the tag "dev" with the value "current":

it "creates an user", dev: :current do
  user = create(:user)
  expect(user.persisted?).to be_truthy
end

then I run

bundle exec rspec . --tag dev:current

Different tags/values can be set in individual specs or groups.

Comments

5

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.

1 Comment

This was mentioned in a comment in the accepted answer. Yes, I think this post is just so old that the command name has changed.
4

specky.vim

Comments

3

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.

Comments

3

Lets say, you're running test for creating todo. You can always run that specific todo spec code using the file crete_spec.rb file as below.

      rspec/spec/features/controller/spec_file_name.rb

   Example:

   Creating  rspec spec/features/todos/create_spec.rb
   Editing   rspec spec/features/todos/edit_spec.rb
   Deleting  rspec spec/features/todos/destroy_spec.rb

If you want to run all the specs in one single short.

   rspec 

If you want to run all the specs in a specific controller user this.

  rspec/spec/feaures/controller_name

  Example:   rspec/spec/features/todos

Hope it gives you more understanding!

Comments

0

And you can run specific line into your test file

rspec spec/models/model_spec.rb:47

1 Comment

This same answer was provided almost 10 years ago.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.