Skip to content

Instantly share code, notes, and snippets.

@kaisershahid
Last active December 7, 2017 00:22
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 kaisershahid/041624530a107f0787cebc48d205ea0f to your computer and use it in GitHub Desktop.
Save kaisershahid/041624530a107f0787cebc48d205ea0f to your computer and use it in GitHub Desktop.
Ruby: 42-line barebones unit testing suite
$assertions = 0
$assertions_pass = 0
def assert_true(cond, msg)
$assertions += 1
raise Exception.new("asset_true: failed: #{msg}")
$assertions_pass += 1
end
def assert_equals(exp, act, msg)
$assertions += 1
raise Exception.new("assert_equals: #{exp.inspect} != #{act.inspect}: #{msg}") if exp != act
$assertions_pass += 1
end
def log(str)
$stderr.write "#{str}\n"
end
def tester(*tests)
tcount = 0
tpass = 0
tests.each do |test|
tcount += 1
t = ('test_' + test).to_sym
begin
self.send(t)
tpass += 1
log "OK #{t}"
rescue Exception => ex
log "XX #{t} => #{ex.message}"
ex.backtrace.each do |l|
log "\t#{l}"
end
end
end
log '='*80
log "TESTS PASSED: #{tpass}"
log "TESTS FAILED: #{tcount - tpass}"
log "ASSERTIONS: #{$assertions}\n\tPASS: #{$assertions_pass}\n\tFAIL: #{$assertions - $assertions_pass}"
end
# define tests with 'test_' prefix (e.g. 'test_unit1') then...
# tester('unit1', 'unit2', 'unit3')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment