Class: SimpleCov::RSpec

Inherits:
Object
  • Object
show all
Defined in:
lib/simplecov-rspec.rb

Overview

Configure SimpleCov to fail RSpec if the test coverage falls below a given threshold

Configures RSpec to:

  1. Fail (and exit with with a non-zero exitcode) if the test coverage is below the configured threshold and
  2. (optionally) list the lines of code not covered by tests.

Simply add the line SimpleCov::RSpec.start in place of SimpleCov::Start in the project's spec_helper.rb. This line must appear before the project is required.

Examples:

Initialize SimpleCov with defaults

SimpleCov::RSpec.start

Initialize SimpleCov with a test coverage threshold other than 100%

SimpleCov::RSpec.start(coverage_threshold: 90, fail_on_low_coverage: true, list_uncovered_lines: false)

Pass a configuration block to SimpleCov::RSpec.start

SimpleCov::RSpec.start { formatter = SimpleCov::Formatter::LcovFormatter }

Bash script to run tests in an infinite loop writing failures to fail.txt:

while true; do FAIL_ON_LOW_COVERAGE=TRUE rspec >> fail.txt; done

Defined Under Namespace

Classes: UncoveredLine

Class Method Summary collapse

Class Method Details

.start(coverage_threshold: 100, fail_on_low_coverage: true, list_uncovered_lines: false, rspec_dry_run: ::RSpec.configuration.dry_run?, env: ENV, &start_config_block) ⇒ Void

Configure and start SimpleCov for RSpec

Examples:

Initialize SimpleCov with defaults

SimpleCov::RSpec.start

Examples:

Initialize SimpleCov with a test coverage threshold other than 100%

SimpleCov::RSpec.start(coverage_threshold: 90)

Initialize SimpleCov to not fail the test run if the coverage is below the threshold

SimpleCov::RSpec.start(fail_on_low_coverage: false)

# OR use an environment variable to override the default
ENV['FAIL_ON_LOW_COVERAGE'] = 'true'
SimpleCov::RSpec.start

# OR use an environment variable to override the default from the rspec command line
FAIL_ON_LOW_COVERAGE=TRUE rspec

Initialize SimpleCov to list the lines not covered by tests

SimpleCov::RSpec.start(list_uncovered_lines: true)

# OR use an environment variable to override the default
ENV['LIST_UNCOVERED_LINES'] = 'true'
SimpleCov::RSpec.start

# OR use an environment variable to override the default from the rspec command line
LIST_UNCOVERED_LINES=TRUE rspec

Parameters:

  • coverage_threshold (Integer) (defaults to: 100)

    the test coverage threshold (default: 100)

    Coverage below this threshold will cause the rspec to fail if fail_on_low_coverage is true.

  • fail_on_low_coverage (Boolean) (defaults to: true)

    whether to fail if the coverage is below the threshold (default: true)

    This setting will be read from the environment variable FAIL_ON_LOW_COVERAGE if it is NOT given in the .start method. Setting that environment variable to 'true', 'yes', 'on', or '1' will cause this setting to be false (the logic is inverted). Any other value will cause this seetting to be true.

  • list_uncovered_lines (Boolean) (defaults to: false)

    whether to list the lines not covered by tests (default: false)

    All lines not covered by tests will be listed if the coverage is below the threshold. Probably only makes sense to use if the threshold is 100%.

    This setting will be read from the environment variable LIST_UNCOVERED_LINES if it is NOT given in the .start method. Setting that environment variable to 'true', 'yes', 'on', or '1' will cause this setting to be true. Any other value will cause this setting to be false.

  • start_config_block (Proc)

    a configuration block to pass to SimpleCov.start (default: nil)

  • rspec_dry_run (Boolean) (defaults to: ::RSpec.configuration.dry_run?)

    whether the rspec run is a dry run

    Typically not set by the user. Used for this gem's unit testing.

    If RSpec is being run in dry run mode, test coverage under the threshold will not fail the build.

    The purpose of this is to allow the test coverage to be run in a dry run by an IDE so it can report failed tests and coverage without reporting that the entire RSpec run has failed.

  • simplecov_module (Module)

    the SimpleCov module (default: ::SimpleCov)

    Typically not set by the user. Used for this gem's unit testing.

    Allows the SimpleCov module to be mocked.

  • env (Hash) (defaults to: ENV)

    the environment variables (default: ENV)

    Typically not set by the user. Used for this gem's unit testing.

Returns:

  • (Void)


133
# File 'lib/simplecov-rspec.rb', line 133

def self.start(...) = new(...).send(:start)