加快Chefspec运行

时间:2013-12-10 13:54:25

标签: rspec chef chefspec

我有很多食谱,他们经过了ChefSpec的严格测试。我有超过800个规范,每次提交代码之前运行它们都会成为问题,因为在我的机器上运行它们大约需要20分钟。这使得每个规格约为0.(6)s,这并不多,但却增加了很长时间。

是否有可能并行运行ChefSpec / RSpec测试或以某种方式提高速度?

1 个答案:

答案 0 :(得分:2)

2014年1月4日更新:现在在ChefSpec(> = 3.1.2)中实现了以下想法。请参阅Readme中的更快规范部分。

================================

这是blogpost摘录,其中详细介绍了此主题。

RSpec allows to extend modules with your own methods的想法是编写类似于 let 的方法,但它也会跨越示例缓存结果。在Chef项目的某处创建一个* spec_helper.rb *文件,并在其中添加以下行:

module SpecHelper
  @@cache = {}
  def shared( name, &block )
    location = ancestors.first.metadata[:example_group][:location]
    define_method( name ) do
      @@cache[location + name.to_s] ||= instance_eval( &block )
    end
  end

  def shared!( name, &block )
    shared name, &block
    before { __send__ name }
  end
end

RSpec.configure do |config|
  config.extend SpecHelper
end

来自 @@ cache 的值永远不会被删除,并且您可以对此块使用相同的名称,因此我还使用了该用法的位置,如下所示:“。/ cookbooks / my_cookbook /规格/ default_spec.rb:3" 。现在,在您的规范中将let( :chef_run )更改为shared( :chef_run )

describe "example::default" do
  shared( :chef_run ) { ChefSpec::ChefRunner.new.converge described_recipe }
  [...]
end

运行测试时,您现在还必须包含spec_helper.rb:

rspec --include ./relative/path/spec_helper.rb cookbooks/*/spec/*_spec.rb