SimpleCov :: Periodic Coverage Report - ruby

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'])

The pre-requisites for SimpleCov to work properly are documented here: Getting Started with SimpleCov. You must be having the SimpleCov related code inside the boot.rb file after the Rails loading code. This is wrong. Promote all that code to the top and the SimpleCov.result.format! method will work inside the console.
However, it's generally a bad idea to have any extra code inside the boot.rb. Usually, coverage reports are needed only in the test environment (when the code is committed and a continuous integration server like Travis runs the full test suite and generates a coverage report). Hence, the documentation refers to this style of setup where everything related to SimpleCov runs in the test environment. The first topic in the Getting Started section mentions that you need to have the SimpleCov.start line at the beginning of the test_helper file (spec_helper.rb if you're using Rspec) since that is the file that loads the Rails environment; which means that you end up loading SimpleCov and it's configuration before loading the actual application code and you get a correct output.

require 'simplecov'
SimpleCov.start do
coverage_dir "custom-coverage_"+Time.now.strftime("%m_%d_%Y").to_s
end

Related

Making SimpleCov work when executing gem binary

I am trying to add SimpleCov coverage to my gem that has a binary.
I would like to test its command line interface, so I wish to test the binary execution itself, and not the library it uses.
I am getting no coverage report (0 LOC) by SimpleCov.
As I understand, the issue is most likely due to the fact that in my test (either cucumber features or rspec specs), I am executing the gem's binary with system or popen3, but I don't know how I can tell SimpleCov to "follow through" (or if I am barking at the right tree...).
I have tried playing with SimpleCov.command_name, SimpleCov.pid = $$, SimpleCov.track_files and almost every other remotely related configuration I found.
I do not wish to use Aruba, although I have tried reviewing their source to look for possible answers.
Related code snippets:
# spec_helper.rb
require 'simplecov'
SimpleCov.start
require 'rubygems'
require 'bundler'
Bundler.require :default, :development
# test_spec.rb
require 'spec_helper'
describe "my bin" do
it "should be covered" do
system 'bin/runme'
end
end
I have prepared a minimal repo as an easy testing ground, if that helps at all.
well it's not easy to find a solution to this issue in particular.
not sure this solves your problem but maybe a start?
without the start block which modifies the filters i could not get SimpleCov to watch the bin directory (using the sample github repo you provided).
Used command_name to give the main processes coverage reports a name and then in the fork used command_name to give the forked processes report a name (SimpleCov merges them for us as long as they have different names).
then used load to load the bin file instead of using system.
(I couldn't figure out a way to make system or spawn add to the coverage reports, maybe if you called it through a script that restarts SimpleCov for you with an alternate command_name)
again, not sure if this is exactly what you are looking for but may be a start. code below:
# spec_helper.rb
require 'simplecov'
SimpleCov.command_name "main_report"
SimpleCov.start do
filters.clear # This will remove the :root_filter and :bundler_filter that come via simplecov's defaults
add_filter do |src|
!(src.filename =~ /^#{SimpleCov.root}/) unless src.filename =~ /bin/ #make sure the bin directory is allowed
end
end
require 'rubygems'
require 'bundler'
Bundler.require :default, :development
# test_spec.rb
require 'spec_helper'
describe "my bin" do
it "should be covered" do
pid = Process.fork do
SimpleCov.start do
command_name "bin_report_section"
end
load "bin/runme"
end
end
end
result:
Coverage report generated for bin_report_section, main_report to
/home/korreyd/simplecov-debug/coverage.
1 / 1 LOC (100.0%) covered.
Have you tried this? https://blog.yossarian.net/2018/04/01/Code-coverage-with-Simplecov-across-multiple-processes
Basically, in your spec_helper.rb
if ENV["COVERAGE"]
require "simplecov"
# Only necessary if your tests *might* take longer than the default merge
# timeout, which is 10 minutes (600s).
SimpleCov.merge_timeout(1200)
# Store our original (pre-fork) pid, so that we only call `format!`
# in our exit handler if we're in the original parent.
pid = Process.pid
SimpleCov.at_exit do
SimpleCov.result.format! if Process.pid == pid
end
# Start SimpleCov as usual.
SimpleCov.start
end
Then inside your bin/runme, add:
if ENV["COVERAGE"]
# Give our new forked process a unique command name, to prevent problems
# when merging coverage results.
SimpleCov.command_name SecureRandom.uuid
SimpleCov.start
end
Child process' test coverage will merge into the parent's process.
If you use SimpleCov's coverage_dir, make sure it's in all SimpleCov.start blocks so that results are written to same location.

Rspec: requiring spec/workers

I've got a bunch of Sidekiq workers in app/workers, and some matching specs in spec/workers. How can I add both to my rspec runs? As in stands none are included when I hit up rspec on the Terminal.
I see a few alternatives for your situation:
Rspec loading:
Make sure your test files in spec/worker follow the pattern setup for Rspec (e.g. *.rb or *_spec.rb).
Require the worker files in the spec_helper.rb.
Manual loading: Require the spec/worker and app/worker files in your spec_helper.rb with a Ruby-defined glob.
Sidekiq helper gem: Add the rspec-sidekiq gem to your project, as explained at Sidekiq's.
To add a bulk of files to the load path, you can either:
Add path strings to the $: global variable, early in your boot process (e.g. at the beginning of spec_helper.rb):
$:.unshift(File.expand_path('../app/workers/**/*_worker.rb', File.dirname(__FILE__))
Use a glob to load the files without modifying the load path:
Dir[File.expand_path('../app/workers/**/*_worker.rb', File.dirname(__FILE__))].each do |file|
require file
end

How to quickly test Class behavior in ruby

I'm building a class-based Tic-tac-toe game with all the classes in tic_tac_toe.rb. I can load the class into irb for interactive testing with irb -r ./tic_tac_toe.rb, but I have to manually create a player and gameboard instance every time. I included p1 = Player.new int tic_tac_toe.rb but that does not seem to run.
More generally, is what I'm doing a good workflow or not? How should I go about writing some code for my class and testing it and going back? (Is there something simpler than unit testing for this small project?)
To directly address your question, you can simplify your workflow greatly with the addition of RSpec. RSpec is a BDD (behavior driven development) tool for Ruby that will let you describe your classes in an (arguably) more descriptive way than plain jane unit tests. I have included a small code sample below to help get you started.
Create a Gemfile if you do not have one for your project and add RSpec. If you've never done this check out Bundler for more information on Gemfiles.
# in your Gemfile
gem 'rspec' # rspec testing tool
gem 'require_relative' # allows you to require files with relative paths
Create a spec folder to house your specs (specs are what RSpec calls its tests).
# via Command Line (or in Windows Explorer) create a spec folder in your project
mkdir spec
Create a spec_helper.rb in the spec/ folder to house the configuration for your tests.
# in spec/spec_helper.rb
require "rspec" # require rspec testing tool
require_relative '../tic_tac_toe' # require the class to be tested
config.before(:suite) do
begin
#=> code here will run before your entire suite
#first_player = Player.new
#second_player = Player.new
ensure
end
end
Now that you've setup two players before your test suite runs, you can use these in your tests. Create a spec for your class that you would like to test and suffix it with _spec.
# in spec/player_spec.rb
require 'spec_helper' # require our setup file and rspec will setup our suite
describe Player do
before(:each) do
# runs before each test in this describe block
end
it "should have a name" do
# either of the bottom two will verify player's name is not nil, for example
#first_player.name.nil? == false
#first_player.name.should_not be_nil
end
end
Run these tests from the root of your project by using bundle exec rspec. This will look for a spec/ folder, load the spec helper, and run your specs. There is much more you can do with RSpec, such as work in Factories etc (this would be for larger projecxts). However for your project you would only need a few specs for your classes.
Other things I would suggest would be RSpec-Given, when you have a firm grasp of rspec. This gem helps DRY up your rspec tests and makes them a bit more readable.
You can also look into Guard and creating a Guardfile, which will watch your files for you and run tests when you change files.
Lastly, I included a small suggestion on a basic project structure to visualize this a bit easier.
/your_project
--- Gemfile
--- tic_tac_toe.rb
--- spec/
------- spec_helper.rb
------- player_spec.rb
I have linked all the referenced docs so if you have any questions definitely check the links out. The documentation on Bundler, RSpec, RSpec-Given, and Guard is pretty decent. Happy programming.

How do I require a ruby file from child directory

I have the following structure
tests
specific tests
first_test.rb
more tests
second_test.rb
test_helper.rb
Now I am trying to require test_helper in both first_test and second_test. The following works
require '../test_helper.rb'
and from command line I should be under specific tests and execute
ruby first_test.rb
How can I get more flexibility ie. I want to be in any directory and execute these tests. Currently I am getting 'no such file'. This is not a rails app but in rails I could simply do require 'test_helper.rb'
Bonus points for making this work with rake.
Put your test_helper and other helpers in a separate directory like
/home/XXX/projects/test_project/lib/
or wherever you like and use base directory like
Dir["base/path/to/your/helper/*.rb"].each {|file| require file }
or from my above example
Dir["/home/XXX/projects/test_project/lib/*.rb"].each {|file| require file }
Thats it.
Now add this line to the beginning of every test you want to run separately from any directory.And forget about the directory issue.

SimpleCov 0% coverage

I'm working on a small gem and included simplecov to spec_helper.rb two lines:
require 'simplecov'
SimpleCov.start
When I run the rspec test, the simplecov seems started correctly but the report is not:
Finished in 0.00214 seconds
8 examples, 0 failures
Coverage report generated for /home/......
spec to /home/megas/Work/calc/coverage. 0 / 0 LOC (0.0%) covered.
What might be a problem and how to fix it? Thanks
Also make sure to enable simplecov (a.k.a. SimpleCov.start) at the very beginning of your file; especially before you require your code.
I had the same symptoms. My problem was that in my test file:
#spec/oneclass_spec.rb
require 'oneclass'
require 'spec_helper'
...Rest of the test file
And I needed to change the order of the requires to:
#spec/oneclass_spec.rb
require 'spec_helper'
require 'oneclass'
...Rest of the test file
Hope this helps someone, I was going crazy...
I had a similar issue. For some reason, some modules were reported with 0% of coverage. After some investigation, I found that one of the initialisers required a controller, which caused modules to be loaded before Simplecov. What I did I moved Simplecov to initialiser:
# config/initializers/_coverage_rspec.rb
if Rails.env.test?
require 'simplecov'
SimpleCov.start 'rails'
end
If you're using spring, remember to turn it off when you run tests with coverage. In another case, the complete application code will be loaded before SimpleCov and report 0% coverage.
Just in case the above two answers didn't work (as in my case) a user on simplecov's github issues page suggested this, which worked for me.
Add this after you require simplecov-
module SimpleCov::Configuration
def clean_filters
#filters = []
end
end
SimpleCov.configure do
clean_filters
load_adapter 'test_frameworks'
end
If one of the above didn't work.
verify that in test.rb:
config.eager_load = false
In my case the issue was spring - I had to create a config/spring.rb with the following:
if ENV['RAILS_ENV'] == 'test'
require 'simplecov'
SimpleCov.start
end
as documented here.
I am running scripts from the command line and I found the solution was simply to put an exit at the end of my script. Doh!
Alternatively, the following also works
SimpleCov.at_exit do
SimpleCov.result.format!
end

Resources