开发者

Test only one it or describe with Rspec

开发者 https://www.devze.com 2023-03-15 23:19 出处:网络
On TestUnit you can launch one test in file with -n option for example require \'test_helper\' class UserTest < ActiveSupport::TestCase

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"


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"


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.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消