gpt4 book ai didi

ruby - 如何配置 Cucumber/Aruba 以使用 SimpleCov?

转载 作者:数据小太阳 更新时间:2023-10-29 08:32:19 24 4
gpt4 key购买 nike

Ruby-2.0.0p247ActiveRecord-4.0.1 cucumber 1.3.10Aruba-0.5.3SimpleCove-0.8.2

我们在一个仍然使用 ActiveRecord 的 NON-RAILS 项目中使用 Cucumber 和 Aruba。我们的 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 的运行命令调用外部 bin/文件时,步骤定义正常工作并且测试按预期完成,但代码覆盖率未与运行的其余部分合并。我正在寻找的是有关如何设置 simplecov 以报告进程外测试的代码覆盖率以及直接由 Cucumber 在进程内运行的部分的说明。

如何做到这一点?

最佳答案

我有一个与您相似的环境,这就是我如何让它工作的:

假设目录树如下:

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

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

它描述了二进制文件应该启动 simplecov。这很骇人听闻,但我将此 header 添加到我的二进制文件中(项目/bin/二进制文件):

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com