Permalink
Browse files

rspec model, controller, view, lib, and helper isolated coverage, upd…

…ated docs, bumped version to 1.1.7, and moved the rcov test:unit tasks around
  • Loading branch information...
1 parent 8a16704 commit 19a24de8482b7546b30531d42d24e10d22359a36 @napcs committed Jun 12, 2009
Showing with 162 additions and 58 deletions.
  1. +47 −14 README.rdoc
  2. +1 −1 tasks/lazy_developer_tasks.rake
  3. +114 −43 tasks/rcov.rake
View
@@ -66,12 +66,12 @@ task drops your tables and starts over by dropping the tables directly and then
re-running your migrations. This provides a great way to test to make sure you
haven't broken migrations at some point, which *will* happen to you at some point.
-=== rake db:to_yaml (formerly rake:db:import)
+=== rake db:to_yaml
Dump your database to fixtures. Stores them in RAILS_ROOT/production_data. You can then use this to load the data back into another database, even one of a different type. We've used this to move
data from SQL Server to MySQL and back again.
-=== rake db:from_yaml (formerly rake:db:export)
+=== rake db:from_yaml
Load fixtures from RAILS_ROOT/production_data into your database. Loads fixtures dumped by using rake db:export
== Subversion
@@ -89,23 +89,56 @@ Displays the last tag.
Creates a new tag from the trunk.
== Rcov
-Thanks to Kevin Gisi for these tasks that make running RCov a bit easier.
-Requires the RCov gem and the rails_rcov plugin
+We're fans of RCov and we've included some ways to make it easier to use RCov in your projects.
-=== rake rcov:models
+=== Test::Unit
+
+In order to use the Test::Unit coverage tasks, you'll need to install the Rails_rcov plugin
+
+ ruby script/plugin install http://svn.codahale.com/rails_rcov/
+
+This is not needed if your project uses Rspec.
+
+==== rake test:models:rcov
Runs coverage on your models
-=== rake rcov:controllers
+=== rake test:controllers:rcov
Runs coverage on your controllers
-=== rake rcov:full
+==== rake test:rcov:full
Runs coverage on models and controllers
+=== RSpec
+
+Rspec already includes the ability to get code coverage, but we made it just as focused and granular as we made the tasks for Test::Unit
+
+==== rake spec:models:rcov
+
+Coverage for models
+
+==== rake spec:controllers:rcov
+
+Coverage for controllers
+
+==== rake spec:views:rcov
+
+Coverage for views
+
+==== rake spec:helpers:rcov
+
+Coverage for helpers
+
+==== rake spec:lib:rcov
+
+Coverage for files in lib/
+
+== Test::Unit
+
+=== Running individual tests
-== Tests
Based on an idea from Geoffrey Grosenbach, you can run all tests in units\user_test.rb by doing
rake test:units:user:all
@@ -119,19 +152,19 @@ The same rules apply to functional tests
rake test:functionals:users:list
-== Specs
+== RSpec
RSpec gets some love here. If you need nice output for your specs, we overrode
some of the built-in specs.
-=== Running all model specs
+=== rake spec:models
- rake spec:models
-
-=== Running all controller specs
+Runs all model specs
- rake spec:controllers
+=== rake spec:controllers
+Runs all controller specs
+
=== Running a specific model or controller spec
rake spec:model:user
@@ -1,4 +1,4 @@
-VERSION="1.1.5"
+VERSION="1.1.7"
View
@@ -1,62 +1,133 @@
+namespace :spec do
+ require 'spec/rake/spectask'
+ spec_prereq = File.exist?(File.join(RAILS_ROOT, 'config', 'database.yml')) ? "db:test:prepare" : :noop
+
+
+ [:models, :controllers, :views, :helpers, :lib].each do |sub|
+ namespace sub do
+ desc "RCov for #{sub}"
+ Spec::Rake::SpecTask.new("rcov" => spec_prereq) do |t|
+ t.spec_opts = ['--options', "\"#{RAILS_ROOT}/spec/spec.opts\""]
+ t.spec_files = FileList["spec/#{sub}/**/*_spec.rb"]
+ t.rcov = true
+
+ t.rcov_opts = lambda do
+ reg_exp = []
+ reg_exp << case sub.to_s
+ when 'models'
+ 'app\/models'
+ when 'views'
+ 'app\/views'
+ when 'controllers'
+ 'app\/controllers'
+ when 'helpers'
+ 'app\/helpers'
+ when 'lib'
+ 'lib'
+ end
+ reg_exp.map!{ |m| "(#{m})" }
+ rcov_opts = " -x \"^(?!#{reg_exp.join('|')})\""
+ rcov_opts.map {|l| l.chomp.split " "}.flatten
+ end
+
+ end
+ end
+ end
+end
+
+namespace :test do
+
+ namespace :models do
+ desc "Run unit tests, and determine coverage on models."
+ task :rcov do
+ ENV["SHOW_ONLY"] = "m"
+ Rake::Task['test:units:rcov'].invoke
+ end
+ end
+
+ namespace :controllers do
+ desc "Run functional tests, and determine coverage on controllers."
+ task :rcov do
+ ENV["SHOW_ONLY"] = "c"
+ Rake::Task['test:functionals:rcov'].invoke
+ end
+ end
+
+ namespace :rcov do
+
+ # Runs all unit and functional tests, and determines
+ # coverage on models and controllers respectively.
+ desc "Run functional and unit tests, and get coverage for both."
+ task :full do
+ Rake::Task['rcov:models'].invoke
+ Rake::Task['rcov:controllers'].invoke
+ end
+
+
+
+ # Runs all unit and functional tests, and determines
+ # coverage on models and controllers respectively, ignoring errors.
+ desc "Run functional and unit tests, and get coverage for both, ignoring errors."
+ task :report do
+ begin
+ Rake::Task['test:models:rcov'].invoke
+ ensure
+ begin
+ Rake::Task['test:controllers:rcov'].invoke
+ ensure
+ units = File.open("#{Dir.pwd}/coverage/units/index.html","r").read
+ units = units[units.index("coverage_code")+15,10]
+ units = units[0,units.index("<")]
+ functionals = File.open("#{Dir.pwd}/coverage/functionals/index.html","r").read
+ functionals = functionals[functionals.index("coverage_code")+15,10]
+ functionals = functionals[0,functionals.index("<")]
+ puts "+-----------------------------+"
+ puts "| UNIT COVERAGE: #{units.ljust(6)} |"
+ puts "| FUNCTIONAL COVERAGE: #{functionals.ljust(6)} |"
+ puts "+-----------------------------+"
+ to_write = "<html><body><h1>Rcov Report</h1>
+ <div style=\"width:100%; height:50px; border: 1px solid black\"><div style=\"height: 50px; width: #{units}%; background-color: #33FF00; font-size: 18pt; font-style: bold\"><a href=\"units/index.html\">Unit: #{units}</a></div></div><br />
+ <div style=\"width:100%; height:50px; border: 1px solid black\"><div style=\"height: 50px; width: #{functionals}%; background-color: #33FF00; font-size: 18pt; font-style: bold\"><a href=\"functionals/index.html\">Functional: #{functionals}</a></div></div>
+ </body></html>"
+ File.open("coverage/report.html", "w") do |f|
+ f.puts to_write
+ end
+
+ if PLATFORM['darwin']
+ system("open #{pwd}/coverage/report.html")
+ end
+ end
+ end
+ end
+ end
+
+end
+
+
namespace :rcov do
# Runs all unit tests, and determines coverage
# only on the model level.
desc "Run unit tests, and determine coverage on models."
task :models do
- ENV["SHOW_ONLY"] = "m"
- Rake::Task['test:units:rcov'].invoke
+ Rake::Task['test:models:rcov'].invoke
end
# Runs all functional tests, and determines coverage
# only on the controller level.
desc "Run functional tests, and determine coverage on controllers."
task :controllers do
- ENV["SHOW_ONLY"] = "c"
- Rake::Task['test:functionals:rcov'].invoke
+ Rake::Task['test:controllers:rcov'].invoke
end
- # Runs all unit and functional tests, and determines
- # coverage on models and controllers respectively.
- desc "Run functional and unit tests, and get coverage for both."
- task :full do
- Rake::Task['rcov:models'].invoke
- Rake::Task['rcov:controllers'].invoke
- end
-
- # Runs all unit and functional tests, and determines
- # coverage on models and controllers respectively, ignoring errors.
desc "Run functional and unit tests, and get coverage for both, ignoring errors."
task :report do
- begin
- Rake::Task['rcov:models'].invoke
- ensure
- begin
- Rake::Task['rcov:controllers'].invoke
- ensure
- units = File.open("#{Dir.pwd}/coverage/units/index.html","r").read
- units = units[units.index("coverage_code")+15,10]
- units = units[0,units.index("<")]
- functionals = File.open("#{Dir.pwd}/coverage/functionals/index.html","r").read
- functionals = functionals[functionals.index("coverage_code")+15,10]
- functionals = functionals[0,functionals.index("<")]
- puts "+-----------------------------+"
- puts "| UNIT COVERAGE: #{units.ljust(6)} |"
- puts "| FUNCTIONAL COVERAGE: #{functionals.ljust(6)} |"
- puts "+-----------------------------+"
- to_write = "<html><body><h1>Rcov Report</h1>
- <div style=\"width:100%; height:50px; border: 1px solid black\"><div style=\"height: 50px; width: #{units}%; background-color: #33FF00; font-size: 18pt; font-style: bold\"><a href=\"units/index.html\">Unit: #{units}</a></div></div><br />
- <div style=\"width:100%; height:50px; border: 1px solid black\"><div style=\"height: 50px; width: #{functionals}%; background-color: #33FF00; font-size: 18pt; font-style: bold\"><a href=\"functionals/index.html\">Functional: #{functionals}</a></div></div>
- </body></html>"
- File.open("coverage/report.html", "w") do |f|
- f.puts to_write
- end
-
- if PLATFORM['darwin']
- system("open #{pwd}/coverage/report.html")
- end
- end
- end
+ Rake::Task['test:rcov:report'].invoke
+ end
+
+ desc "Run functional and unit tests, and get coverage for both."
+ task :full do
+ Rake::Task['test:rcov:full'].invoke
end
end

0 comments on commit 19a24de

Please sign in to comment.