MiniTest Assertions. All assertion methods accept a msg which is printed if the assertion fails.
Fails unless test is a true value.
# File lib/minitest/unit.rb, line 74 74: def assert test, msg = nil 75: msg ||= "Failed assertion, no message given." 76: self._assertions += 1 77: unless test then 78: msg = msg.call if Proc === msg 79: raise MiniTest::Assertion, msg 80: end 81: true 82: end
Fails unless the block returns a true value.
# File lib/minitest/unit.rb, line 87 87: def assert_block msg = nil 88: msg = message(msg) { "Expected block to return true value" } 89: assert yield, msg 90: end
Fails unless obj is empty.
# File lib/minitest/unit.rb, line 95 95: def assert_empty obj, msg = nil 96: msg = message(msg) { "Expected #{obj.inspect} to be empty" } 97: assert_respond_to obj, :empty? 98: assert obj.empty?, msg 99: end
Fails unless exp == act.
For floats use assert_in_delta
# File lib/minitest/unit.rb, line 106 106: def assert_equal exp, act, msg = nil 107: msg = message(msg) { "Expected #{mu_pp(exp)}, not #{mu_pp(act)}" } 108: assert(exp == act, msg) 109: end
For comparing Floats. Fails unless exp and act are within delta of each other.
assert_in_delta Math::PI, (22.0 / 7.0), 0.01
# File lib/minitest/unit.rb, line 117 117: def assert_in_delta exp, act, delta = 0.001, msg = nil 118: n = (exp - act).abs 119: msg = message(msg) { "Expected #{exp} - #{act} (#{n}) to be < #{delta}" } 120: assert delta >= n, msg 121: end
For comparing Floats. Fails unless exp and act have a relative error less than epsilon.
# File lib/minitest/unit.rb, line 127 127: def assert_in_epsilon a, b, epsilon = 0.001, msg = nil 128: assert_in_delta a, b, [a, b].min * epsilon, msg 129: end
Fails unless collection includes obj.
# File lib/minitest/unit.rb, line 134 134: def assert_includes collection, obj, msg = nil 135: msg = message(msg) { 136: "Expected #{mu_pp(collection)} to include #{mu_pp(obj)}" 137: } 138: assert_respond_to collection, :include? 139: assert collection.include?(obj), msg 140: end
Fails unless obj is an instace of cls.
# File lib/minitest/unit.rb, line 145 145: def assert_instance_of cls, obj, msg = nil 146: msg = message(msg) { 147: "Expected #{mu_pp(obj)} to be an instance of #{cls}, not #{obj.class}" 148: } 149: 150: assert obj.instance_of?(cls), msg 151: end
Fails unless obj is a kind of cls.
# File lib/minitest/unit.rb, line 156 156: def assert_kind_of cls, obj, msg = nil # TODO: merge with instance_of 157: msg = message(msg) { 158: "Expected #{mu_pp(obj)} to be a kind of #{cls}, not #{obj.class}" } 159: 160: assert obj.kind_of?(cls), msg 161: end
Fails unless exp is =~ act.
# File lib/minitest/unit.rb, line 166 166: def assert_match exp, act, msg = nil 167: msg = message(msg) { "Expected #{mu_pp(exp)} to match #{mu_pp(act)}" } 168: assert_respond_to act, :"=~" 169: exp = /#{Regexp.escape exp}/ if String === exp && String === act 170: assert exp =~ act, msg 171: end
Fails unless obj is nil
# File lib/minitest/unit.rb, line 176 176: def assert_nil obj, msg = nil 177: msg = message(msg) { "Expected #{mu_pp(obj)} to be nil" } 178: assert obj.nil?, msg 179: end
For testing equality operators and so-forth.
assert_operator 5, :<=, 4
# File lib/minitest/unit.rb, line 186 186: def assert_operator o1, op, o2, msg = nil 187: msg = message(msg) { "Expected #{mu_pp(o1)} to be #{op} #{mu_pp(o2)}" } 188: assert o1.__send__(op, o2), msg 189: end
Fails if stdout or stderr do not output the expected results. Pass in nil if you don’t care about that streams output. Pass in “” if you require it to be silent.
See also: #
# File lib/minitest/unit.rb, line 198 198: def assert_output stdout = nil, stderr = nil 199: out, err = capture_io do 200: yield 201: end 202: 203: x = assert_equal stdout, out, "In stdout" if stdout 204: y = assert_equal stderr, err, "In stderr" if stderr 205: 206: (!stdout || x) && (!stderr || y) 207: end
Fails unless the block raises one of exp
# File lib/minitest/unit.rb, line 212 212: def assert_raises *exp 213: msg = String === exp.last ? exp.pop : nil 214: msg = msg.to_s + "\n" if msg 215: should_raise = false 216: begin 217: yield 218: should_raise = true 219: rescue Exception => e 220: details = "#{msg}#{mu_pp(exp)} exception expected, not" 221: assert(exp.any? { |ex| 222: ex.instance_of?(Module) ? e.kind_of?(ex) : ex == e.class 223: }, exception_details(e, details)) 224: 225: return e 226: end 227: 228: exp = exp.first if exp.size == 1 229: flunk "#{msg}#{mu_pp(exp)} expected but nothing was raised." if 230: should_raise 231: end
Fails unless obj responds to meth.
# File lib/minitest/unit.rb, line 236 236: def assert_respond_to obj, meth, msg = nil 237: msg = message(msg) { 238: "Expected #{mu_pp(obj)} (#{obj.class}) to respond to ##{meth}" 239: } 240: assert obj.respond_to?(meth), msg 241: end
Fails unless exp and act are #
# File lib/minitest/unit.rb, line 246 246: def assert_same exp, act, msg = nil 247: msg = message(msg) { 248: data = [mu_pp(act), act.object_id, mu_pp(exp), exp.object_id] 249: "Expected %s (oid=%d) to be the same as %s (oid=%d)" % data 250: } 251: assert exp.equal?(act), msg 252: end
send_ary is a receiver, message and arguments.
Fails unless the call returns a true value
# File lib/minitest/unit.rb, line 259 259: def assert_send send_ary, m = nil 260: recv, msg, *args = send_ary 261: m = message(m) { 262: "Expected #{mu_pp(recv)}.#{msg}(*#{mu_pp(args)}) to return true" } 263: assert recv.__send__(msg, *args), m 264: end
Fails if the block outputs anything to stderr or stdout.
See also: #
# File lib/minitest/unit.rb, line 271 271: def assert_silent 272: assert_output "", "" do 273: yield 274: end 275: end
Fails unless the block throws sym
# File lib/minitest/unit.rb, line 280 280: def assert_throws sym, msg = nil 281: default = "Expected #{mu_pp(sym)} to have been thrown" 282: caught = true 283: catch(sym) do 284: begin 285: yield 286: rescue ArgumentError => e # 1.9 exception 287: default += ", not #{e.message.split(/ /).last}" 288: rescue NameError => e # 1.8 exception 289: default += ", not #{e.name.inspect}" 290: end 291: caught = false 292: end 293: 294: assert caught, message(msg) { default } 295: end
Captures $stdout and $stderr into strings:
out, err = capture_io do warn "You did a bad thing" end assert_match %r%bad%, err
# File lib/minitest/unit.rb, line 306 306: def capture_io 307: require 'stringio' 308: 309: orig_stdout, orig_stderr = $stdout, $stderr 310: captured_stdout, captured_stderr = StringIO.new, StringIO.new 311: $stdout, $stderr = captured_stdout, captured_stderr 312: 313: yield 314: 315: return captured_stdout.string, captured_stderr.string 316: ensure 317: $stdout = orig_stdout 318: $stderr = orig_stderr 319: end
Returns details for exception e
# File lib/minitest/unit.rb, line 324 324: def exception_details e, msg 325: "#{msg}\nClass: <#{e.class}>\nMessage: <#{e.message.inspect}>\n---Backtrace---\n#{MiniTest::filter_backtrace(e.backtrace).join("\n")}\n---------------" 326: end
Fails with msg
# File lib/minitest/unit.rb, line 331 331: def flunk msg = nil 332: msg ||= "Epic Fail!" 333: assert false, msg 334: end
Returns a proc that will output msg along with the default message.
# File lib/minitest/unit.rb, line 339 339: def message msg = nil, &default 340: proc { 341: if msg then 342: msg = msg.to_s unless String === msg 343: msg += '.' unless msg.empty? 344: msg += "\n#{default.call}." 345: msg.strip 346: else 347: "#{default.call}." 348: end 349: } 350: end
mu_pp gives a human-readable version of obj. By default # is called. You can override this to use # if you want.
# File lib/minitest/unit.rb, line 57 57: def mu_pp obj 58: s = obj.inspect 59: s = s.force_encoding(Encoding.default_external) if defined? Encoding 60: s 61: end
used for counting assertions
# File lib/minitest/unit.rb, line 355 355: def pass msg = nil 356: assert true 357: end
Fails if test is a true value
# File lib/minitest/unit.rb, line 362 362: def refute test, msg = nil 363: msg ||= "Failed refutation, no message given" 364: not assert(! test, msg) 365: end
Fails if obj is empty.
# File lib/minitest/unit.rb, line 370 370: def refute_empty obj, msg = nil 371: msg = message(msg) { "Expected #{obj.inspect} to not be empty" } 372: assert_respond_to obj, :empty? 373: refute obj.empty?, msg 374: end
Fails if exp == act.
For floats use refute_in_delta.
# File lib/minitest/unit.rb, line 381 381: def refute_equal exp, act, msg = nil 382: msg = message(msg) { 383: "Expected #{mu_pp(act)} to not be equal to #{mu_pp(exp)}" 384: } 385: refute exp == act, msg 386: end
For comparing Floats. Fails if exp is within delta of act
refute_in_delta Math::PI, (22.0 / 7.0)
# File lib/minitest/unit.rb, line 393 393: def refute_in_delta exp, act, delta = 0.001, msg = nil 394: n = (exp - act).abs 395: msg = message(msg) { 396: "Expected #{exp} - #{act} (#{n}) to not be < #{delta}" 397: } 398: refute delta > n, msg 399: end
For comparing Floats. Fails if exp and act have a relative error less than epsilon.
# File lib/minitest/unit.rb, line 405 405: def refute_in_epsilon a, b, epsilon = 0.001, msg = nil 406: refute_in_delta a, b, a * epsilon, msg 407: end
Fails if collection includes obj
# File lib/minitest/unit.rb, line 412 412: def refute_includes collection, obj, msg = nil 413: msg = message(msg) { 414: "Expected #{mu_pp(collection)} to not include #{mu_pp(obj)}" 415: } 416: assert_respond_to collection, :include? 417: refute collection.include?(obj), msg 418: end
Fails if obj is an instance of cls
# File lib/minitest/unit.rb, line 423 423: def refute_instance_of cls, obj, msg = nil 424: msg = message(msg) { 425: "Expected #{mu_pp(obj)} to not be an instance of #{cls}" 426: } 427: refute obj.instance_of?(cls), msg 428: end
Fails if obj is a kind of cls
# File lib/minitest/unit.rb, line 433 433: def refute_kind_of cls, obj, msg = nil # TODO: merge with instance_of 434: msg = message(msg) { "Expected #{mu_pp(obj)} to not be a kind of #{cls}" } 435: refute obj.kind_of?(cls), msg 436: end
Fails if exp =~ act
# File lib/minitest/unit.rb, line 441 441: def refute_match exp, act, msg = nil 442: msg = message(msg) { "Expected #{mu_pp(exp)} to not match #{mu_pp(act)}" } 443: assert_respond_to act, :"=~" 444: exp = (/#{Regexp.escape exp}/) if String === exp and String === act 445: refute exp =~ act, msg 446: end
Fails if obj is nil.
# File lib/minitest/unit.rb, line 451 451: def refute_nil obj, msg = nil 452: msg = message(msg) { "Expected #{mu_pp(obj)} to not be nil" } 453: refute obj.nil?, msg 454: end
Fails if o1 is not op o2 nil. eg:
refute_operator 1, :>, 2 #=> pass refute_operator 1, :<, 2 #=> fail
# File lib/minitest/unit.rb, line 462 462: def refute_operator o1, op, o2, msg = nil 463: msg = message(msg) { 464: "Expected #{mu_pp(o1)} to not be #{op} #{mu_pp(o2)}" 465: } 466: refute o1.__send__(op, o2), msg 467: end
Fails if obj responds to the message meth.
# File lib/minitest/unit.rb, line 472 472: def refute_respond_to obj, meth, msg = nil 473: msg = message(msg) { "Expected #{mu_pp(obj)} to not respond to #{meth}" } 474: 475: refute obj.respond_to?(meth), msg 476: end
Fails if exp is the same (by object identity) as act.
# File lib/minitest/unit.rb, line 481 481: def refute_same exp, act, msg = nil 482: msg = message(msg) { 483: data = [mu_pp(act), act.object_id, mu_pp(exp), exp.object_id] 484: "Expected %s (oid=%d) to not be the same as %s (oid=%d)" % data 485: } 486: refute exp.equal?(act), msg 487: end
Skips the current test. Gets listed at the end of the run but doesn’t cause a failure exit code.
# File lib/minitest/unit.rb, line 493 493: def skip msg = nil, bt = caller 494: msg ||= "Skipped, no message given" 495: raise MiniTest::Skip, msg, bt 496: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.