简体   繁体   中英

High code coverage rate with few tests for a Rails app using simplecov

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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