SkillAgentSearch skills...

Simplecov

Code coverage for Ruby with a powerful configuration library and automatic merging of coverage across test suites

Install / Use

/learn @simplecov-ruby/Simplecov
About this skill

Quality Score

0/100

Supported Platforms

Universal

README

SimpleCov Gem Version Build Status Maintainability Inline docs

Code coverage for Ruby

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/Coverage track covered ruby code, gathering coverage for common templating solutions like erb, slim and haml is not supported.

In most cases, you'll want overall coverage results for your projects, including all types of tests, Cucumber features, etc. SimpleCov automatically takes care of this by caching and merging results when generating reports, so your report actually includes coverage across your test suites and thereby gives you a better picture of blank spots.

The official formatter of SimpleCov is packaged as a separate gem called simplecov-html, but will be installed and configured automatically when you launch SimpleCov. If you're curious, you can find it on GitHub, too.

Contact

Code and Bug Reports

  • Issue Tracker
  • See CONTRIBUTING for how to contribute along with some common problems to check out before creating an issue.

Questions, Problems, Suggestions, etc.

  • Mailing List "Open mailing list for discussion and announcements on Google Groups"

Getting started

  1. Add SimpleCov to your Gemfile and bundle install:

    gem 'simplecov', require: false, group: :test
    
  2. Load and launch SimpleCov at the very top of your test/test_helper.rb (or spec_helper.rb, rails_helper, cucumber env.rb, or whatever your preferred test framework uses):

    require 'simplecov'
    SimpleCov.start
    
    # Previous content of test helper now starts here
    

    Note: If SimpleCov starts after your application code is already loaded (via require), it won't be able to track your files and their coverage! The SimpleCov.start must be issued before any of your application code is required!

    This is especially true if you use anything that keeps your tests application loaded like spring, check out the spring section.

    SimpleCov must be running in the process that you want the code coverage analysis to happen on. When testing a server process (e.g. a JSON API endpoint) via a separate test process (e.g. when using Selenium) where you want to see all code executed by the rails server, and not just code executed in your actual test files, you need to require SimpleCov in the server process. For rails for instance, you'll want to add something like this to the top of bin/rails, but below the "shebang" line (#! /usr/bin/env ruby) and after config/boot is required:

    if ENV['RAILS_ENV'] == 'test'
      require 'simplecov'
      SimpleCov.start 'rails'
      puts "required simplecov"
    end
    
  3. Run your full test suite to see the percent coverage that your application has.

  4. After running your tests, open coverage/index.html in the browser of your choice. For example, in a Mac Terminal, run the following command from your application's root directory:

    open coverage/index.html
    

    in a debian/ubuntu Terminal,

    xdg-open coverage/index.html
    

    Note: This guide can help if you're unsure which command your particular operating system requires.

  5. Add the following to your .gitignore file to ensure that coverage results are not tracked by Git (optional):

    echo coverage >> .gitignore
    

    If you're making a Rails application, SimpleCov comes with built-in configurations (see below for information on profiles) that will get you started with groups for your Controllers, Models and Helpers. To use it, the first two lines of your test_helper should be like this:

    require 'simplecov'
    SimpleCov.start 'rails'
    

Example output

Coverage results report, fully browsable locally with sorting and much more:

SimpleCov coverage report

Source file coverage details view:

SimpleCov source file detail view

Use it with any framework!

Similarly to the usage with Test::Unit described above, the only thing you have to do is to add the SimpleCov config to the very top of your Cucumber/RSpec/whatever setup file.

Add the setup code to the top of features/support/env.rb (for Cucumber) or spec/spec_helper.rb (for RSpec). Other test frameworks should work accordingly, whatever their setup file may be:

require 'simplecov'
SimpleCov.start 'rails'

You could even track what kind of code your UI testers are touching if you want to go overboard with things. SimpleCov does not care what kind of framework it is running in; it just looks at what code is being executed and generates a report about it.

Notes on specific frameworks and test utilities

For some frameworks and testing tools there are quirks and problems you might want to know about if you want to use SimpleCov with them. Here's an overview of the known ones:

<table> <tr><th>Framework</th><th>Notes</th><th>Issue</th></tr> <tr> <th> parallel_tests </th> <td> As of 0.8.0, SimpleCov should correctly recognize parallel_tests and supplement your test suite names with their corresponding test env numbers. SimpleCov locks the resultset cache while merging, ensuring no race conditions occur when results are merged. </td> <td> <a href="https://github.com/simplecov-ruby/simplecov/issues/64">#64</a> &amp; <a href="https://github.com/simplecov-ruby/simplecov/pull/185">#185</a> </td> </tr> <tr> <th> knapsack_pro </th> <td> To make SimpleCov work with Knapsack Pro Queue Mode to split tests in parallel on CI jobs you need to provide CI node index number to the <code>SimpleCov.command_name</code> in <code>KnapsackPro::Hooks::Queue.before_queue</code> hook. </td> <td> <a href="https://knapsackpro.com/faq/question/how-to-use-simplecov-in-queue-mode">Tip</a> </td> </tr> <tr> <th> RubyMine </th> <td> The <a href="https://www.jetbrains.com/ruby/">RubyMine IDE</a> has built-in support for SimpleCov's coverage reports, though you might need to explicitly set the output root using `SimpleCov.root('foo/bar/baz')` </td> <td> <a href="https://github.com/simplecov-ruby/simplecov/issues/95">#95</a> </td> </tr> <tr> <th> Spork </th> <td> Because of how Spork works internally (using preforking), there used to be trouble when using SimpleCov with it, but that has apparently been resolved with a specific configuration strategy. See <a href="https://github.com/simplecov-ruby/simplecov/issues/42#issuecomment-4440284">this</a> comment. </td> <td> <a href="https://github.com/simplecov-ruby/simplecov/issues/42#issuecomment-4440284">#42</a> </td> </tr> <tr> <th> Spring </th> <td> <a href="#want-to-use-spring-with-simplecov">See section below.</a> </td> <td> <a href="https://github.com/simplecov-ruby/simplecov/issues/381">#381</a> </td> </tr> <tr> <th> Test/Unit </th> <td> Test Unit 2 used to mess with ARGV, leading to a failure to detect the test process name in SimpleCov. <code>test-unit</code> releases 2.4.3+ (Dec 11th, 2011) should have this problem resolved. </td> <td> <a href="https://github.com/simplecov-ruby/simplecov/issues/45">#45</a> &amp; <a href="https://github.com/test-unit/test-unit/pull/12">test-unit/test-unit#12</a> </td> </tr> </table>

Configuring SimpleCov

Configuration settings can be applied in three

Related Skills

View on GitHub
GitHub Stars4.9k
CategoryDevelopment
Updated3d ago
Forks576

Languages

Ruby

Security Score

100/100

Audited on Mar 20, 2026

No findings