本文介绍了具有多个应用程序的SimpleCov-或简而言之,Simplecov如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置SimpleCov为3个应用程序生成报告,这些应用程序共享来自本地gem的大多数代码(模型,控制器),但是每个应用程序使用的代码规范都位于每个./spec中,而不是

I'm trying to setup SimpleCov to generate reports for 3 applications that share most of their code(models, controllers) from a local gem but the specs for the code that each app uses are inside each ./spec and not on the gem itself.

举一个更清晰的例子。当我在app_1内运行使用本地gem共享模型的bundle exec rspec spec时,我想获取(准确)此app_1在./spec内的所有规格的报告。

For a clearer example. When i run bundle exec rspec spec inside app_1 that uses the shared models from the local gem I want to get(accurate) reports for all the specs that this app_1 has inside ./spec.

本地gem在命名空间中还具有一些专门属于app_2的模型,因此当我在app_1内运行测试套件时,我想跳过这些文件的报告。

The local gem also has some models that belong exclusively for app_2, inside a namespace, so i want to skip the report for those files when i run the test suite inside app_1.

我正在尝试通过app_1 / spec / spec_helper中的以下代码来实现此目的。

I'm trying to achieve this with something like the following code in app_1/spec/spec_helper.

# This couple of lines are needed to generate report for the models, etc. inside the local gem.
SimpleCov.adapters.delete(:root_filter)
SimpleCov.filters.clear

SimpleCov.adapters.define 'my_filter' do
  root = SimpleCov.root.split("/")
  root.pop
  add_filter do |src|
    !(src.filename =~ /^#{root.join("/")}/)
  end

  add_filter "/app_2_namespace/"
end

if ENV["COVERAGE"] == "true"
  SimpleCov.start 'rails'
end

此方法有效,直到出现一些问题为止。

This works, until some questions begin to arise.

为什么我在gem内但模型在app_2内(我在app_1内运行规范)的模型覆盖率达到85%。

Why i get a 85% coverage for a model that's inside the gem but the spec is inside app_2(I'm running the spec inside app_1).

第一次出现问题是,当我尝试改进该模型时,我单击了报告,然后发现发现了哪些行,并试图修复它们,以便为他们在app_2 / spec / namespace / my_model_spec.rb上。

The first time that was a problem, was when i tried to improve that model so i clicked on the report for it and saw which lines were uncovered and i tried to fix them writing tests for them on app_2/spec/namespace/my_model_spec.rb.

但这没什么区别,我尝试了更具攻击性的测试,并且删除了所有内容规格文件,但不知何故我仍然获得了85%的覆盖率,因此my_model_spec.rb与my_model.rb的覆盖率结果无关。

But that didn't make any difference, i tried a more aggressive test and i erased all the content on the spec file but somehow i still was getting the 85% of coverage, so the my_model_spec.rb is not related to the coverage results of my_model.rb. Kind of unexpected.

但是由于此文件位于app_2上,我决定在app_1 spec_helper的SimpleCov.start块上添加一个过滤器,例如:

But since this file was on app_2 i decided to add a filter on the SimpleCov.start block on app_1 spec_helper, like:

add_filter "/app_2_name_space/"

然后我移到app_2文件夹并开始设置SimpleCov,看看我将在这里得到什么结果。结果他们变得很奇怪。

I moved then to the app_2 folder and started setting up SimpleCov and see what results i would get here. And they turned out weirder.

对于相同的模型,我获得了100%的覆盖率,我对清空my_model_spec.rb文件进行了相同的测试,但仍然获得了100%的覆盖率。所以,这真的很烦,或者我听不懂。

For the same model i got 100% coverage, i did the same test of empty'ing the my_model_spec.rb file and still got the 100%. So this really f**ed up, or i don't understand something.

这是如何工作的?( ,当我在本地运行官方示例时,文档我得到了不同的结果,所以我认为那里是一个错误或过时的文档)

How does this work?(with the Ruby 1.9 Coverage module you say, well when i run locally the example on the official documentation i get different results, so i think there's a bug or outdated documentation there)

ruby-doc: {"foo.rb"=>[1, 1, 10, nil, nil, 1, 1, nil, 0, nil]}
locally:  {"foo.rb"=>[1, 1, 10, nil, nil, 1, 0, nil, 1, nil]}

我希望报告不显示无论在何处,在应用程序代码中某个位置进行评估的行的肯定结果。

I hope the reports don't show positive results for lines that get evaluated somewhere on the app code, no matter where.

我认为预期的行为是例如模型的结果与其规格,控制器相同等。

I think the expected behavior is that the results for a model for example are related to it's spec, same thing for controllers, etc.

是这种情况吗?如果是这样,我为什么会得到这个奇怪的结果。

Is this the case? If so, why am i getting this strange results.

还是您认为我的应用程序的结构可能与SimpleCov和Coverage搞混了?

Or do you think the structure of my apps could be messing up with SimpleCov and Coverage?

谢谢抽空阅读本文,如果您需要更多详细信息,请询问。

Thank you for taking the time to read this, if you need more detailed info, just ask.

推荐答案

关于您对模型的困惑因为我不确定我是否理解正确,所以100%涵盖了:Coverage(因此SimpleCov)无法知道您的代码是从规范执行还是从其他地方执行。假设我有一个方法 foo和一个调用 foo的方法 bar。如果我在规格中调用bar,那么foo当然也会显示为被覆盖。

Regarding your confusion with the model being 100% covered, as I'm not sure that I understand correctly: There's no way for Coverage (and therefore SimpleCov) to know whether your code has been executed from a spec or "somewhere else". Say I have a method "foo" and a method "bar" that calls foo. If I invoke bar in my specs, of course foo will also be shown as covered.

关于您的一般问题:我认为应该可以报告覆盖率。仅仅因为源代码与您的项目根目录位于不同的点,不应导致覆盖报告的丢失。

As to your general problem: I think it should be possible to have the coverage reported. Just because the source code is at some different point than your project root should not lead to the loss of coverage reporting.

基本配置中的两件事:删除基本适配器(第2行)是不必要的,因为适配器基本上是美化的配置块,并且在这一点上您已经执行了它(因为在加载Simplecov时会调用它)。

Two things in your base config: Removing the base adapter (line 2) is unneccessary as adapters basically are glorified config chunks and at this point you'll have it already executed (since it gets invoked when Simplecov is loaded). Resetting the filters should be sufficient.

此外,不使用您定义的自定义适配器。有关如何正确设置适配器的信息,请参阅自述文件,但我认为当您现在开始覆盖运行时,只需在SimpleCov config块中使用此选项就可以了:

Also, the custom adapter you define is not used. Please refer to the README as to how to properly set up adapters, but I think you'd be fine with simply using this in the SimpleCov config block when you start the coverage run for now:

SimpleCov.start 'rails' do
  your_custom_config
end

您可能想要的是所有应用程序的合并覆盖率报告。为此,您必须首先在配置块内为每个规格套件定义一个command_name,如下所示: command_name'App1 Specs'

What you'll probably want though is a merged coverage report for all your apps. For this, you'll have to define a command_name for each of your spec suites first, inside your config block, like this: command_name 'App1 Specs'.

您还必须定义一个中央 coverage_path ,它将存储您在所有应用套件中的覆盖率报告。假设您有〜/ projects / my_project / app [1-3] ,然后将其放入 my_project / coverage 有道理。这将导致您不同的测试套件结果合并到一个报告中,就像将SimpleCov与Cucumber&以RSpec为例。合并的默认超时时间约为10分钟,因此您可能需要使用配置中的 merge_timeout 3600 将其设置为更高的值(以秒为单位)。有关这些配置选项的详细信息,请再次查看README和SimpleCov :: Configuration文档。

You'll also have to define a central coverage_path, which will store away your coverage reports across your app suites. Say you have ~/projects/my_project/app[1-3], then putting that into my_project/coverage might make sense. This will lead to your different test suite results getting merged into one single report, just like when using SimpleCov with Cucumber & RSpec for example. Merging has a default timeout of ~10 minutes, so you might need to set this to a higher value using merge_timeout 3600 in your config (those are seconds). For the specifics of these configuration options please again check out the README and the SimpleCov::Configuration documentation. These things are outlined there in fair detail.

因此,总而言之,您的每个应用都应如下所示:

So, to sum it up, each of your apps should look somewhat like this:

require 'SimpleCov'
SimpleCov.start 'rails' do
  reset_filters!
  command_name 'App1 Spec'
  coverage_path File.dirname(__FILE__) + '../../coverage' # Assuming this is in my_project/app1/spec/spec_helper.rb
  merge_timeout 3600
end

接下来,您可能想添加过滤器以拒绝所有非项目宝石路径,并且您应该处于&正在运行。

Next thing you might want to add filters to reject all non-project gems by path, and you should be up & running.

这篇关于具有多个应用程序的SimpleCov-或简而言之,Simplecov如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-06 10:42