How to Ace Test Coverage, Code Quality and Code Style Using Travis CI and Code Climate

Darcie Walsh
makers-team-coad
Published in
6 min readOct 30, 2018
Automation and Continuous Integration Testing Flow Diagram

What is Continuous Integration and how can it help you become a better software developer? This is what me and my group (Team COAD woo!) have been asking ourselves this week as we have been tasked with creating a Facebook clone, “Acebook”, with an emphasis on collaborating effectively as a team as if we were simulating a real dev environment. We decided to use Travis CI to run our automated test suites and Code Climate to check our code’s overall quality — this post is about what we’ve learnt so far…

Continuous Integration, tell me more!

Continuous Integration (CI) is a software development practice that aims to increase code quality by regularly integrating any changes and updates to your code. Integrations are handled by a server, where an automated build tests the code and all checks are reported back to you. It encourages developers to commit small changes often, with the aim of detecting any bugs or errors that could have been caused by a commit.

Benefits of CI and Automation:

Reduced integration risk:

With multiple people often working on the same project in lots of different branches, integration risk can be high. Integrating often and using the correct style and quality checks can reduce unnecessary time spent on debugging issues and any errors can be found sooner than later. Every time you commit, a build is triggered, tests are run and any problems caused by the changes are identified.

Higher code quality:

Automating the process means less time is spent on manually deploying projects, and more time is spent writing better, cleaner code — resulting in a higher quality product.

Supports agile principles:

CI and automation work effectively together to support common agile values and core principles. Maintaining regular deployment at a constant pace allows teams to reflect and improve often, whilst being able to produce software quickly and more responsively.

Travis CI

Travis CI is a continuous integration tool that links up easily with your Github repository. It is able to automatically run your tests each time a push is made — every time it runs, Travis builds your app with a clean slate, installing your code then running your test suites for you and alerting you to any errors it finds. We agreed it was like an ‘automated code review’ and we found using it reduced the time it took to review a merge request as we could easily source out where any problems were.

We also noticed that we were writing better quality code as we made sure to get no offenses with Rubocob and at least 95% test coverage with Simplecov before deploying to production. We liked that it allowed us to focus more on the product. We also set it up so that if the CI passed its build, we allowed for automatic deployment to Heroku — meaning more time to spend on making fun features!

Quick setup instructions for Travis CI:

Setting it up was fairly simple although we did encounter some minor problems with what code we actually needed in the .travis.yml file used for set-up so I’ll briefly show you how we did it.

  1. Got to https://travis-ci.org, sign up with your GitHub account and allow the authorisation of Travis CI to your Github account.
  2. Activate the GitHub Apps Integration and select the specific Git Repositories you would like to sync:

3. Create a .travis.yml file in the root of your repository:

Snippet of our .travis.yml file

This file contains code that tells Travis CI what to expect and what actions to perform when it runs your programme. For example:

before_script:
- bundle
- bundle exec rails db:create
- bundle exec rails db:migrate RAILS_ENV=test

Before running the script run bundle to install the required Gems, run bundle exec rails db:create to create the databases and run bundle exec rails db:migrate to migrate the tables into the databases.

The snippet above is what we needed in ours but it should be customised to your program as appropriate. More information on how to customise your build can be found on the Travis CI website here.

4. Commit the .travis.yml file and push it to Github. Travis will automatically find the file after scanning your repo and will start it’s first build! Check the job log to get detailed information on any errors.

Code Climate

Code Climate is a review tool that continuously measures and monitors the quality of your code. We decided to use it alongside Travis CI as it is an extra step in ensuring 100% coverage across your program. It regularly reports how your build is doing in terms of maintainability and shows you the percentage of your code that is actually executed when you run your test suite.

Quick setup instructions for Code Climate:

  1. Visit https://codeclimate.com and sign up, allow authorisation for Github.
  2. Add a GitHub repository to your account
Add a GitHub repo

3. Set-up your test coverage with Travis CI by going to the overview and clicking on the test coverage icon

Setup Test Coverage

4. Scroll down and you should see your Test Report ID:

Test Reporter ID

NOTE: You may have noticed these lines in our .travis.yml file:

env:
global:
- CC_TEST_REPORTER_ID=[YOUR_CODE_CLIMATE_TOKEN_ID]
#### Rest of code omitted for clarity ####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
#### Rest of code omitted for clarity ####after_script:
- ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT

5. You’ll need to add these to your file and just replace [YOUR_CODE_CLIMATE_TOKEN_ID] with your Test Reporter ID.

6. Add, commit and push the file to GitHub.

That’s it! Each time you push to your repo — Travis CI will automatically send a test coverage report to Code Climate.

Pretty README.md Badges 😻

And of course, no good GitHub repo would be complete without showing off the glowing status of your build with some badges oooh!

Test status, maintainability and test coverage for badges from Travis CI and Code Climate.

Most websites have them in their Settings sections and they’re a fairly good indicator that you have well tested and good quality code. They are pretty easy to add to your README.md file — here’s how to add the ones from Code Climate:

  1. Navigate to your repo on https://codeclimate.com/repos.
  2. Go to Repo Settings and then Badges (under Extras):
Code Climate Badges

3. Copy the markdown and paste it into your README.md file and voila! Pretty badges for everyone 🎉🎉🎉🎉

We found implementing continuous integration to be really simple once we figured out what needed to go where. We like how it picks up any bugs quickly and efficiently, allowing us to get to the root of the problem and figure out exactly where and why the build broke — just in case our beloved TDD’ing ways missed something! It’s a great way to stay on top of writing better, cleaner code and I’ll definitely be using it my solo projects going forward.

--

--