简体   繁体   English

Ruby on Rails使用simplecov测试覆盖率

[英]Ruby on Rails test coverage with simplecov


I want to analyse the test coverage of our code , and therefore, installed the simplecov gem. 我想分析代码的测试覆盖率,因此,安装了simplecov gem。

Our testing environement has 2 seperate project: REST API test (Java+Rest-Assured) and Web UI testing (Java-Selenium). 我们的测试环境有两个单独的项目:REST API测试(Java + Rest-Assured)和Web UI测试(Java-Selenium)。
As you can see, we dont have unit testing inside the rails app, and we are testing using external projects. 如您所见,我们在rails app中没有单元测试,我们正在使用外部项目进行测试。

I configured the simplecov gem as descriped in the tutorial and put this, in the rails script: 我按照教程中的描述配置了simplecov gem,并将其放在rails脚本中:

require 'simplecov'
SimpleCov.start 'rails'
puts "require simplecov"

When loading the app, I see the string I printed. 加载应用程序时,我看到我打印的字符串。

I ran both automation test projects, saw their printouts in the rails log, but I don't see any coverage of controllers/models, I see only small precentage of initializtion files of some gems and rails. 我运行了两个自动化测试项目,在rails日志中看到了他们的打印输出,但是我没有看到控制器/模型的任何覆盖,我看到一些gems和rails的初始化文件的小百分比。

I searched the net, and tried putting the code phrase in boot.rb or even puma.rb and it returned the same results. 我搜索网络,并尝试将代码短语放在boot.rb甚至puma.rb中,它返回相同的结果。



Any ideas? 有任何想法吗?

EDIT 编辑

Nothing helped with all the comments, but I figured out something very interesting, in all cases, I only see the name of methods marked as tested, not the content (in controllers). 没有任何帮助所有的评论,但我想出了一些非常有趣的东西,在所有情况下,我只看到标记为测试的方法的名称,而不是内容(在控制器中)。
I tried to put the simplecov start phrase in both bin/rails, puma.rb, config.ru, environment.rb, all not given the desired results of code coverage. 我试图将simplecov启动短语放在bin / rails,puma.rb,config.ru,environment.rb中,所有这些都没有给出所需的代码覆盖率结果。

I'm not sure simplecov can measure the whole rails app coverage... But I googled something that you can attach as a rack middleware: 我不确定simplecov可以测量整个rails应用程序的覆盖范围......但我搜索了一些你可以作为机架中间件附加的内容:

https://github.com/danmayer/coverband https://github.com/danmayer/coverband

And it's output is compatible with simplecov. 它的输出与simplecov兼容。 So it looks like it could be useful in your case. 所以看起来它可能对你的情况有用。

As you mentioned in your question you're using puma. 正如你在问题中提到的,你正在使用美洲狮。 I suspect that, since it's multi-threaded, it spawns a few rails apps and their simplecov output overwrites each other's results. 我怀疑,因为它是多线程的,它会产生一些rails应用程序,它们的simplecov输出会覆盖彼此的结果。 I'd try with the single threaded server like webrick - but this may make your tests slower (depending on how the tests are fired up really) or try the coverband gem. 我尝试使用像webrick这样的单线程服务器 - 但是这可能会使你的测试变得更慢(取决于测试是如何被激发的)或尝试使用coverband gem。

Also - even if the server is single threaded - I'm not sure if each request would not overwrite simplecov 's output. 另外 - 即使服务器是单线程的 - 我也不确定每个请求是否都不会覆盖simplecov的输出。

Maybe you have to specify the paths 也许你必须指定路径

require 'simplecov'
SimpleCov.start do

  # add_filter '/admin/'

  add_group "Models", "app/models"
  add_group "Controllers", "app/controllers"
  add_group "Lib", "lib/"
  add_group "Helpers", "app/helpers"
end

You need to to start SimpleCov before loading any of your files, so put these lines as early as possible in your ruby entrypoint: 您需要在加载任何文件之前启动SimpleCov,因此请尽快将这些行放在ruby入口点中:

require 'simplecov'
SimpleCov.start

You could see an example in one of my repos here: https://github.com/tareksamni/DockUp/blob/master/spec/spec_helper.rb 你可以在我的一个回购中找到一个例子: https//github.com/tareksamni/DockUp/blob/master/spec/spec_helper.rb

I do autoload my ruby code after starting SimpleCov . 启动SimpleCov后,我会autoload我的ruby代码。 You need to the same as well: 你需要同样的:

require 'simplecov'
SimpleCov.start

require './autoload'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM