I am a big fan of code coverage. Enough has been written about the need of the same. It helps raise confidence within development teams, solidifies the unit testing process and alleviates some risks that lurk when there is not a plethora of quality assurance resources. When this is coupled with good peer code review and inspection practices that come with say Git Pull Requests or Atlassian Crucible, you can be certain to a good degree that the funtionality at least at the unit level is not brittle. Even though it is metrics vs inspections, I find that investment in keeping a track of code coverage metrics is better than having Fagan Inspections or reading code line by line with the whole team sitting around a table while taking multiple coffee breaks.
Note that I am working with a sexy MacbookPro. So if you are using a Windows machine (bless your heart) the commands may not be that different. Please feel free to add those as comments if you end up trying this on Windows.
Now, let us first walk through the steps you will need to get going locally.
2. Teaspoon depends on Istanbul for coverage. Install it as:
npm install -g istanbul
3. Edit the Gemfile of your Rails app and enable the phantomjs gem as :
group :development, :test do ... gem 'phantomjs' ... end
phantomjs is a headless WebKit browser.
4. Now run bundle install to make sure phantomjs is installed.
You may have to actually run Teaspoon as well to make sure phantomjs is installed.
bundle exec teaspoon
5. Edit teaspoon_env.rb and enable at least these lines in the coverage area:
#... config.use_coverage = nil config.coverage do |coverage| coverage.reports = ["html"] #Which coverage reports Istanbul should generate...
Now, in order to test and generate a default configuration report locally, do:
bundle exec teaspoon --coverage=default
Report is generated under coverage folder in <App_Home> folder, by the name of the configuration you specified. There you will find index.html, open it and viola!
Now, to get the same effect going for CircleCI build (that is cloud baby!) you first have to tell the CircleCI environment that coverage is on. This is done through circle.yml
So edit that file and add these following line under dependencies / pre section. This will install Istanbul as part of the CI build, if it has not been done on a higher level.
- npm install -g istanbul
Also in test / post section, enable coverage and then copy the artifacts to the default CIRCLE artifacts location. Note that this may be different for you. No point make this a parallel task as teaspoon tests (if well written) are execute very quickly. But note that copy should be done after the Teaspoon tests have run. In other words, the commands are sequential.
- bundle exec teaspoon --coverage=default - cp -r ./coverage/default $CIRCLE_ARTIFACTS
Run the build. When it has run successfully, check the Artifacts tab and scroll down for index.html.
More configuration options and settings can be set in teaspoon_env.rb. You may check out the teaspoon gem site for more settings.