Skip to content

Instantly share code, notes, and snippets.

@dlitvakb
Created November 15, 2016 14:35
Show Gist options
  • Select an option

  • Save dlitvakb/947bda3a25a2bb726b8370377f8ee7f9 to your computer and use it in GitHub Desktop.

Select an option

Save dlitvakb/947bda3a25a2bb726b8370377f8ee7f9 to your computer and use it in GitHub Desktop.
class MyTest
include Mini::Unit
def test_foo
assert(2, 1)
end
end
MyTest.run!
module Mini
class FailureException < Exception
end
class Failure
attr_reader :message, :test
def initialize(message, test)
@message = message
@test = test
end
def to_s
"In #{test}: #{message}"
end
end
class Error
attr_reader :error, :test
def initialize(error, test)
@error = error
@test = test
end
def to_s
"In #{test}: #{error.inspect}\n\tBacktrace:\n\t\t#{error.backtrace.join("\n\t\t")}"
end
end
class Skip
attr_reader :message, :test
def initialize(message, test)
@message = message
@test = test
end
def to_s
"#{test} - #{message}"
end
end
module Unit
def initialize
@tests = self.methods.map(&:to_s).select { |m| m.start_with?('test') }.shuffle
@passes = 0
@failures = []
@errors = []
@skips = self.methods.map(&:to_s).select { |m| m.start_with?('_test') }.map { |test| Skip.new("Skipped with '_test'", test) }
end
def assert(expectee, expected)
failure("Assertion failed:\n\tExpected: '#{expected}'\n\tBut got: '#{expectee}'", caller_locations(1,1)[0]) if expectee != expected
end
def pass
end
def success
print "."
@passes += 1
end
def failure(message, test = nil)
action(@failures, Failure, 'F', message, test)
raise FailureException.new
end
def error(exception, test = nil)
action(@errors, Error, 'E', exception, test)
end
def skip(message, test = nil)
action(@skips, Skip, 'S', message, test)
end
def run!
puts "Starting Integration Tests Run for #{api_name}:"
puts
start = Time.now
execute_specs
duration = Time.now - start
end_report(duration)
exit(red? ? 1 : 0)
end
private
def red?
!green?
end
def green?
@failures.empty? && @errors.empty?
end
def execute_specs
@tests.each do |test|
begin
self.send(test)
success
rescue FailureException
next
rescue SystemExit, Interrupt
return
rescue Exception => e
error(e, test)
end
end
end
def action(queue, queue_class, print_char, message, test = nil)
print print_char
test = caller_locations(2,1)[0] if test.nil?
queue << queue_class.new(message, test)
end
def print_summary(duration)
puts
puts
puts "Finished in: %.6f seconds" % duration
puts "Passed: #{@passes} - Failed: #{@failures.size} - Errored: #{@errors.size} - Skipped: #{@skips.size}"
end
def print_failures
print_kind('failures')
end
def print_errors
print_kind('errors')
end
def print_skips
print_kind('skips')
end
def print_kind(name)
measure = instance_variable_get("@#{name}".to_sym)
return if measure.empty?
puts
puts "#{name.capitalize}:"
measure.each { |m| puts "\n#{m}" }
end
def end_report(duration)
print_summary(duration)
print_failures
print_errors
print_skips
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment