34

Using simple_cov gem in a Rails app, can we have the files that we are not testing included in the report?

  • If yes, how?

  • If no, that files should count to the coverage percentage, right ?

Alex Altair
  • 3,246
  • 3
  • 21
  • 37
Gustavo Semião-Lobo
  • 2,468
  • 3
  • 18
  • 26
  • If you run the whole spec folder, then the report show every controller, helper, model coverage. For any specific test it only shows that test coverage. – Emu Feb 27 '14 at 17:23
  • 1
    Using minitest and is not doing that, even when running the whole test folder. – Gustavo Semião-Lobo Feb 28 '14 at 11:35

3 Answers3

55

Try to edit your config/environments/test.rb and set this line:

config.eager_load = false

to true in this way the whole app is loaded and simplecov reads it.

alex88
  • 4,788
  • 4
  • 39
  • 60
  • 1
    this is the preferred solution and imho fixes the issue – Chris Hough Nov 27 '14 at 22:34
  • This fixed it for me. – Tomas Romero Jun 17 '15 at 20:03
  • I'm having the same problem as @koen. Does anyone know what the workaround is after that? I'm running with Minitest for what it's worth. – JWitter Sep 29 '17 at 18:23
  • It jumped my coverage from 73.xx% to 85.xx%, simply eager_loading. This is kind of funny. But gist is simplecov was considering all the code ran during eager_load phase also as covered. So no files in our project had 0% coverage anymore. Not a desired behavior i think. – Zia Ul Rehman Mughal Dec 01 '18 at 05:26
16

Eager load the whole Rails app when running tests suite with code coverage. Add Rails.application.eager_load! to spec_helper.rb.

Simplecov slows down tests that's why I use shell environment variable to turn it on. Usually my spec_helper.rb/rails_helper.rb looks something like this:

if ENV['COVERAGE']
  require 'simplecov'
  # some SimpleCov setup, e.g. formatters
  SimpleCov.start 'rails'
end

ENV['RAILS_ENV'] ||= 'test'
require 'spec_helper'
require File.expand_path('../../config/environment', __FILE__)
require 'rspec/rails'

Rails.application.eager_load! if ENV['COVERAGE']
Michał Knapik
  • 585
  • 6
  • 20
5

Since version 0.11.0 the files that should be tracked can be explicit set (Pull Request).

For a Rails app that would be:

require 'simplecov'
SimpleCov.start do
  track_files '{app,lib}/**/*.rb'
end

Or simple use:

require 'simplecov'
SimpleCov.start 'rails'

And the files to be tracked will be set by default (current related code).

Filipe Giusti
  • 2,923
  • 3
  • 24
  • 18