Improve your Ruby code reviews with actionable code coverage and Undercover

Getting started with local coverage checks

Setup

gem install undercover
gem 'simplecov'
gem 'simplecov-lcov'
gem 'simplecov-html' # if you'd like classic html coverage reports
require 'simplecov'
require 'simplecov-lcov'
SimpleCov::Formatter::LcovFormatter.config.report_with_single_file = true
SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new([
SimpleCov::Formatter::LcovFormatter,
SimpleCov::Formatter::HTMLFormatter,
])
SimpleCov.start do
add_filter(/^\/spec\//) # For RSpec
add_filter(/^\/test\//) # For Minitest
end

Using the CLI

$ undercover
✅ No reportable changes
Undercover finished in 0.0742s

Compare to branch or commit

undercover --compare master

Automate

PrePush:
Undercover:
enabled: true
required: true
command: ['undercover', '--compare', 'master']

Set up Continuous Integration

Step 1: Build status check

Travis CI

# .travis.yml
language: ruby
rvm:
- 2.5.1
- 2.4.4
- 2.3.4
before_install:
- gem install bundler undercover
- gem update --system
script:
- bundle exec rake
- git pull origin master:master # required for --compare below
- undercover --compare master

CircleCI

# .circleci/config.yml
version: 2
jobs:
build:
docker:
- image: circleci/ruby:2.5-browsers
steps:
- checkout
- run:
name: Install dependencies
command: |
sudo apt-get install cmake
bundle install
- run:
name: Run RSpec
command: bundle exec rspec
- run:
name: Check coverage
command: |
gem install undercover
undercover --compare origin/master

Semaphore

version: v1.0
name: RSpec + Undercover
agent:
machine:
type: e1-standard-2
os_image: ubuntu1804
blocks:
- name: "RSpec"
task:
jobs:
- name: Run Specs and Check Coverage
commands:
- checkout
- bundle install
- bundle exec rspec
- gem install undercover
- undercover --compare origin/master

Codeship

rvm use 2.5.3 --install
bundle install
gem install undercover
bundle exec rspec --format documentation --color# pull origin/master to have a ref to compare against
git remote set-branches --add origin master
git fetch
undercover --compare origin/master

Merging test results from multiple test runners

Step 2: Automated GitHub comments

That’s how automated code review looks like at Rainforest
# Setup Commands
rvm use 2.5.3 --install
bundle install
gem install pronto
gem install pronto-undercover
# Test Pipeline
bundle exec rspec --format documentation --color
# pull origin/master to have a ref to compare against
git remote set-branches --add origin master
git fetch
pronto run -f github_pr -c origin/master

Summary

  1. Set up the undercover gem to run locally, so that untested code changes do not escape the development environment
  2. Configure your CI to do the same thing, but across all commits and remote branches
  3. Create an automated code coverage feedback loop with Pronto and code review comments that appear directly in a GitHub pull request diff.

--

--

--

software engineer @rainforestqa, hardware hacker and fixed-gear cycling enthusiast from Warsaw, PL.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Know how to share your intelligence with the Traffic Light Protocol

JAVA Classes and objects

Software Development For Startups: How To Start and What Pitfalls To Avoid

Thoughts about naming variables and methods

OpenShift case study

PERSONAL ACCESS TOKENS ON GIT/GITHUB: A TALE OF A LESSON, MISTAKE, AND FUTURE QUEST

Leetcode Q180. Consecutive Numbers (Q173)

@Fancybirdsio is copyleft.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Jan Grodowski

Jan Grodowski

software engineer @rainforestqa, hardware hacker and fixed-gear cycling enthusiast from Warsaw, PL.

More from Medium

Write Better Code Faster: 5 min read

DOCKER LEARNING IV

Application Framework and Software Engineering principles

Why Even Small Applications Should Properly Architectured Before Commencing Development