Coder Social home page Coder Social logo

simplecov-check-action's Introduction

SimpleCov+ Action

A Github check action which displays failing test coverage from SimpleCov while providing the option to fail a build based on minimum coverage threshold.

Github PR Check UI

Want to see more examples of this check in action? ๐Ÿ˜‰ See screenshots.md

Basic Installation

In order for SimpleCov+ Action to function properly, you first need the simplecov gem. See Getting Started.

Assuming you've followed the guide above (you have the gem in your Gemfile and have properly setup test_helper.rb), then the only other step is to utilize the Github action within your workflow.

  # However you run your tests to generate code coverage
  - name: Run my tests
    run: |
      bundle exec rspec specs/

  # Minimum configuration
  - uses: joshmfrankel/simplecov-check-action@main
    with:
      github_token: ${{ secrets.GITHUB_TOKEN }}

Configuration Options

See https://github.com/joshmfrankel/simplecov-check-action/blob/main/action.yml for all available options and their defaults.

Most useful is the minimum_coverage option as it allows specification as to the value at which a failure result should be produced.

Advanced Installation

The advanced installation switches the coverage failing mode from overall test coverage to per file coverage. This is similiar to the minimum_coverage_by_file option that SimpleCov provides. See minimum_coverage_by_file

In order to activate advanced mode, you'll need to configure the simplecov-json gem. See Usage for simplecov-json.

You'll need to setup both the standard formatter as well as json formatter within your test_helper.rb. Example below:

require "simplecov"
require "simplecov-json"
SimpleCov.formatters = SimpleCov::Formatter::MultiFormatter.new([
  SimpleCov::Formatter::HTMLFormatter,
  SimpleCov::Formatter::JSONFormatter
])
SimpleCov.start do
  # your config
end

One large benefit to this approach is that your code coverage minimum threshold is less skewed by outlier results. Said best by the SimpleCov documentation:

You can define the minimum coverage by file percentage expected... This is useful to help ensure coverage is relatively consistent, rather than being skewed by particularly good or bad areas of the code.

Example configuration

Still struggling to set this up? SimpleCov+ Action utilizes itself within a Github workflow. You can view the workflow and the spec_helper files for a good example of how to configure this check.

Example Github Workflow

Example Spec Helper SimpleCov Setup

FAQs

First off there is a debug option which prints many of the API calls made to Github. This can be helpful for diagnosing faults in SimpleCov+ Action.

I don't want to fail coverage on a per-file basis

If you've configured SimpleCov-json then SimpleCov+ Action will automatically fail CI when any file is below the minimum file coverage. This can be disabled if you want to utilize SimpleCov-json but don't want SimpleCov+ Action to fail by setting minimum_file_coverage to 0.

My Code Coverage isn't accurate for Rails parallelize

Use the following configuration within your test_helper.rb or spec_helper.rb:

# inside test_helper.rb or spec_helper.rb
class ActiveSupport::TestCase
  parallelize(workers: :number_of_processors)

  parallelize_setup do |worker|
    SimpleCov.command_name "#{SimpleCov.command_name}-#{worker}"
  end

  parallelize_teardown do
    SimpleCov.result
  end
end

Additionally, make sure that your test environment is eager loading your application. This ensures that SimpleCov picks up coverage for all files accurately.

# config/environments/test.rb
Rails.application.configure do
  # many more configs...
  
  # Note: ENV["CI"] is set to true by default from Github in the testing environment
  config.eager_load = ENV["CI"].present?
  
  # some more configs...
end

simplecov-check-action's People

Contributors

joshmfrankel avatar mvach avatar ashok-chorus avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.