2

We started to use the simplecov gem in order to calculate code coverage for a Ruby on Rails application.

SimpleCov.start 'rails'

Although we developed only a few test cases using Cucumber, the rate provided by simplecov is as high as 40%.

How to make this rate more accurate?

Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121
Charmi
  • 594
  • 1
  • 5
  • 20
  • I don't think this is the right place for this question. – MZaragoza May 10 '16 at 14:57
  • Review what is covered to see if there is something wrong with that. If there is a mistake, go to simplecov (https://github.com/colszowka/simplecov/issues) and submit an issue. – Dave McNulla May 10 '16 at 16:10

1 Answers1

2

The measured coverage figure is probably accurate. Ruby module, class and method definitions are code, so merely loading modules and classes while simplecov is running covers a substantial percentage of the code. A single Cucumber scenario will likely refer to many of your classes and so cause this effect. References to your code in rake tasks and other places will also increase this 'background' coverage. You can see the extent to which this is true by looking at your coverage report and observing that class and module and def lines are covered while the bodies of the definitions are not.

Don't fight it; just work with it. Don't try to run simplecov after your code is loaded, because simplecov will still include module, class and method definitions in the denominator of the code coverage figure, which would be even more annoying.

In fact, you may even want to eager load all of your code so that simplecov shows you the lack of coverage in files that your tests wouldn't cause to be loaded otherwise. (simplecov doesn't instrument files that are never loaded.) That will really get you an accurate measurement. I had to stop doing that in my most recent project because it interfered with Coveralls in a way that I've forgotten, but that might not be an issue for you.

Community
  • 1
  • 1
Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121