Joel AZEMAR
Joel AZEMAR

Reputation: 2526

Test only one it or describe with Rspec

On TestUnit you can launch one test in file with -n option

for example

require 'test_helper'

class UserTest < ActiveSupport::TestCase

  test "the truth" do
    assert true
  end

  test "the truth 2" do
    assert true
  end

end

You can execute only the test the truth

ruby -Itest test/unit/user_test.rb -n test_the_truth

The ouput

1 tests, 1 assertions, 0 failures, 0 errors, 0 skip

How can that with rspec ?

The command seem not work

rspec spec/models/user_spec.rb -e "User the truth"

Upvotes: 4

Views: 3117

Answers (3)

Waiting for Dev...
Waiting for Dev...

Reputation: 13029

At least in Rspec 2.11.1 you can use all of the following options:

** Filtering/tags **

In addition to the following options for selecting specific files, groups,
or examples, you can select a single example by appending the line number to
the filename:

  rspec path/to/a_spec.rb:37

-P, --pattern PATTERN            Load files matching pattern (default: "spec/**/*_spec.rb").
-e, --example STRING             Run examples whose full nested names include STRING (may be
                                   used more than once)
-l, --line_number LINE           Specify line number of an example or group (may be
                                   used more than once).
-t, --tag TAG[:VALUE]            Run examples with the specified tag, or exclude examples
                                 by adding ~ before the tag.
                                   - e.g. ~slow
                                   - TAG is always converted to a symbol

Upvotes: 1

hsgubert
hsgubert

Reputation: 2264

You can also select in which line is the test case you want to execute.

rspec spec/models/user_spec.rb:8

By passing any line inside the scope of the test case, only this test case will be executed. You can also use this to execute a whole context inside your test.

Upvotes: 2

Rob Davis
Rob Davis

Reputation: 15772

You didn't include the source of your spec, so it's hard to say where the problem is, but in general you can use the -e option to run a single example. Given this spec:

# spec/models/user_spec.rb
require 'spec_helper'
describe User do

  it "is true" do
    true.should be_true
  end

  describe "validation" do
    it "is also true" do
      true.should be_true
    end
  end

end

This command line:

rspec spec/models/user_spec.rb -e "User is true"

Will produce this output:

Run filtered including {:full_description=>/(?-mix:User\ is\ true)/}
.

Finished in 0.2088 seconds
1 example, 0 failures

And if you wanted to invoke the other example, the one nested inside the validation group, you'd use this:

rspec spec/models/user_spec.rb -e "User validation is also true"

Or to run all the examples in the validation group:

rspec spec/models/user_spec.rb -e "User validation"

Upvotes: 8

Related Questions