[英]How do I run only specific tests in Rspec?
I think there's a way to run only tests with a given label.我认为有一种方法可以只运行具有给定标签的测试。 Anybody know?有人知道吗?
You can tag examples with :focus
hash attribute.您可以使用:focus
哈希属性标记示例。 For example,例如,
# spec/foo_spec.rb
RSpec.describe Foo do
it 'is never executed' do
raise "never reached"
end
it 'runs this spec', focus: true do
expect(1).to eq(1)
end
end
rspec --tag focus spec/foo_spec.rb
More info on GitHub .更多信息在GitHub 上。 (anyone with a better link, please advise) (谁有更好的链接,请指教)
(update) (更新)
RSpec is now superbly documented on relishapp.com . RSpec 现在在 relishapp.com 上有很好的记录。 See the --tag option section for details.有关详细信息,请参阅--tag 选项部分。
As of v2.6 this kind of tag can be expressed even more simply by including the configuration option treat_symbols_as_metadata_keys_with_true_values
, which allows you to do:从 v2.6 开始,通过包含配置选项treat_symbols_as_metadata_keys_with_true_values
可以更简单地表达这种标记,它允许您执行以下操作:
describe "Awesome feature", :awesome do
where :awesome
is treated as if it were :awesome => true
.其中:awesome
被视为:awesome => true
。
Also, see this answer for how to configure RSpec to automatically run 'focused' tests.此外,请参阅此答案以了解如何配置 RSpec 以自动运行“重点”测试。 This works especially well with Guard .这对Guard尤其有效。
You can run all tests that contain a specific string with --example (or -e) option :您可以使用--example(或 -e)选项运行包含特定字符串的所有测试:
rspec spec/models/user_spec.rb -e "User is admin"
I use that one the most.我用的最多的是那个。
Make sure RSpec is configured in your spec_helper.rb
to pay attention to focus
:确保在您的spec_helper.rb
中配置了 RSpec 以注意focus
:
RSpec.configure do |config|
config.filter_run focus: true
config.run_all_when_everything_filtered = true
end
Then in your specs, add focus: true
as an argument:然后在您的规范中,添加focus: true
作为参数:
it 'can do so and so', focus: true do
# This is the only test that will run
end
You can also focus tests by changing it
to fit
(or exclude tests with xit
), like so:您还可以通过将it
更改为fit
(或使用xit
排除测试)来集中测试,如下所示:
fit 'can do so and so' do
# This is the only test that will run
end
或者,您可以传递行号: rspec spec/my_spec.rb:75
- 行号可以指向单个规范或上下文/描述块(运行该块中的所有规范)
You can also string multiple line numbers together with colon :您还可以将多个行号与冒号一起串起来:
$ rspec ./spec/models/company_spec.rb:81:82:83:103
Output:输出:
Run options: include {:locations=>{"./spec/models/company_spec.rb"=>[81, 82, 83, 103]}}
As of RSpec 2.4 (I guess) you can prepend an f
or x
to it
, specify
, describe
and context
:从 RSpec 2.4 开始(我猜)你可以在it
前面加上f
或x
, specify
, describe
和context
:
fit 'run only this example' do ... end
xit 'do not run this example' do ... end
http://rdoc.info/github/rspec/rspec-core/RSpec/Core/ExampleGroup#fit-class_method http://rdoc.info/github/rspec/rspec-core/RSpec/Core/ExampleGroup#xit-class_method http://rdoc.info/github/rspec/rspec-core/RSpec/Core/ExampleGroup#fit-class_method http://rdoc.info/github/rspec/rspec-core/RSpec/Core/ExampleGroup#xit-class_method
Be sure to have config.filter_run focus: true
and config.run_all_when_everything_filtered = true
in your spec_helper.rb
.确保在您的spec_helper.rb
中有config.filter_run focus: true
和config.run_all_when_everything_filtered = true
。
In newer versions of RSpec, it's even easier to configure support fit
:在较新版本的 RSpec 中,配置 support fit
更加容易:
# spec_helper.rb
# PREFERRED
RSpec.configure do |c|
c.filter_run_when_matching :focus
end
# DEPRECATED
RSpec.configure do |c|
c.filter_run focus: true
c.run_all_when_everything_filtered = true
end
See:看:
https://relishapp.com/rspec/rspec-core/docs/filtering/filter-run-when-matching https://relishapp.com/rspec/rspec-core/docs/filtering/filter-run-when-matching
https://relishapp.com/rspec/rspec-core/v/3-7/docs/configuration/run-all-when-everything-filtered https://relishapp.com/rspec/rspec-core/v/3-7/docs/configuration/run-all-when-everything-filtered
Also you can run specs which have focus: true
by default您也可以运行具有focus: true
spec/spec_helper.rb规范/spec_helper.rb
RSpec.configure do |c|
c.filter_run focus: true
c.run_all_when_everything_filtered = true
end
Then simply run然后简单地运行
$ rspec
and only focused test will be run并且只会运行重点测试
then when you remove focus: true
all tests well be run again然后当您移开focus: true
所有测试都将再次运行
More information: https://www.relishapp.com/rspec/rspec-core/v/2-6/docs/filtering/inclusion-filters更多信息: https ://www.relishapp.com/rspec/rspec-core/v/2-6/docs/filtering/inclusion-filters
您可以作为rspec spec/models/user_spec.rb -e "SomeContext won't run this"
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.