Created
March 15, 2012 00:44
-
-
Save natritmeyer/2040769 to your computer and use it in GitHub Desktop.
(Possibly) The World's Smallest Ruby Unit Test Tool
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require './tinytest' | |
class MyTests < TinyTest | |
def setup | |
@name = "Bob" | |
end | |
def teardown | |
@name = "" | |
end | |
def test_name_length_is_greater_than_zero | |
assert @name.length > 0 | |
end | |
def test_name_length_is_greater_than_ninety_nine | |
assert @name.length > 99 | |
end | |
def test_name_begins_with_b | |
assert @name[0].downcase == "b" | |
end | |
end | |
class LookNoTests < TinyTest | |
def not_a_test_method | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ruby tests.rb | |
.F. | |
Failed Tests: | |
MyTests#test_name_length_is_greater_than_ninety_nine, tests.rb:17 | |
2 passed, 1 failed |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TinyTest | |
def assert(result) | |
raise caller.first unless result == true | |
end | |
end | |
at_exit do | |
@pass_count, @fail_count, test_classes, failures = 0, 0, [], [] | |
ObjectSpace.each_object(Class) { |obj| test_classes << obj if obj.ancestors.include?(TinyTest) && obj != TinyTest && obj.instance_methods.select {|method_name| method_name =~ /^test_/}.size > 0 } | |
test_classes.each do |test_class| | |
test_class.instance_methods.select {|method_name| method_name =~ /^test_/}.each do |test_method_name| | |
begin | |
tc = test_class.new | |
tc.setup if tc.respond_to? :setup | |
tc.send test_method_name.to_sym if tc.respond_to? test_method_name.to_sym | |
tc.teardown if tc.respond_to? :teardown | |
@pass_count += 1 | |
print "." | |
rescue => e | |
@fail_count += 1 | |
failures << "#{test_class.name}\##{test_method_name}, #{e.message.scan(/^(.*):/).flatten.first}" | |
print "F" | |
end | |
end | |
end | |
puts "\n\nFailed Tests:" | |
failures.each {|failure| puts failure} | |
puts "\n#{@pass_count} passed, #{@fail_count} failed" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment