Forum: Ruby on Rails [tip] Test coverage

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ef0db53920b243d6758c2f6b1306df0d?d=identicon&s=25 Steve Ross (cwd)
on 2006-05-25 00:15
(Received via mailing list)
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] http://eigenclass.org/hiki.rb?rcov

------------

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...
Sent from the RubyOnRails Users forum at Nabble.com.
This topic is locked and can not be replied to.