Read more

How to use Simplecov to find untested code in a Rails project with RSpec and Cucumber

Deleted user #4117
April 22, 2020Software engineer

Simplecov Show archive.org snapshot is a code coverage tool. This helps you to find out which parts of your application are not tested.

Illustration web development

Do you need DevOps-experts?

Your development team has a full backlog? No time for infrastructure architecture? Our DevOps team is ready to support you!

  • We build reliable cloud solutions with Infrastructure as code
  • We are experts in security, Linux and databases
  • We support your dev team to perform
Read more Show archive.org snapshot

Integrating this in a rails project with rspec, cucumber and parallel_tests is easy.

  1. Add it to your Gemfile and bundle

    group :test do
      gem 'simplecov', require: false
    end
    
  2. Add a .simplecov file in your project root:

    SimpleCov.start 'rails' do
      # any custom configs like groups and filters can be here at a central place
      enable_coverage :branch # see https://github.com/colszowka/simplecov#branch-coverage-ruby--25
    end
    

    Note that we're using a predefined profile called rails which will group your models, controllers etc. in different tabs. You can also create your own profiles or use no profile at all.

    Optional: Enable "Branch Coverage" by adding enable_coverage :branch on Ruby 2.5+ projects to make Simplecov check if all possible branches of a condition have been hit.

  3. Require in your features/support/env.rb for cucumber (important: require it before anything else, especially rails!):

    require 'simplecov'
    SimpleCov.command_name 'features' + (ENV['TEST_ENV_NUMBER'] || '') # remove the TEST_ENV_NUMBER part if you don't use parallel_tests
    
    # ...
    
  4. Require it in spec/spec_helper.rb for rspec (important: require it before anything else, especially rails!)

    require 'simplecov'
    SimpleCov.command_name 'specs' + (ENV['TEST_ENV_NUMBER'] || '') # remove the TEST_ENV_NUMBER part if you don't use parallel_tests
    
    # ...
    
  5. Run your tests (rspec and cucumber)

  6. At the end of the testrun you should see something like Coverage report generated for features, features2, features3, features4, specs, specs2, specs3, specs4 to /home/<user>/<my-project>/coverage.

  7. (optional): add coverage/* to your project's .gitignore

  8. Open the file with your browser: xdg-open coverage/index.html
    You should see a table like the one in the documentation Show archive.org snapshot .

  9. Sort the table by "lines missed" to find the blind spots

Deleted user #4117
April 22, 2020Software engineer
Posted to makandra dev (2020-04-22 17:35)