RSpec 2 的 RCov 无法正确检测覆盖范围? (不是铁轨!)

发布于 2024-12-27 11:02:02 字数 1534 浏览 3 评论 0原文

前言
我刚刚开始接触 Ruby,不仅尝试学习该语言,还尝试学习一些开发策略。作为初学者,我专注于测试和行为驱动开发。 (是的,我这样做是为了进行比较)

在我的小型软件项目中,我使用

  • UnitTest (TDD)
  • Cucumber (BDD)
  • Rspec(TDD 和 BDD)

在不同的地方,我遇到了 RCov 作为一种工具来告诉我有多少我正在测试的实际代码。

我在 Rakefile 中设置了以下 RakeTask,用于 UnitTests 的协方差分析:

desc "Run RCov to get coverage of UnitTests"
Rcov::RcovTask.new(:rcov_units) do |t|
  t.pattern = 'tests/**/tc_*.rb'
  t.verbose = true
  t.rcov_opts << "--html"
  t.rcov_opts << "--text-summary"
  t.output_dir = "coverage/tests"
end

这工作正常,我在 coverage/tests 中得到了一个漂亮的彩色 HTML 报告。

问题介绍
类似地,我为 RCov 编写了以下 RakeTasks,用于我的规范的覆盖率分析:

desc "Run RCov to get coverage of Specs"
Rcov::RcovTask.new(:rcov_spec) do |t|
  t.pattern = 'spec/**/*_spec.rb'
  t.verbose = true
  t.rcov_opts << "--html"
  t.rcov_opts << "--text-summary"
  t.output_dir = "coverage/spec"
end

问题定义
然而,coverage/spec 中生成的 HTML 报告看起来不完整,几乎失败。

没有一个被测试的方法体被标记为被覆盖并因此被标记为红色。不过,我 100% 确定它们是在规范内执行的。只有 def method_name(args)class ClassName 行标记为“绿色”。 (以及带有 attr_reader :instance_variable 的行)

我错过了什么吗?


$: ruby --version  
ruby 1.8.7 (2011-02-18 patchlevel 334) [x86_64-linux]

$: rspec --version  
2.8.0

$: rcov --version  
rcov 0.9.11 2010-02-28

$: rake --version  
rake, version 0.9.2

Preface
I've just started getting into Ruby and try to not only learn the language but also some development strategies. As kind of a beginner I'm concentrating on Test and Behaviour Driven Development. (yes, I'm doing both for comparison purposes)

With my small software project I'm using

  • UnitTest (TDD)
  • Cucumber (BDD)
  • Rspec (TDD and BDD)

On various places I encountered RCov as a tool for telling me how much of my actual code I'm really testing.

I set up the following RakeTask in my Rakefile for the covarage analysis of the UnitTests:

desc "Run RCov to get coverage of UnitTests"
Rcov::RcovTask.new(:rcov_units) do |t|
  t.pattern = 'tests/**/tc_*.rb'
  t.verbose = true
  t.rcov_opts << "--html"
  t.rcov_opts << "--text-summary"
  t.output_dir = "coverage/tests"
end

This works fine and I'm getting a nice coloured HTML report in coverage/tests.

Problem Introduction
Similar I wrote the following RakeTasks for RCov to be used for coverage analysis of my specs:

desc "Run RCov to get coverage of Specs"
Rcov::RcovTask.new(:rcov_spec) do |t|
  t.pattern = 'spec/**/*_spec.rb'
  t.verbose = true
  t.rcov_opts << "--html"
  t.rcov_opts << "--text-summary"
  t.output_dir = "coverage/spec"
end

Problem Definition
However, the generated HTML report in coverage/spec looks somehow incomplete and almost failed.

None of the tested method bodies are marked as covered and thus red. However, I'm 100% sure they are executed within the specs. Only the lines def method_name(args) and class ClassName are marked 'green'. (as well lines with attr_reader :instance_variable)

Am I missing something?


$: ruby --version  
ruby 1.8.7 (2011-02-18 patchlevel 334) [x86_64-linux]

$: rspec --version  
2.8.0

$: rcov --version  
rcov 0.9.11 2010-02-28

$: rake --version  
rake, version 0.9.2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

败给现实 2025-01-03 11:02:02

我也有类似的烦恼。
我 100% 确定当我在 RCov 中运行它们时,规范不会被执行。但当我禁用 RCov 时,它们就会这样做。

将 RSpec 降级到版本 2.6.0 对我有帮助。

I've got similar trouble.
I am 100% sure that specs are not executed when I run them win RCov. But they do when I disable RCov.

Downgrading RSpec to version 2.6.0 helped me.

暖风昔人 2025-01-03 11:02:02

确保您足够早地需要 rcov。

来自 http://rubydoc.info/github/relevance/rcov/master/Rcov/代码覆盖率分析器

请注意,在解析要分析的代码之前(即在加载或需要它之前),您必须要求“rcov”。您可以通过使用 -rrcov 命令行选项调用 ruby​​ 来做到这一点,或者只是:

Make sure that you are requiring rcov early enough.

From http://rubydoc.info/github/relevance/rcov/master/Rcov/CodeCoverageAnalyzer:

Note that you must require 'rcov' before the code you want to analyze is parsed (i.e. before it gets loaded or required). You can do that by either invoking ruby with the -rrcov command-line option or just:

淤浪 2025-01-03 11:02:02

我通过添加解决了这个问题:

RSpec::Core::Runner.autorun

到我的规范文件的底部;即使使用 rspec 2.9 也能工作

I fixed this by adding:

RSpec::Core::Runner.autorun

to the bottom of my spec file; worked even with rspec 2.9

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文