Ruby-2.0.0p247 ActiveRecord-4.0.1 cucumber 1.3.10 阿鲁巴-0.5.3 SimpleCove-0.8.2
我们在一个非 RAILS 项目中将 Cucumber 与 Aruba 结合使用,但仍然使用 ActiveRecord。我们的 Cucumber 功能可以在进程内和进程外执行代码。进程外代码使用与生产中相同的加载程序序列通过 bin 中的启动 stub 执行:
#!/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 的 run 命令调用外部 bin/文件时,步骤定义正常工作并且测试按预期完成,但代码覆盖率不会与运行的其余部分合并。我正在寻找的是如何设置 simplecov 来报告进程外测试的代码覆盖率以及直接由 cucumber 在进程内运行的部分的说明。
如何做到这一点?
最佳答案
我有一个与你类似的环境,这就是我的工作方式:
假设目录树如下:
project
|- bin
| |- binary
|- lib
| |- ...
|- spec
| |- ...
|- features
| |- support
| | |- env.rb
| |- ...
先检查一下这个问题 https://github.com/colszowka/simplecov/issues/234
它描述了二进制文件应该启动 simplecov。这是很黑客的,但我将此 header 添加到我的二进制文件(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
然后在调用 cucumber 内的二进制文件时,应设置 COVERAGE 环境变量。 在 feature/support/env.rb 的 before 子句中:
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
关于ruby - 如何配置 Cucumber/Aruba 来使用 SimpleCov?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20207667/