本文简要介绍ruby语言中 Coverage模块
的用法。
Coverage
为 Ruby 提供覆盖率测量函数。此函数是实验性的,因此这些 API 将来可能会更改。
警告:目前,仅支持process-global 覆盖测量。您无法测量per-thread 覆盖。
用法
-
需要“coverage”
-
需要或加载 Ruby 源文件
-
Coverage.result
将返回一个包含文件名作为键和覆盖数组作为值的哈希。覆盖数组为每一行提供解释器执行的行数。nil
值表示此行(如else
和end
之类的行)禁用覆盖。
例子
[foo.rb]
s = 0
10.times do |x|
s += x
end
if s == 45
p :ok
else
p :ng
end
[EOF]
require "coverage"
Coverage.start
require "foo.rb"
p Coverage.result #=> {"foo.rb"=>[1, 1, 10, nil, nil, 1, 1, nil, 0, nil]}
行 Coverage
如果在开始覆盖时未明确指定覆盖模式,则将运行行覆盖。它报告每行的行执行次数。
require "coverage"
Coverage.start(lines: true)
require "foo.rb"
p Coverage.result #=> {"foo.rb"=>{:lines=>[1, 1, 10, nil, nil, 1, 1, nil, 0, nil]}}
行覆盖结果的值是一个数组,其中包含每行执行了多少次。这个数组中的顺序很重要。例如,该数组中的第一项,位于索引 0 处,报告在运行覆盖时该文件的第 1 行执行了多少次(在此示例中为一次)。
nil
值表示此行(如 else
和 end
之类的行)的覆盖范围已禁用。
Oneshot 线条 Coverage
Oneshot 行覆盖在覆盖运行时跟踪并报告已执行的行。它不会报告一行被执行了多少次,只会报告它被执行了多少次。
require "coverage"
Coverage.start(oneshot_lines: true)
require "foo.rb"
p Coverage.result #=> {"foo.rb"=>{:oneshot_lines=>[1, 2, 3, 6, 7]}}
oneshot 行覆盖结果的值是一个包含已执行行号的数组。
分支 Coverage
分支覆盖率报告每个条件中的每个分支执行了多少次。
require "coverage"
Coverage.start(branches: true)
require "foo.rb"
p Coverage.result #=> {"foo.rb"=>{:branches=>{[:if, 0, 6, 0, 10, 3]=>{[:then, 1, 7, 2, 7, 7]=>1, [:else, 2, 9, 2, 9, 7]=>0}}}}
分支散列中的每个条目都是一个条件,其值是另一个散列,其中每个条目是该条件中的一个分支。值是方法执行的次数,键是关于分支的标识信息。
构成识别分支或条件的每个键的信息如下,从左到右:
-
分支或条件类型的标签。
-
唯一标识符。
-
它出现在文件中的起始行号。
-
它出现在文件中的起始列号。
-
它出现在文件中的结束行号。
-
它出现在文件中的结束列号。
方法 Coverage
方法覆盖率报告每个方法执行了多少次。
[foo_method.rb]
class Greeter
def greet
"welcome!"
end
end
def hello
"Hi"
end
hello()
Greeter.new.greet()
[EOF]
require "coverage"
Coverage.start(methods: true)
require "foo_method.rb"
p Coverage.result #=> {"foo_method.rb"=>{:methods=>{[Object, :hello, 7, 0, 9, 3]=>1, [Greeter, :greet, 2, 2, 4, 5]=>1}}}
方法哈希中的每个条目代表一个方法。此散列中的值是方法执行的次数,键是有关方法的标识信息。
构成识别方法的每个键的信息如下,从左到右:
-
班上。
-
方法名称。
-
方法出现在文件中的起始行号。
-
方法出现在文件中的起始列号。
-
方法出现在文件中的结束行号。
-
方法出现在文件中的结束列号。
所有 Coverage
模式
您还可以使用此快捷方式同时运行所有覆盖模式。请注意,运行所有覆盖模式不会同时运行行和 oneshot 行。这些模式不能同时运行。在这种情况下运行行覆盖,因为您仍然可以使用它来确定是否执行了行。
require "coverage"
Coverage.start(:all)
require "foo.rb"
p Coverage.result #=> {"foo.rb"=>{:lines=>[1, 1, 10, nil, nil, 1, 1, nil, 0, nil], :branches=>{[:if, 0, 6, 0, 10, 3]=>{[:then, 1, 7, 2, 7, 7]=>1, [:else, 2, 9, 2, 9, 7]=>0}}, :methods=>{}}}
相关用法
- Ruby Coverage.peek_result用法及代码示例
- Ruby Context.save_history=用法及代码示例
- Ruby Constants模块用法及代码示例
- Ruby Comparable.between?用法及代码示例
- Ruby Complex.arg用法及代码示例
- Ruby ComposedSet类用法及代码示例
- Ruby Continuation类用法及代码示例
- Ruby Complex.abs2用法及代码示例
- Ruby Context.echo_on_assignment?用法及代码示例
- Ruby Complex.angle用法及代码示例
- Ruby Complex.polar用法及代码示例
- Ruby Comment.extract_call_seq用法及代码示例
- Ruby Cookie.new用法及代码示例
- Ruby Comparable.clamp用法及代码示例
- Ruby Complex.to_f用法及代码示例
- Ruby Complex.cmp / numeric用法及代码示例
- Ruby Complex.conj用法及代码示例
- Ruby Converter.primitive_errinfo用法及代码示例
- Ruby Config.get_value用法及代码示例
- Ruby Context.echo?用法及代码示例
- Ruby Command.arguments用法及代码示例
- Ruby Complex类用法及代码示例
- Ruby Complex.infinite?用法及代码示例
- Ruby Complex.abs用法及代码示例
- Ruby Context.echo_on_assignment用法及代码示例
注:本文由纯净天空筛选整理自ruby-lang.org大神的英文原创作品 Coverage模块。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。