Module Test::Unit::Assertions
In: lib/test/unit/assertions.rb

Test::Unit::Assertions contains the standard Test::Unit assertions. Assertions is included in Test::Unit::TestCase.

To include it in your own code and use its functionality, you simply need to rescue Test::Unit::AssertionFailedError. Additionally you may override add_assertion to get notified whenever an assertion is made.

Notes:

  • The message to each assertion, if given, will be propagated with the failure.
  • It is easy to add your own assertions based on assert_block().

Example Custom Assertion

  def deny(boolean, message = nil)
    message = build_message message, '<?> is not false or nil.', boolean
    assert_block message do
      not boolean
    end
  end

Methods

Constants

UncaughtThrow = { NameError => /^uncaught throw \`(.+)\'$/, #` ArgumentError => /^uncaught throw (.+)$/, ThreadError => /^uncaught throw \`(.+)\' in thread /

Public Class methods

Select whether or not to use the pretty-printer. If this option is set to false before any assertions are made, pp.rb will not be required.

Public Instance methods

Asserts that boolean is not false or nil.

Example:

  assert [1, 2].include?(5)

Passes if object#alias_name is an alias method of object#original_name.

Example:

  assert_alias_method([], :length, :size)  # -> pass
  assert_alias_method([], :size, :length)  # -> pass
  assert_alias_method([], :each, :size)    # -> fail

The assertion upon which all other assertions are based. Passes if the block yields true.

Example:

  assert_block "Couldn't do the thing" do
    do_the_thing
  end

Passes if actual is a boolean value.

Example:

  assert_boolean(true) # -> pass
  assert_boolean(nil)  # -> fail

Passes if expression "expected operator actual" is true.

Example:

  assert_compare(1, "<", 10)  # -> pass
  assert_compare(1, ">=", 10) # -> fail

Passes if object.const_defined?(constant_name)

Example:

  assert_const_defined(Test, :Unit)          # -> pass
  assert_const_defined(Object, :Nonexistent) # -> fail

Passes if object is empty.

Example:

  assert_empty("")                       # -> pass
  assert_empty([])                       # -> pass
  assert_empty({})                       # -> pass
  assert_empty(" ")                      # -> fail
  assert_empty([nil])                    # -> fail
  assert_empty({1 => 2})                 # -> fail

Passes if expected == +actual.

Note that the ordering of arguments is important, since a helpful error message is generated when this one fails that tells you the values of expected and actual.

Example:

  assert_equal 'MY STRING', 'my string'.upcase

Passes if assertion is failed in block.

Example:

  assert_fail_assertion {assert_equal("A", "B")}  # -> pass
  assert_fail_assertion {assert_equal("A", "A")}  # -> fail

Passes if actual is false.

Example:

  assert_false(false)  # -> pass
  assert_false(nil)    # -> fail

Passes if expected_float and actual_float are equal within delta tolerance.

Example:

  assert_in_delta 0.05, (50000.0 / 10**6), 0.00001

Passes if expected_float and actual_float are equal within epsilon relative error of expected_float.

Example:

  assert_in_epsilon(10000.0, 9900.0, 0.1) # -> pass
  assert_in_epsilon(10000.0, 9899.0, 0.1) # -> fail

Passes if collection includes object.

Example:

  assert_include([1, 10], 1)            # -> pass
  assert_include(1..10, 5)              # -> pass
  assert_include([1, 10], 5)            # -> fail
  assert_include(1..10, 20)             # -> fail

Passes if object.instance_of?(klass). When klass is an array of classes, it passes if any class satisfies +object.instance_of?(class).

Example:

  assert_instance_of(String, 'foo')            # -> pass
  assert_instance_of([Fixnum, NilClass], 100)  # -> pass
  assert_instance_of([Numeric, NilClass], 100) # -> fail

Passes if object.kind_of?(klass). When klass is an array of classes or modules, it passes if any class or module satisfies +object.kind_of?(class_or_module).

Example:

  assert_kind_of(Object, 'foo')                # -> pass
  assert_kind_of([Fixnum, NilClass], 100)      # -> pass
  assert_kind_of([Fixnum, NilClass], "string") # -> fail

Passes if string =~ pattern.

Example:

  assert_match(/\d+/, 'five, 6, seven')

Passes if object is nil.

Example:

  assert_nil [1, 2].uniq!

Deprecated. Use assert_not_match instead.

Passes if regexp !~ string

Example:

  assert_no_match(/two/, 'one 2 three')   # -> pass
  assert_no_match(/three/, 'one 2 three') # -> fail

Passes if !object.const_defined?(constant_name)

Example:

  assert_not_const_defined(Object, :Nonexistent) # -> pass
  assert_not_const_defined(Test, :Unit)          # -> fail

Passes if object is not empty.

Example:

  assert_not_empty(" ")                      # -> pass
  assert_not_empty([nil])                    # -> pass
  assert_not_empty({1 => 2})                 # -> pass
  assert_not_empty("")                       # -> fail
  assert_not_empty([])                       # -> fail
  assert_not_empty({})                       # -> fail

Passes if expected != actual

Example:

  assert_not_equal 'some string', 5

Passes if expected_float and actual_float are not equal within delta tolerance.

Example:

  assert_not_in_delta(0.05, (50000.0 / 10**6), 0.00002) # -> pass
  assert_not_in_delta(0.05, (50000.0 / 10**6), 0.00001) # -> fail

Passes if expected_float and actual_float are not equal within epsilon relative error of expected_float.

Example:

  assert_not_in_epsilon(10000.0, 9900.0, 0.1) # -> fail
  assert_not_in_epsilon(10000.0, 9899.0, 0.1) # -> pass

Passes if collection doesn‘t include object.

Example:

  assert_not_include([1, 10], 5)            # -> pass
  assert_not_include(1..10, 20)             # -> pass
  assert_not_include([1, 10], 1)            # -> fail
  assert_not_include(1..10, 5)              # -> fail

Passes if regexp !~ string

Example:

  assert_not_match(/two/, 'one 2 three')   # -> pass
  assert_not_match(/three/, 'one 2 three') # -> fail

Passes if ! object .nil?

Example:

  assert_not_nil '1 two 3'.sub!(/two/, '2')

Passes if object.predicate is not true.

Example:

  assert_not_predicate([1], :empty?) # -> pass
  assert_not_predicate([], :empty?)  # -> fail

Passes if object does not .respond_to? method.

Example:

  assert_not_respond_to('bugbear', :nonexistence) # -> pass
  assert_not_respond_to('bugbear', :size)         # -> fail

Passes if ! actual .equal? expected

Example:

  assert_not_same Object.new, Object.new

Passes if the method send doesn‘t return a true value.

send_array is composed of:

  • A receiver
  • A method
  • Arguments to the method

Example:

  assert_not_send([[1, 2], :member?, 1]) # -> fail
  assert_not_send([[1, 2], :member?, 4]) # -> pass

Passes if block does not raise an exception.

Example:

  assert_nothing_raised do
    [1, 2].uniq
  end

Passes if block does not throw anything.

Example:

 assert_nothing_thrown do
   [1, 2].uniq
 end

Compares the +object1+ with +object2+ using operator.

Passes if object1.send(operator, object2) is true.

Example:

  assert_operator 5, :>=, 4

Passes if path exists.

Example:

  assert_path_exist("/tmp")          # -> pass
  assert_path_exist("/bin/sh")       # -> pass
  assert_path_exist("/nonexistent")  # -> fail

Passes if path doesn‘t exist.

Example:

  assert_path_not_exist("/nonexistent")  # -> pass
  assert_path_not_exist("/tmp")          # -> fail
  assert_path_not_exist("/bin/sh")       # -> fail

Passes if object.predicate is true.

Example:

  assert_predicate([], :empty?)  # -> pass
  assert_predicate([1], :empty?) # -> fail

Passes if the block raises one of the expected exceptions. When an expected exception is an Exception object, passes if expected_exception == actual_exception.

Example:

  assert_raise(RuntimeError, LoadError) do
    raise 'Boom!!!'
  end # -> pass

  assert_raise do
    raise Exception, 'Any exception should be raised!!!'
  end # -> pass

  assert_raise(RuntimeError.new("XXX")) {raise "XXX"} # -> pass
  assert_raise(MyError.new("XXX"))      {raise "XXX"} # -> fail
  assert_raise(RuntimeError.new("ZZZ")) {raise "XXX"} # -> fail

Passes if the block raises one of the given exceptions or sub exceptions of the given exceptions.

Example:

  assert_raise_kind_of(SystemCallError) do
    raise Errno::EACCES
  end

Passes if an exception is raised in block and its message is expected.

Example:

  assert_raise_message("exception") {raise "exception"}  # -> pass
  assert_raise_message(/exc/i) {raise "exception"}       # -> pass
  assert_raise_message("exception") {raise "EXCEPTION"}  # -> fail
  assert_raise_message("exception") {}                   # -> fail

Alias of assert_raise.

Will be deprecated in 1.9, and removed in 2.0.

Passes if object .respond_to? method

Example:

  assert_respond_to 'bugbear', :slice

Passes if actual .equal? expected (i.e. they are the same instance).

Example:

  o = Object.new
  assert_same o, o

Passes if the method send returns a true value.

send_array is composed of:

  • A receiver
  • A method
  • Arguments to the method

Example:

  assert_send([[1, 2], :member?, 1]) # -> pass
  assert_send([[1, 2], :member?, 4]) # -> fail

Passes if the block throws expected_object

Example:

  assert_throw(:done) do
    throw(:done)
  end

Alias of assert_throw.

Will be deprecated in 1.9, and removed in 2.0.

Passes if actual is true.

Example:

  assert_true(true)  # -> pass
  assert_true(:true) # -> fail

Builds a failure message. head is added before the template and arguments replaces the ’?’s positionally in the template.

Flunk always fails.

Example:

  flunk 'Not done testing yet.'

[Validate]