RSpec:获取将 运行 的所有示例的列表

RSpec: get list of all examples that will be run

我正在使用RSpec 3.

before(:suite) 块中想要获得(嵌套?)所有示例的列表,这些示例将在当前规范 运行 中成为 运行(即当给出路径时)这样我就可以扫描他们所有的元数据。

背景:希望能推断出水豚规格是否存在,如果是则启动前端服务器。

您不一定非要预先做,when_first_matching_example_defined hook 将 运行 指定的代码

RSpec.configure do |config|
  config.when_first_matching_example_defined(:capybara) do
    start_server
  end
end
RSpec.describe do
  describe 'non-capybara' do
    # when running this example group only, `start_server` won't be called
  end

  describe 'capybara', :capybara do
    # when RSpec gets to this example group, it will call `start_server`
  end
end