Display your Ruby test coverage using the SimpleCov gem and CodeClimate

When developing a Ruby or Ruby on Rails app, one popular code quality tool is the SimpleCov gem. This post will show you how to use test coverage data generated by it to create a CodeClimate badge that you can add to a Readme or website.

CodeClimate’s test coverage badge

The SimpleCov gem

I have also written a post explaining how to add badges to your Readmes, including Coveralls to display code coverage data. One advantage of using SimpleCov is that it is a gem that can be installed and run locally, rather than submitting your data to Coveralls to process. It is free and easy to configure.

SimpleCov is a code coverage analysis tool for Ruby. It uses Ruby’s built-in Coverage library to gather code coverage data, but makes processing its results much easier by providing a clean API to filter, group, merge, format, and display those results, giving you a complete code coverage suite that can be set up with just a couple lines of code.
 — SimpleCov

To add it to your Ruby or Ruby on Rails project, add 'simplecov' and ’simplecov-console’ to your Gemfile/Gemspec and then to your spec_helper:

# spec/spec_helper.rb
require 'simplecov'
require 'simplecov-console'
SimpleCov.formatter = SimpleCov.formatter = SimpleCov::Formatter::Console
SimpleCov.start

This uses the SimpleCov Console Formatter, but you can pass it other formatters if you wish, eg a HTML formatter.

Run bundle install then rspec to run your test suite locally and see the code coverage generated.

Once your code has reached the level of coverage you are happy with, its time to add this to Travis CI and CodeClimate.

Add your coverage data to your CI service

This post focuses on using Travis CI for building your ruby projects, but CodeClimate works with other CI tools too. Sign up for Travis CI if you haven’t already.

Once you’re signed in to Travis CI, and we’ve synchronized your GitHub repositories, go to your profile page and enable the repository you want to build:

You need to find your Test Coverage Token from CodeClimate from:

Your Repo on CodeClimate -> Settings -> Test Coverage

Each repository added to Code Climate is automatically assigned a unique test coverage token. When you run the test reporter, it submits your coverage data to Code Climate and includes this token (which it reads from the environment) so we can map the data to the correct repository.
 — Finding Your Test Coverage Token on CodeClimate

Now, add a .travis.yml file to your repository to tell Travis CI what to do. Add the following to set a global variable with your Test Coverage Token:

env:
global:
- CC_TEST_REPORTER_ID=

Then define the ruby environment you are working with, for example:

language: ruby
rvm:
- 2.5.0
before_install: gem install bundler -v 1.16.1
notifications:
- false

(notifications : false tells travis not to send you emails about your builds, but delete this line if you want to keep this feature on)

Generate your CodeClimate coverage badge

CodeClimate has a test reporter tool, that takes the coverage data generated by SimpleCov and transforms it into a format CodeClimate can work with.

The Code Climate test coverage reporter takes a supported test coverage report, transforms it into a generalized format, and submits it to the Code Climate API. It can also combine multiple test coverage report payloads created from parallel test processes within a parallelized CI build. 
Configuring Test Coverage on CodeClimate

This can be downloaded using curl. Specify this as a before script in the .travis.yml:

before_script:
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
- chmod +x ./cc-test-reporter
- ./cc-test-reporter before-build

Run your tests using rspec:

scripts:
- bundle exec rspec spec

Then push the coverage data to Travis CI and CodeClimate:

after_script:
- ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT

The full .travis.yml file will now look like:

env:
global:
- CC_TEST_REPORTER_ID=
language: ruby
rvm:
- 2.5.0
before_install: gem install bundler -v 1.16.1
notifications:
- false
before_script:
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
- chmod +x ./cc-test-reporter
- ./cc-test-reporter before-build
scripts:
- bundle exec rspec spec
after_script:
- ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT

For more on this see:
CodeClimate: Travis CI Test Coverage

Display your badge on your Readme

Finally, display your coverage on your readme using one of CodeClimate’s badges at codeclimate.com/github///badges, copy the markdown and paste it into your Readme and git push → → →

Example of CodeClimate coverage badge on my ruby_types project

Read more from Ryan Davidson

Read more from the web

read original article here