RSpec:如何获得`c.filter_run_excluding`排除的测试数量?

问题描述 投票:0回答:1

RSpec有一个很好的方法,通过在配置中使用filter_run_excluding来排除单个测试/示例或整个组,然后标记示例:

例:

RSpec.configure do |c|
  c.filter_run_excluding :xcode => true
end

RSpec.describe "something" do
  it "does one thing" do
  end

  it "does another thing that needs xcode", :xcode => true do
  end
end

“做一件事”将被检查, “做另一件事”不会。


我们使用这个例子来跳过一些测试,取决于运行测试的平台,将c.filter_run_excluding :skip => true包装在if块中:

If Mac, 
   no exclusions, 
if Ubuntu, 
   exclude tests that do something with Xcode.

现在,如果使用排除过滤器,传递的示例/测试的数量会更低,但是看到跳过的实际测试数量会很高兴。

有没有办法在测试运行期间获取此方法跳过的测试数量?

ruby rspec filter-run-excluding
1个回答
0
投票

根据我在https://groups.google.com/forum/#!topic/rspec/5qeKQr_7G7k与Myron Marston的讨论,回答我自己的问题:

RSpec没有提供一种方法来获取由于排除过滤器而未运行的示例数。但它提供了一种“跳过”示例的方法,这也将测试标记为“待定”:

500个例子,0个失败,20个未决

您像以前一样标记示例,然后添加:

# spec_helper.rb
require 'rbconfig'

RSpec.configure do |config|
  unless RbConfig::CONFIG['host_os'] =~ /darwin/
    config.define_derived_metadata(:xcode) do |meta|
      meta[:skip] = "Can only be run on OS X"
    end
  end
end

这将把:skip添加到在非macOS系统上运行时用:xcode标记的所有示例。

一个特例是触发before(:all)钩子的例子:

如果您的标签仅在特殊条件下有效(例如,在特定平台上运行测试时),您可以通过检查相同条件手动“跳过”执行挂钩:

before(:all) do
  skip "reason to skip" if should_skip?
  # rest of your hook logic
end

或者,您可以全局覆盖挂钩并再次访问通过标记创建的元数据:

module HookOverrides   
  def before(*args)
    super unless metadata[:skip]   
  end 
end

RSpec.configure do |c|   
  c.extend HookOverrides 
end

这里的约束:你必须标记钩子的父元素(很可能是通过describe的一个示例组),然后才能在钩子中获得元数据。你不能只标记个别的例子(因为它们在钩子后面),并且通过设置skip: false排除不被跳过的单个例子也不起作用。

就是这样。有关详细信息,请阅读https://groups.google.com/forum/#!topic/rspec/5qeKQr_7G7k上的整个邮件主题。

感谢Myron花时间帮助我,我非常感谢。

© www.soinside.com 2019 - 2024. All rights reserved.