【问题标题】:How does one configure Cucumber / Aruba to use SimpleCov?如何配置 Cucumber / Aruba 以使用 SimpleCov?
【发布时间】:2013-11-26 02:56:39
【问题描述】:

Ruby-2.0.0p247 ActiveRecord-4.0.1 黄瓜 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 以报告进程外测试的代码覆盖率以及 cucumber 直接在进程内运行的部分的说明。

如何做到这一点?

【问题讨论】:

    标签: ruby cucumber aruba simplecov


    【解决方案1】:

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

    假设目录树如下:

    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
    

    然后在cucumber内部的二进制调用中,应该设置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

    【讨论】:

    • 当我返回该项目并为自己尝试此解决方案时,我会将其标记为答案。
    【解决方案2】:

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

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多