EzDevInfo.com

simplecov

Code coverage for Ruby 1.9+ with a powerful configuration library and automatic merging of coverage across test suites The Ruby Toolbox - SimpleCov: Code coverage for Ruby 1.9+ with a powerful configuration library and automatic merging...

simple cov gem missing untested files

Using simple_cov gem, can we have the files that we are not testing included in the report?

  • If yes, how?

  • If no, that files should count to the coverage percentage, right ?


Source: (StackOverflow)

Coveralls coverage not matching SimpleCov

I'm using the 'simplecov' and 'coveralls' ruby gems but the report that is generated by simplecov doesn't match the report generated by coveralls.

I've found that coveralls is not ignoring code that's wrapped in

# :nocov:

Here is my configuration

require "simplecov"
require "coveralls"

SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[
  SimpleCov::Formatter::HTMLFormatter,
  Coveralls::SimpleCov::Formatter
]
SimpleCov.start

Is there something else I need to be doing to get coveralls to ignore code?


Source: (StackOverflow)

Advertisements

How to merge multiple simplecov directories inside CircleCI when running parallel_test?

I have a rails application that runs parallel_test with rspec inside circleci

Looking around on Internet I added up this to the very beginnging of my spec_helper.rb file:

if ENV['COVERAGE']
  require 'simplecov'
  # on circleci change the output dir to the artifacts
  if ENV['CIRCLE_ARTIFACTS']
    dir = File.join("..", "..", "..", ENV['CIRCLE_ARTIFACTS'], "coverage")
    SimpleCov.coverage_dir(dir)
    SimpleCov.merge_timeout 3600
    SimpleCov.command_name "rspec_#{Process.pid.to_s}#{ENV['TEST_ENV_NUMBER']}"
  end
  SimpleCov.start 'rails'
end

The problem is that as a result I get different folders one for each circleci instance:

enter image description here

What am I doing wrong ?


Source: (StackOverflow)

Rspec SimpleCov NoMethodError

When running my rails 4 rspec suite via rake everything works correctly but when attempting to run rake simplecov I get hundreds of failures all w/ a NoMethodError something like:

  1) Answer Validations
     Failure/Error: it { should validate_presence_of :text }
     NoMethodError:
       undefined method `validate_presence_of' for #<RSpec::Core::ExampleGroup::Nested_16::Nested_1:0x007faf9fb5b7c8>
     # ./spec/models/answer_spec.rb:12:in `block (3 levels) in <top (required)>'

Any clues as to why this is happening? I should also mention that I'm testing using sqlite3 :memory:.


Source: (StackOverflow)

no coverage with SimpleCov, Rake and Test::Unit

In trying to generate code coverage statistics for a ruby repository I hit a wall. I use Rake to run my Test::Unit unit tests, but cannot get SimpleCov to return anything other than

Coverage report generated for Unit Tests <a_folder>. 0.0 / 0.0 LOC (100.0%) covered.

rakefile.rb looks like:

require 'simplecov'
SimpleCov.start
SimpleCov.command_name 'Unit Tests'

desc 'Run unit tests'
task :test do
    sh 'ruby -I ./app test/test_*.rb'
end
task :default => :test

Adding the following as suggested by this issue did not help

module SimpleCov::Configuration
  def clean_filters
    @filters = []
  end
end

SimpleCov.configure do
  clean_filters
  load_adapter 'test_frameworks'
end

I also added a couple puts to verify load order (one at the top of my test file and one immediately after declaring the simplecov dependency) to verify that SimpleCov.start occurs before test code (as it should).

After reading this issue it appears that simplecov and test-unit may not play well together; am I missing a workaround, or should I switch to another testing framework, and if so, which one, and can I do it easily?

Thanks!


Source: (StackOverflow)

How does one configure Cucumber / Aruba to use SimpleCov?

Ruby-2.0.0p247 ActiveRecord-4.0.1 Cucumber 1.3.10 Aruba-0.5.3 SimpleCove-0.8.2

We use Cucumber with Aruba in a NON-RAILS project that nonetheless uses ActiveRecord. Our cucumber features exercise the code both in-process and out-of-process. The out-of-process code is executed using the same loader sequences as in production through a startup stub in bin:

#!/usr/bin/env ruby
require 'bundler/setup'
Bundler.require

require 'pathname'
my_dir = Pathname.new(
  File.join( File.dirname(
    __FILE__ ), '../', 'lib/' ) ).realpath.to_s + '/'

require my_dir +  File.basename( __FILE__ )

HllThForexRssFetch::Main.new( ARGV ).execute
#EOF

Our features/support/env.rb file contains this:

$ cat features/support/env.rb
# Must load and start simplecov before any application code
require 'simplecov'
SimpleCov.start do
  add_filter "/features/"
  add_filter "/libexec"
  add_filter "/lib/hll_active_record/"
  add_filter "/test/"
  add_filter "/tmp/"
end
SimpleCov.command_name( "Cucumber Features" )

# Do not use cucumber/rails in standalone projects
#require 'cucumber/rails' 

. . .

When our step definitions call the external bin/file through aruba's run command the step definitions work properly and the tests complete as expected but the code coverage is not merged with the rest of the run. What I am seeking are instructions for how to set up simplecov to report the code coverage of the out-of-process tests together with the portions that are run in-process by cucumber directly.

How does one do this?


Source: (StackOverflow)

How to write rspec testing without database in ruby for this ruby code

I am creating a ruby project with the class which is the inheritance of ActiveRecord::Base. How can i write rspec testing and simple coverage for the following code sample without using database.

class Person < ActiveRecord::Base
    validates_length_of :name, within: 10..40
end
person = Person.create(:name => "aungaung")
person.save

Source: (StackOverflow)

SimpleCove coverage not coming for only some models

I am having very weird issue with simple cov and rspec. I have following in my rails_hlper file

require 'simplecov'

SimpleCov.start 'rails' do
  add_filter "app/helpers/"
  add_filter "app/models/concerns/"
  add_group 'Models', 'app/models'
end

Even if try to run that model's spec its not showing in coverage report.


Source: (StackOverflow)

How can I tell Simplecov to focus on specific directories in a Rails project?

The title mostly speaks for itself. To be more specific, it would seem to make sense to me that Simplecov be configured to only focus on directories like the app directory instead of including all of the configuration files and such that are going to be covered no matter what and are left unchanged.


Source: (StackOverflow)

Simplecov report ignores folders in /app directory

I use simplecov to measure coverage for my Rails 4.0.11 app. I use Ruby 2.0.0. I noticed that some subfolders of /app are ignored by simplecov and I can't figure out why.

test_helper.rb:
require 'simplecov'
SimpleCov.start 'rails'

ENV["RAILS_ENV"] = "test"
require File.expand_path('../../config/environment', __FILE__)

I have tried upgrading to Ruby 2.1 after reading some posts that that might help, but still some subfolders are being ignored. I also tried to move the SimpleCov.start into a .simplecov file at the root of my project, but the problem persists. I also tried to remove the rails configuration and add filters and groups myself, but the ignored ones just turn up empty.

Folder like /mailers, /serializers, /presenters are all ignored. It does pick up on /models and /controllers.


Source: (StackOverflow)

SimpleCov :: Periodic Coverage Report

I have a requirement where in, i need to get the coverage so far. If I stop the server, the report gets generated and I do get the coverage so far. But if i start the server again, my previous coverage results are lost and I can only get the coverage after the server was restarted.

Is there a way for me to keep checking periodically for the the coverage% - without stopping the server?

If i try to generate a report without starting the server, by using the following command, in rails console (SimpleCov.result.format! ),I dont get anycoverage number.

The following is my config in my config/boot.rb:

require 'simplecov'
SimpleCov.start 'rails' do
add_filter "/vendor/"
end

Please share your thoughts Thanks Ramya

This is the content of my boot.rb:

require 'simplecov'

# # create coverage directory if it doesn't exist already.
 Dir.mkdir("coverage") unless Dir.exist?("coverage")

 SimpleCov.start 'rails' do
        SimpleCov.use_merging(true)
   add_filter "/vendor/"
   SimpleCov.merge_timeout 30
 end


require 'rubygems'

# Set up gems listed in the Gemfile.
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)

require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])

Source: (StackOverflow)

Simplecov gem using obsolete lockfile boolean argument

I have an application which I have just added Simplecov to in order to get code coverage stats. After adding this Gem, I began getting the following errors:

/Users/KristaOdger/.rvm/gems/jruby-1.7.6/gems/lockfile-2.1.0/lib/lockfile.rb:72 warning: optional boolean argument is obsoleted
/Users/KristaOdger/.rvm/gems/jruby-1.7.6/gems/lockfile-2.1.0/lib/lockfile.rb:73 warning: optional boolean argument is obsoleted
/Users/KristaOdger/.rvm/gems/jruby-1.7.6/gems/lockfile-2.1.0/lib/lockfile.rb:74 warning: optional boolean argument is obsoleted
/Users/KristaOdger/.rvm/gems/jruby-1.7.6/gems/lockfile-2.1.0/lib/lockfile.rb:75 warning: optional boolean argument is obsoleted

(there are about 20 lines like this printed out when I launch the rails server)

They seem to be only warnings, so I'm not super worried, but it would be nice to get rid of them. Both Simplecov and lockfile gems are already the most recent versions (Simplecov 0.8.1 and lockfile 2.1.0), so I can't update one of them... Has anyone else run into this?

Edit: other environment details

Jruby 1.7.6, Rails 3.1.3, Warbler 1.3.8, Bundler 1.3.5


Source: (StackOverflow)

In statement "33.08% covered at 12.13 hits/line" what does hits/line mean?

Recently I was working with rails project , and generated a code coverage report using simplecov, I got a nice report , below. I know it's a simple question ( I googled many times but not getting any proper explanation)

can anyone explain what does hits/line mean.

Also lets say I have a file test.rb (100 lines of code )which was not hit anytime , now lets say some test_script has hit some lines of file test.rb so will the coverage calculated by

 total no.of lines hit / total lines of code , (All files) 
                                      OR
lines hit / total lines of code (file1) .+.+....lines hit/ total line of code (file n) / total number of file count

enter image description here


Source: (StackOverflow)

rails rspec tests for lib class used by routes contains private methods

Simplecov detected that I was missing some tests on my lib/api_verson.rb class:

class ApiVersion

  def initialize(version)
    @version = version
  end

  def matches?(request)
    versioned_accept_header?(request) || version_one?(request)
  end

  private

  def versioned_accept_header?(request)
    accept = request.headers['Accept']
    accept && accept[/application\/vnd\.#{Rails.application.secrets.my_app_accept_header}-v#{@version}\+json/]
  end

  def unversioned_accept_header?(request)
    accept = request.headers['Accept']
    accept.blank? || accept[/application\/vnd\.#{Rails.application.secrets.my_app_accept_header}/].nil?
  end

  def version_one?(request)
    @version == Rails.application.secrets.my_app_default_api_version && unversioned_accept_header?(request)
  end

end

This class is used by the routes file to help setup api versions:

namespace :api, path: "", defaults: {format: :json} do

scope module: :v1, constraints: ApiVersion.new(1) do
  get '/alive', to: 'api#alive'
end

scope module: :v2, constraints: ApiVersion.new(2) do
  get '/alive', to: 'api#alive'
end

end

This setup was ported from versioning_your_ap_is.

I am trying to test the methods here that simplecov is reporting as failures:

require 'spec_helper'

describe ApiVersion do

  before(:each) do
    @apiversion = ApiVersion.new(1)
    @request = ActionController::TestRequest.new(host: 'localhost')
    @request.headers["Accept"] = "application/vnd.#{Rails.application.secrets.my_app_accept_header}-#{Rails.application.secrets.my_app_default_api_version}+json"
  end

  describe 'Method #versioned_accept_header =>' do
    it 'Should return the correct accept header version' do

binding.pry


    end
  end

end

I am trying to build this first test to attempt??? to @apiversion.send(:unversioned_accept_header, @request) and I am getting the error:

@apiversion.send(:unversioned_accept_header, @request)
NoMethodError: undefined method `unversioned_accept_header' for #<ApiVersion:0x007fae009bdad8 @version=1>
from (pry):1:in `block (3 levels) in <top (required)>'

Basically the following methods are flagged: "matches?, versioned_accept_header?, unversioned_accept_header?, and version_one?"

I am not the rockstar at rspec and could use some pointers here. Thank you for your help.

Btw, this is a rails 4 application running:

group :development, :test do
    gem 'pry'
    gem 'pry-doc'
    gem 'pry-debugger'
    gem 'pry-rails'
    gem 'pry-plus'
    gem 'pry-rescue'
    gem 'pry-stack_explorer'
    gem 'pry-clipboard'
    gem 'pry-nav'
  gem 'rspec-rails'
    gem 'factory_girl_rails'
    gem 'faker'
    gem 'seedbank'
    gem 'capybara'
end

group :test do
    gem 'simplecov', '~> 0.7.1'
    gem 'shoulda-matchers'
    gem 'spork-rails'
  gem 'database_cleaner'
  gem 'email_spec'
  gem 'timecop'
    gem 'json_spec'
end

Source: (StackOverflow)

Ruby SimpleCov missing one return line

This is my class which returns the multiplication of parameters

class NetAssetValue

def calculate_net_asset_value(number_of_shares, price)

    number_of_shares * price
  end

end

And this is my Test

require 'net_asset_value'

require 'test/unit'

class NetAssetValueTest  < Test::Unit::TestCase

  def setup

    @asset = NetAssetValue.new

  end


  def test_calculate_net_asset_value_for_a_symbol

    assert_equal(100, @asset.calculate_net_asset_value(20,5))

  end

end

I am using SimpleCov 0.7.1. The coverage report says the one line in the method is not covered though it is being covered.

It says the coverage is 66.67% and the line

number_of_shares * price

is not covered But when I debug in RubyMine and place a breakpoint on that line it is being hit.

Need Help on this. Thank you.


Source: (StackOverflow)