如何配置Cucumber / Aruba使用SimpleCov?

时间:2013-11-26 02:56:39

标签: ruby cucumber aruba simplecov

红宝石2.0.0p247 ActiveRecord的-4.0.1 黄瓜1.3.10 阿鲁巴-0.5.3 SimpleCove-0.8.2

我们在NON-RAILS项目中使用Cucumber和Aruba,但仍使用ActiveRecord。我们的黄瓜功能可以在进程内和进程外执行代码。进程外代码使用与生产中相同的加载程序序列通过bin中的启动存根执行:

#!/usr/bin/env ruby
require 'bundler/setup'
Bundler.require

require 'pathname'
my_dir = Pathname.new(
  File.join( File.dirname(
    __FILE__ ), '../', 'lib/' ) ).realpath.to_s + '/'

require my_dir +  File.basename( __FILE__ )

HllThForexRssFetch::Main.new( ARGV ).execute
#EOF

我们的功能/ support / env.rb文件包含:

$ cat features/support/env.rb
# Must load and start simplecov before any application code
require 'simplecov'
SimpleCov.start do
  add_filter "/features/"
  add_filter "/libexec"
  add_filter "/lib/hll_active_record/"
  add_filter "/test/"
  add_filter "/tmp/"
end
SimpleCov.command_name( "Cucumber Features" )

# Do not use cucumber/rails in standalone projects
#require 'cucumber/rails' 

。 。

当我们的步骤定义通过aruba的run命令调用外部bin /文件时,步骤定义正常工作,测试按预期完成,但代码覆盖率未与运行的其余部分合并。我正在寻求的是如何设置simplecov以报告进程外测试的代码覆盖率以及黄瓜直接在进程中运行的部分的说明。

如何做到这一点?

2 个答案:

答案 0 :(得分:5)

我有一个类似于你的环境,这就是我的工作方式:

假设目录树如:

project
|- bin
|  |- binary
|- lib
|  |- ...
|- spec
|  |- ...
|- features
|  |- support
|  |  |- env.rb
|  |- ...

拳头检查此问题https://github.com/colszowka/simplecov/issues/234

它描述了二进制文件应该启动simplecov。这是hackish但我把这个标题添加到我的二进制文件(project / bin / binary):

if ENV['COVERAGE']
  require 'simplecov'

  # As described in the issue, every process must have an unique name:
  SimpleCov.command_name "binary #{Process.pid}"

  # When running with aruba simplecov was using /tmp/aruba as the root folder. 
  # This is to force using the project folder
  SimpleCov.root(File.join(File.expand_path(File.dirname(__FILE__)), '..'))

  SimpleCov.start do
    filters.clear

    # Because simplecov filters everything outside of the SimpleCov.root
    # This should be added, cf. 
    # https://github.com/colszowka/simplecov#default-root-filter-and-coverage-for-things-outside-of-it
    add_filter do |src|
      !(src.filename =~ /^#{SimpleCov.root}/) unless src.filename =~ /project/
    end

    # Ignoring test folders and tmp for Aruba
    add_filter '/spec/'
    add_filter '/test/'
    add_filter '/features/'
    add_filter '/tmp/'
  end
end

然后在调用黄瓜中的二进制文件时,应该设置COVERAGE环境变量。 在before子句的feature / support / env.rb中:

require 'simplecov'
SimpleCov.command_name 'Cucumber'

Before do
  # This is using the aruba helper, 
  # cf. https://github.com/cucumber/aruba/blob/master/lib/aruba/api.rb
  set_env('COVERAGE', 'true')
  # This could also be accomplished with the "I set the environment variables to:" step
end

如果您的环境中有两个框架(如本例中的RSpec和Cucumber),请不要忘记https://github.com/colszowka/simplecov#merging-results

答案 1 :(得分:0)

使用上面的命令,根据aruba的PID设置command_name 进程导致SimpleCov累积一个非常大的结果集和污染 旧跑的结果。

我最好设置命令名称,如下所示:

# As described in the issue, every process must have an unique name:
SimpleCov.command_name ARGV.join(' ')

这导致只包含具有相同参数的最新运行 结果。