【问题标题】:Guard: how to run specific tags from w/in Guard's console?Guard:如何在 Guard 的控制台中运行特定的标签?
【发布时间】:2013-09-01 07:51:50
【问题描述】:

我在我的 RSpec 测试套件中使用了 Spork 和 Guard。我排除了运行缓慢的测试:

RSpec.configure do |config|
  ...
  config.filter_run_excluding slow: true
  ...
end

然后,当我需要时,我在单独的 shell 中运行慢速测试:$ rspec . --tag slow

我想知道在 Guard 自动运行其测试的 same shell 中是否有运行慢速标签的快捷方式?

有控制台> 提示?在查看文档后,我发现输入 >. rspec . --tag slow 可以工作......但这只是比切换到另一个 shell 更冗长一些。似乎这将是一个相当普遍的要求。想法?

【问题讨论】:

    标签: ruby-on-rails rspec guard spork


    【解决方案1】:

    您可以定义组并在每个组中具有不同的 rspec 配置。

    将下面的代码附加到/Guardfile的内容中:

    scope group: :fast
    
    group :fast do
      guard 'rspec', cli: '--tag ~slow' do
        # code for watching
      end
    end
    
    group :slow do
      guard 'rspec', cli: '--tag slow' do
        # code for watching
      end
    end
    

    当您启动 Guard 时,它默认为快速规格:

    $ guard                                                                                                          
    21:56:35 - INFO - Guard::RSpec is running
    21:56:35 - INFO - Guard is now watching at '/Users/michi/testproject'
    [1] {Fast} guard(main)>
    

    按回车键将运行所有快速规范:

    22:02:00 - INFO - Run Fast
    22:02:00 - INFO - Running all specs
    Run options: exclude {:slow=>true}
    

    现在您可以通过按slow 来运行所有慢速:

    [2] {Fast} guard(main)> slow
    22:02:50 - INFO - Run Slow
    22:02:50 - INFO - Running all specs
    Run options: include {:slow=>true}
    

    您还可以将范围切换到慢速规格并按回车键全部运行:

    [3] {Fast} guard(main)> scope slow
    [4] {Slow} guard(main)>
    22:03:30 - INFO - Run Slow
    22:03:30 - INFO - Running all specs
    Run options: include {:slow=>true}
    

    希望有帮助!

    【讨论】:

    • 如果这可行,但还没有机会实施,那么这正是我正在寻找的!谢谢!
    • 当然行得通,上面的例子是从shell的一个真实例子中复制出来的:-)
    【解决方案2】:

    此代码将运行我们正在查看的文件中标记为“快速”的所有测试。

    guard 'rspec', :version => 2, :cli => "--tag ~fast" do
    # code for watching
    end
    

    您需要使用 cli 选项仅运行您需要的测试。

    【讨论】:

    • 那么运行这些测试的命令行触发器是什么(在您的示例中标记为fast)?
    • "--tag fast" 表示只运行快速测试。 “--tag ~fast”表示运行不快的测试。
    猜你喜欢
    • 2015-01-15
    • 2023-03-22
    • 2012-02-13
    • 1970-01-01
    • 1970-01-01
    • 2011-10-16
    • 2013-04-02
    • 2013-01-06
    • 1970-01-01
    相关资源
    最近更新 更多