1

I have a requirement where in, i need to get the coverage so far. If I stop the server, the report gets generated and I do get the coverage so far. But if i start the server again, my previous coverage results are lost and I can only get the coverage after the server was restarted.

Is there a way for me to keep checking periodically for the the coverage% - without stopping the server?

If i try to generate a report without starting the server, by using the following command, in rails console (SimpleCov.result.format! ),I dont get anycoverage number.

The following is my config in my config/boot.rb:

require 'simplecov'
SimpleCov.start 'rails' do
add_filter "/vendor/"
end

Please share your thoughts Thanks Ramya

This is the content of my boot.rb:

require 'simplecov'

# # create coverage directory if it doesn't exist already.
 Dir.mkdir("coverage") unless Dir.exist?("coverage")

 SimpleCov.start 'rails' do
        SimpleCov.use_merging(true)
   add_filter "/vendor/"
   SimpleCov.merge_timeout 30
 end


require 'rubygems'

# Set up gems listed in the Gemfile.
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)

require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
Ramya AT
  • 159
  • 3
  • 15

2 Answers2

0

The pre-requisites for SimpleCov to work properly are documented here: Getting Started with SimpleCov. You must be having the SimpleCov related code inside the boot.rb file after the Rails loading code. This is wrong. Promote all that code to the top and the SimpleCov.result.format! method will work inside the console.

However, it's generally a bad idea to have any extra code inside the boot.rb. Usually, coverage reports are needed only in the test environment (when the code is committed and a continuous integration server like Travis runs the full test suite and generates a coverage report). Hence, the documentation refers to this style of setup where everything related to SimpleCov runs in the test environment. The first topic in the Getting Started section mentions that you need to have the SimpleCov.start line at the beginning of the test_helper file (spec_helper.rb if you're using Rspec) since that is the file that loads the Rails environment; which means that you end up loading SimpleCov and it's configuration before loading the actual application code and you get a correct output.

Kashyap
  • 4,696
  • 24
  • 26
  • Thanks for the reply Kashyap - i do get the correct result when i stop the server. But if i just give "SimpleCov.result.format!" in rails console (when the server is still running) - i do not get the correct numbers. How can I get the coverage number (so far) without stopping the rails server? – Ramya AT Feb 06 '14 at 09:12
  • Where do you have the `SimpleCov.start {...}` block in your `boot.rb` file? at the top or bottom? – Kashyap Feb 06 '14 at 09:20
  • please find the full content of my boot.rb in the description – Ramya AT Feb 06 '14 at 09:45
  • Ah! make sure you run the server and the console in the test environment (or production mode). Or else, it will generate coverage reports of the files that are loaded in memory. In development mode, if you run rails console, it will load only the files that are specific to console and not the whole framework. In test and production environments, the whole framework is loaded into memory. – Kashyap Feb 06 '14 at 11:05
  • 1
    Use `RAILS_ENV=test bundle exec rails server` and `RAILS_ENV=test bundle exec rails console`. – Kashyap Feb 06 '14 at 11:05
0
require 'simplecov'
SimpleCov.start do
    coverage_dir  "custom-coverage_"+Time.now.strftime("%m_%d_%Y").to_s
end
zawhtut
  • 8,335
  • 5
  • 52
  • 76