2

Ruby-2.0.0p247 ActiveRecord-4.0.1 Cucumber 1.3.10 Aruba-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

我们的 features/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 的运行命令调用外部 bin/文件时,步骤定义正常工作并且测试按预期完成,但代码覆盖率没有与运行的其余部分合并。我正在寻找的是有关如何设置 simplecov 以报告进程外测试的代码覆盖率以及黄瓜直接在进程内运行的部分的说明。

如何做到这一点?

4

2 回答 2

5

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

假设目录树如下:

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

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

它描述了二进制文件应该以 simplecov 开头。这很 hackish,但我将此标头添加到我的二进制文件(项目/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

于 2013-12-10T21:18:01.790 回答
0

使用上述方法,根据 aruba 进程的 PID 设置 command_name 会导致 SimpleCov 累积非常大的结果集并使用旧运行污染结果。

我最好将命令名称设置如下:

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

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

于 2014-02-25T15:34:11.033 回答