[tip] Test coverage

I was using rcov[1] on my tests and was pleasantly surprised to see that
much
of my code was being exercised, but somehow the number of lines of test
code
did not sync up with what I expected so I went looking for models and
controllers that were missing unit and functional tests. Here is a rake
task
you can dump into your Rakefile or /lib/tasks. You can then run:

rake missing_tests

and it will let you know where you have failed to create a test file (it
offers no opinion on how meaningful your tests are :). I really
recommend
rcov as another tool if you are looking for some automated feedback
about
what code is being executed by your tests.

Comments welcome!

[1] eigenclass.org


desc “Look for possibly missing tests in unit and functional
directories”
task :missing_tests
models = Dir.glob(“app/models//*.rb")
controllers = Dir.glob("app/controllers/
/.rb")
unit_tests = Dir.glob("test/unit/**/
.rb”)
functional_tests = Dir.glob(“test/functional/**/*.rb”)

models.each do|model|
original_dir, suspected_test = File.split(model)
suspected_test.sub!(‘.rb’, ‘’)
test_found = !unit_tests.select{|s| s =~
/#{suspected_test}_test/}.empty?
puts “unit test apparently missing for #{model}” unless test_found
end

controllers.each do|controller|
original_dir, suspected_test = File.split(controller)
suspected_test.sub!(‘.rb’, ‘’)
test_found = !functional_tests.select{|s| s =~
/#{suspected_test}_test/}.empty?
puts “functional test apparently missing for #{controller}” unless
test_found
end


View this message in context:
http://www.nabble.com/-tip-+Test+coverage-t1678002.html#a4550279
Sent from the RubyOnRails Users forum at Nabble.com.