Add badges for TravisCI, Coveralls and Code Climate to your Readme (ruby)

To monitor your code and show its quality in your Github Readmes you can add badges, such as for Travis CI (to show build status and test reports), Coveralls (code coverage reports), and Code Climate (code quality, a measurement out of 4). As well as seeing the status of your project’s code, clicking on the badges allow users to see detailed reports on these websites.

This tutorial shows you how to set up your project with these code reports. As well as this, it includes instructions for adding SimpleCov for code coverage data in your terminal and how to configure RSpec to print out more verbose test reports. This example is for ruby projects, but can be easily adapted to other languages. My Ruby tic tac toe tech test is one project where I used this setup.

Prerequisites

  • A Github account
  • Started a ruby project and created a Github repo for it
  • Knowledge of using RSpec for testing with Ruby

Setup your project

To add the testing setup to your project, add the following gems to your Gemfile, then run bundle install.

# Gemfile
gem 'rake'
group :test do
gem 'coveralls', require: false
gem 'rspec'
gem 'simplecov', require: false
gem 'simplecov-console', require: false
end

If you haven’t already initialized rspec for testing, enter rspec -—init in your terminal in the root of your project directory. Add the following to the top of your spec/spec_helper.rb . It gathers the code coverage data for Coveralls and SimpleCov.

# spec_helper.rb
require 'coveralls'
require 'simplecov'
require 'simplecov-console'
Coveralls.wear!
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new(
[
SimpleCov::Formatter::Console,
# Want a nice code coverage website? Uncomment this next line!
# SimpleCov::Formatter::HTMLFormatter
]
)
SimpleCov.start

To format the rspec output to be more verbose than a line of green dots. Add the following to .rspec

--require spec_helper
--format documentation
--color

Run rspec in your terminal to see these reports. Here is an example of how it will look:

… and at the end you will see coverage report, including data for the lines and files covered.

Using Travis CI, Coveralls and Code Climate

You need to sign up for Travis CI, Coveralls, and Code Climate. Once you have done this, sync your github repos with them, and click on the switch to to select the github repo of the project you want to add.

Add .travis.yml to your project that contains the below code. This setup specifies which ruby version to use, to install the gems when building the project, then run rspec and the rake task to push the data to Coveralls.

# .travis.yml
language: ruby
rvm:
- 2.4.0
before_install:
- gem install bundler -v 1.15.1
notifications:
- false
scripts:
- bundle exec rspec spec
after_script:
- bundle exec rake coveralls:push

The Rakefile runs rspec and then pushes the data to coveralls.

# Rakefile
require 'rubygems'
require 'rake'
require 'rspec/core/rake_task'
require 'coveralls/rake/task'
desc 'Run RSpec'
RSpec::Core::RakeTask.new do |t|
t.verbose = false
end
task default: :spec
Coveralls::RakeTask.new

To stop the coverage data being pushed to your github repo, add /coverage/ to your .gitignore.

Add the badges to your Readme

Lastly, add the markdown for the badges in yourReadme.md (where you will add your own links to you github repo). Take out the spaces between the badges to show them all on the same line.

[![Build Status](https://travis-ci.org//.svg?branch=master)](https://travis-ci.org//)
[![Coverage Status](https://coveralls.io/repos/github///badge.svg?branch=master)](https://coveralls.io/github//?branch=master)
[![Code Climate](https://codeclimate.com/github/codeclimate/codeclimate/badges/gpa.svg)](https://codeclimate.com/github//)

Now, add the files, commit and push to Github, and wait for Travis CI to build the project and send the data to Coveralls and Code Climate. Click on the badges to see more details of the code reports.

Read more from Ryan Davidson

Read more from the web

read original article here