【问题标题】:How do I run only specific tests in Rspec?如何在 Rspec 中仅运行特定测试?
【发布时间】:2011-07-01 11:29:21
【问题描述】:

我认为有一种方法可以只运行带有给定标签的测试。有人知道吗?

【问题讨论】:

    标签: ruby rspec


    【解决方案1】:

    查找文档并不容易,但您可以使用哈希标记示例。例如。

    # 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

    【讨论】:

    • 所以你不必去搜索,直接链接到zetetic的建议在这里(对于Rspec 2.12)relishapp.com/rspec/rspec-core/v/2-12/docs/command-line/…
    • 我们在我们的套件中添加了一个规范,以确保代码永远不会与焦点合并:仍然在源代码控制中。 gist.github.com/jwg2s/7361603
    • @jwg2s 我使用 git 钩子来阻止 :focus 的提交,这也可以防止像 'binding.pry, console.log` 等不受欢迎的东西潜入代码库。
    • @Otheus 不,我只是一个粉丝 :) 我真的很喜欢他们在 Relish 上所做的事情,但是 SO 刚刚推出了自己的文档功能,所以我们可能会看到一些竞争。
    • 也许你可以指出我实际描述 rspec 程序的用法和实际行为的文档的方式:) 因为 Relish 文档没有。
    【解决方案2】:

    您可以使用--example (or -e) option 运行所有包含特定字符串的测试:

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

    我最常使用那个。

    【讨论】:

      【解决方案3】:

      确保你的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
      

      【讨论】:

      • 在 rspec 3.5 中,它是 config.filter_run_when_matching,只需在示例中添加 :focus 即可工作
      • 如果 'focus: true' 被意外提交,尽管没有运行大部分测试,您的 CI 仍将通过。
      【解决方案4】:

      您也可以传递行号:rspec spec/my_spec.rb:75 - 行号可以指向单个规范或上下文/描述块(运行该块中的所有规范)

      【讨论】:

        【解决方案5】:

        您还可以将多个行号与冒号一起串起来:

        $ rspec ./spec/models/company_spec.rb:81:82:83:103
        

        输出:

        Run options: include {:locations=>{"./spec/models/company_spec.rb"=>[81, 82, 83, 103]}}
        

        【讨论】:

        • 这太黄金了! ??‍♂️
        【解决方案6】:

        从 RSpec 2.4 开始(我猜),您可以在 itspecifydescribecontext 前添加 fx

        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: trueconfig.run_all_when_everything_filtered = true

        【讨论】:

          【解决方案7】:

          在较新版本的 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

          【讨论】:

            【解决方案8】:

            您还可以运行默认具有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,则始终包含。
            【解决方案9】:

            您可以使用rspec spec/models/user_spec.rb -e "SomeContext won't run this" 运行。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2014-02-03
              • 2014-11-27
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多