Skip to content

Instantly share code, notes, and snippets.

@jhubert
Created October 22, 2012 23:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jhubert/3935459 to your computer and use it in GitHub Desktop.
Save jhubert/3935459 to your computer and use it in GitHub Desktop.
Describe your unit tests in an easy to read format
namespace :test do
# Describe the tests that are run for the provided class or all classes
task :describe, :klass do |t, args|
name = args[:klass] ? args[:klass].underscore : '*'
classes = {}
files = Dir["test/*/#{name}_test.rb"]
files.each do |f|
file = File.read(f)
test_class = file.scan(/class\s([A-Z][\w]+)Test/).flatten
next unless test_class
test_class = test_class.first if test_class.is_a?(Array)
classes[test_class] = file.scan(/\s+test\s*\"(.*)\"\s*do/).flatten
end
classes.each do |name, tests|
print "#{name}\n"
tests.each do |test|
print " - #{test}\n"
end
puts ""
end
end
end
> rake test:describe[ClassName] (ex: User)
User
- should have a valid email address
- should have a valid phone number
...
> rake test:describe
# describes all tested classes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment