I am trying to use SimpleCov to gather code coverage information but I can't seem to understand how it works. It always give me 0.0% LOC. Do I need to do something to make it work?
require 'simplecov'
SimpleCov.start
SimpleCov.command_name 'Unit Tests'
def foo
puts '12345'
end
foo
I see the following output:
$ ruby mytest.rb
12345
Coverage report generated for Unit Tests to /private/tmp/simpletest/coverage. 0.0 / 0.0 LOC (100.0%) covered.
Do I have to use it under rspec or some special environment? Can I not just enable code coverage for arbitrary code?
SimpleCov filters the original source file itself out of the coverage report. To make it work standalone like this, you need to put your test code into a separate file:
require 'simplecov'
SimpleCov.start
SimpleCov.command_name 'Unit Tests'
require_relative 'my_code'
foo
Then in my_code.rb
:
def foo
puts '12345'
end
Now you will get a proper report.