【问题标题】:Command line to run all examples in RSpec, including ones that are filtered out?在 RSpec 中运行所有示例的命令行,包括那些被过滤掉的示例?
【发布时间】:2012-10-12 14:49:29
【问题描述】:

有几个例子比较慢,过滤掉如下:

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

describe 'get averages but takes a long time', slow: true do
  it 'gets average foo' do
    ....
  end

  it 'gets average bar' do
    ...
  end
end

这很好用,不会运行缓慢的测试。

rspec

但是从命令行运行所有示例的 RSpec 命令是什么,包括被过滤掉的慢的?

【问题讨论】:

    标签: ruby rspec tdd


    【解决方案1】:

    如果你运行rspec --help,输出包括以下内容:

        -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
    

    您可以运行rspec --tag slow 来运行所有标记为慢的示例;但是,这并不能如您所愿运行所有示例。我认为没有一种简单的方法可以得到你想要的。 exclusion 过滤器专为您不想在命令行覆盖它的情况而设计(例如,基于 ruby​​ 版本或其他 - 强制运行不适用于您的规范是没有意义的红宝石版本)。您可以打开rspec core issue,以便我们讨论潜在的更改以添加您想要的内容。同时,您可以使用环境变量获取它:

    RSpec.configure do |c|
      c.filter_run_excluding slow: true unless ENV['ALL']
    end
    

    通过此设置,rspec 将运行除慢速以外的所有规范,ALL=1 rspec 将运行包括慢速在内的所有规范。

    【讨论】:

      【解决方案2】:

      排除慢速测试

      如果您希望 rake 默认排除慢速测试,Myron 的答案可能是您最好的选择。然而,这是一个更简单的解决方案,适用于大多数人。

      # Run all tests
      rspec
      
      # Run tests, excluding the ones marked slow
      rspec --tag ~slow
      

      我在开发时使用 guard 来运行我的测试。您可以告诉警卫在运行所有测试时排除慢速测试。这样,您可以在开发时只运行快速测试,并且可以在需要时使用rakerake --tag slow 运行完整套件。这也很棒,因为您的 CI 服务器可以运行您的完整套件,而无需知道要传入的特殊 ENV 变量。

      保护文件:

      guard :rspec, cli: '--drb', run_all: {cli: '--tag ~slow'} do
        ...
      end
      

      当您触发监视时,Guard 仍会运行缓慢的测试,例如在您编辑它时。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-20
        • 2011-06-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多