用于在RSpec中运行所有示例的命令行,包括被过滤掉的示例?

时间:2012-10-12 14:49:29

标签: ruby rspec tdd

有几个缓慢的例子,按如下方式过滤掉:

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命令是什么,包括被过滤掉的慢速命令?

2 个答案:

答案 0 :(得分:24)

如果您运行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将运行所有规格,包括慢速规格。

答案 1 :(得分:7)

排除慢速测试

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

# Run all tests
rspec

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

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

<强> Guardfile:

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

当您为其触发监视时,Guard仍然会进行慢速测试。就像您正在编辑它时一样。