Last active
December 7, 2017 00:22
-
-
Save kaisershahid/041624530a107f0787cebc48d205ea0f to your computer and use it in GitHub Desktop.
Ruby: 42-line barebones unit testing suite
This file contains 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
$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