2

I'm trying to test minitest files like this:

COVERAGE=true ruby -Itest test/views/info_pages_test.rb
COVERAGE=true ruby -Itest test/views/errors_test.rb

now my info_pages_test has 97% coverage and my errors_test has 75% coverage. Together they should cover 100%.. but each time I run the above commands, I get one result or the other 75% or 97%. Is there a way to combine the results of the two test files into one coverage report?

Help would be greatly appreciated!

Here is the top of my minitest_helper.rb file

## SimpleCOV

require 'simplecov'

if ENV["COVERAGE"]

  SimpleCov.start('rails') do

    add_filter "/test/"

  end
  puts "Started SimpleCOV"
end

I also have a .simplecov file in the application root but using it gives me unpredictable results.. I get 100% coverage once in a while and it is unpredictable..

.simplecov file

SimpleCov.use_merging true
SimpleCov.merge_timeout 3600
Milos Blasko
  • 614
  • 10
  • 25
sambehera
  • 959
  • 3
  • 13
  • 33

1 Answers1

5

The problem you are bumping into is that each of those "test suites" will overwrite each other because the suite name (configurable via SimpleCov.command_name 'xyz'), as opposed to when merging for example cucumber and rspec results.

Preferred solution: Generate the coverage report by running the whole test suite at once, using rake test or some other, similar facility.

If you insist on running individual test files you can trick SimpleCov into merging those results instead of overwriting them by supplying a pseudo-random command name, i.e. SimpleCov.command_name "MiniTest #{Time.now}", or (depending on your setup) using ARGV, i.e. SimpleCov.command_name "Minitest #{File.basename(ARGV[1])}". The latter has the advantage of not duplicating results on re-runs of the same test file since those will be overwritten on merge, but may fail when you run all your tests and do not check for the presence of ARGV correctly, or your test framework tampers with ARGV before you can grab it.

Although you can make this work for individual test runs, in general I'd recommend to base coverage reports off full test suite runs only, as the other approaches leave room for error.

TheDeadSerious
  • 842
  • 8
  • 11
  • So how do you recommend I run the full "suite" (2 files as of now under "test/*/*_test.rb") using rake? Also, I'm assuming that if i figure out the rake route.. i no longer need the .simplecov file in my application root? would love some help!! .. wait i think ive seen you on github while i was researching this! – sambehera Mar 26 '13 at 01:21
  • Full disclosure: I'm the guy who wrote simplecov :) I am not using minitest myself, so I'm no expert, but this might help: http://stackoverflow.com/questions/4788288/how-to-run-all-the-tests-with-minitest - and you will still need either `.simplecov` or `SimpleCov.start` at the top of your `test_helper.rb` in order to launch simplecov's coverage detection – TheDeadSerious Mar 26 '13 at 10:42
  • 1
    wonderful.. I figured out how to run all minitest tests under a single rake task from here (http://stackoverflow.com/questions/10596478/how-to-run-a-full-minitest-suite-without-rake?lq=1) and now SimpleCOV works perfectly! Its a bit slow (even with zeus..) but at least I get proper coverage reports when i need them!! – sambehera Apr 03 '13 at 01:44
  • Cool, happy faring! By the way, on MRI (the regular Ruby implementation, as opposed to i.e. JRuby or Rubinius) the coverage should barely impact performance, actually below 5%, so SimpleCov's recommendation is to just keep it on all the time :) – TheDeadSerious Apr 05 '13 at 10:45