Code Coverage Tools For Padrino
This article is part of an ongoing series about my exploration of the Padrino web framework. If you want to read more about it please check out padrinobook.com. You sniff around in the sources of the book under GitHub.
As long as your application exists, some developers leave others will join. It’s good to have some metrics about certain code smells. A code smell is part of your source code which may be the root of a design problem but is not actually a bug. It’s good to have some tools to be “lord of the smells” for Padrino - don’t let smells lower the quality of your project.
(Note: This post is tested with padrino 0.12.2, simplecov 0.9, metric_fu 4.11.1, and ruby 2.1.2p95)
Available Tools
I will go through the following tools and will explain how you can use them.
- simplecov: It will automatically detect the tests you are using Rubies 1.9’s built-in Coverage library to gather code coverage data.
- metric_fu: Creates churn, code smells, and other coverage tools that generate reports about your code.
- codeclimate.com: Online tool for measuring quality and security for your application.
Simplecov
Add the gem to your Gemfile
:
gem 'simplecov'
Next, I want to start the code coverage generation every time I run the tests. So we need to add the following line to the spec_helper.rb
:
require 'simplecov'
SimpleCov.start
And that’s all. Next time when you run the tests you can detect lines with the following output:
Coverage report generated for RSpec to ~/git/job-vacancy/coverage. 209 / 252 LOC (82.94%) covered.
/
After all tests have been passed, you can see the output in the coverage/index.html
file:
It is also possible to divide parts of your application into several groups. Add options to the Simplecov.start
block:
SimpleCov.start do
add_group "Models", "app/models"
add_group "Controllers", "app/controllers"
add_group "Helpers", "app/helpers"
add_group "Mailers", "app/mailers"
end
metric_fu
Add the following line to your Gemfile
:
gem 'metric_fu'
When this is done you need to start the metric_fu
command from your command line:
$ ~/git/job-vacancy: metric_fu
******* STARTING METRIC reek
******* ENDING METRIC reek
******* STARTING METRIC flog
******* ENDING METRIC flog
******* STARTING METRIC flay
******* ENDING METRIC flay
******* STARTING METRIC saikuro
******* ENDING METRIC saikuro
******* STARTING METRIC roodi
******* ENDING METRIC roodi
******* STARTING METRIC cane
******* ENDING METRIC cane
******* STARTING METRIC churn
******* ENDING METRIC churn
******* STARTING METRIC stats
******* ENDING METRIC stats
******* STARTING METRIC hotspots
******* ENDING METRIC hotspots
******* SAVING REPORTS
******* GENERATING GRAPHS
*****Generating graphs
*****Generating graphs for tmp/metric_fu/_data/20140318.yml
all done
It will generate a tmp/metric_fu
directory with the following contents:
tmp/metric_fu
├── _data
│ └── 20140318.yml
├── output
│ ├── app_app.rb.html
│ ├── app_controllers_page.rb.html
│ ├── app_controllers_sessions.rb.html
│ ├── app_controllers_users.rb.html
│ ├── app_helpers_page_helper.rb.html
│ ├── app_helpers_sessions_helper.rb.html
│ ├── app_helpers_users_helper.rb.html
│ ├── app_mailers_confirmation.rb.html
│ ├── app_mailers_registration.rb.html
│ ├── app_models_job_offer.rb.html
│ ├── app_models_user_observer.rb.html
│ ├── app_models_user.rb.html
│ ├── app_views_application.erb.html
│ ├── app_views_users_edit.erb.html
│ ├── app_views_users_new.erb.html
│ ├── bluff-min.js
│ ├── cane.html
│ ├── cane.js
│ ├── churn.html
│ ├── excanvas.js
│ ├── flay.html
│ ├── flay.js
│ ├── flog.html
│ ├── flog.js
│ ├── Gemfile.html
│ ├── Gemfile.lock.html
│ ├── hotspots.html
│ ├── index.html
│ ├── js-class.js
│ ├── lib_tasks_auth_token_attribute.rake.html
│ ├── reek.html
│ ├── reek.js
│ ├── roodi.html
│ ├── roodi.js
│ ├── saikuro.html
│ ├── spec_app_controllers_users_controller_spec.rb.html
│ ├── stats.html
│ └── stats.js
├── report.yml
└── scratch
└── churn
└── 873660ae5973b77b09c47d1dcf03577222095d6e.json
4 directories, 41 files
The following metrics are created by cane
can be used if code quality thresholds are met.
churn
measures the change ratio of files and you can use this indicator to have more code review, refactoring, more tests for this beast of a file.
flog
a high flog score is an indicator for code complexity.
flay
analyzes code similarities in your code base - good way to stay clean with DRY.
reek
checks your classes and modules after code smells. Under the reek wiki you can find all code smells and what they actually mean.
hotspot
a gathering of the flog, flay and reek score of the files in your application.
roodi
scans your code and informs you about design issues you may have.
saikuro
generates a list of cyclomatic complexity of each method found in your application.
Code Climate
Go to the website codeclimate.com and register. Once you are logged, you can add any ruby related open source project for free.
It will take a while till the metrics are generated for your application:
But when it’s ready, you get a nice overview:
Or a more detailed overview of single classes: