【发布时间】:2011-07-01 11:29:21
【问题描述】:
我认为有一种方法可以只运行带有给定标签的测试。有人知道吗?
【问题讨论】:
我认为有一种方法可以只运行带有给定标签的测试。有人知道吗?
【问题讨论】:
查找文档并不容易,但您可以使用哈希标记示例。例如。
# spec/my_spec.rb
describe SomeContext do
it "won't run this" do
raise "never reached"
end
it "will run this", :focus => true do
1.should == 1
end
end
$ rspec --tag focus spec/my_spec.rb
有关GitHub 的更多信息。 (谁有更好的链接,请指教)
(更新)
RSpec 现在是superbly documented here。有关详细信息,请参阅--tag option 部分。
从 v2.6 开始,这种标签可以通过包含配置选项treat_symbols_as_metadata_keys_with_true_values 来更简单地表示,它允许您这样做:
describe "Awesome feature", :awesome do
其中:awesome 被视为:awesome => true。
另请参阅this answer,了解如何配置 RSpec 以自动运行“重点”测试。这特别适用于Guard。
【讨论】:
:focus 的提交,这也可以防止像 'binding.pry, console.log` 等不受欢迎的东西潜入代码库。
rspec 程序的用法和实际行为的文档的方式:) 因为 Relish 文档没有。
您可以使用--example (or -e) option 运行所有包含特定字符串的测试:
rspec spec/models/user_spec.rb -e "User is admin"
我最常使用那个。
【讨论】:
确保你的spec_helper.rb中配置了RSpec,注意focus:
RSpec.configure do |config|
config.filter_run focus: true
config.run_all_when_everything_filtered = true
end
然后在您的规范中,添加 focus: true 作为参数:
it 'can do so and so', focus: true do
# This is the only test that will run
end
您还可以通过将it 更改为fit(或使用xit 排除测试)来集中测试,如下所示:
fit 'can do so and so' do
# This is the only test that will run
end
【讨论】:
config.filter_run_when_matching,只需在示例中添加 :focus 即可工作
您也可以传递行号:rspec spec/my_spec.rb:75 - 行号可以指向单个规范或上下文/描述块(运行该块中的所有规范)
【讨论】:
您还可以将多个行号与冒号一起串起来:
$ rspec ./spec/models/company_spec.rb:81:82:83:103
输出:
Run options: include {:locations=>{"./spec/models/company_spec.rb"=>[81, 82, 83, 103]}}
【讨论】:
从 RSpec 2.4 开始(我猜),您可以在 it、specify、describe 和 context 前添加 f 或 x:
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
确保您的spec_helper.rb 中有config.filter_run focus: true 和config.run_all_when_everything_filtered = true。
【讨论】:
在较新版本的 RSpec 中,配置支持更加容易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
见:
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
【讨论】:
您还可以运行默认具有focus: true 的规范
spec/spec_helper.rb
RSpec.configure do |c|
c.filter_run focus: true
c.run_all_when_everything_filtered = true
end
然后直接运行
$ rspec
只会运行重点测试
那么当您删除 focus: true 时,所有测试都会再次运行
更多信息:https://www.relishapp.com/rspec/rspec-core/v/2-6/docs/filtering/inclusion-filters
【讨论】:
spec/spec_helper.rb 吗?或者只有在没有选项的情况下?为什么测试模块有require 'spec_helber',而没有上面的代码通过指定文件来消除运行单个测试的可能性?我找不到这方面的任何文档。
spec_helper.rb 如果您在项目根目录中的.rspec 中有--require spec_helper,则始终包含。
您可以使用rspec spec/models/user_spec.rb -e "SomeContext won't run this" 运行。
【讨论】: