Class: Test::Unit::TestCase

Inherits:
Object
  • Object
show all
Defined in:
lib/more_unit_test/assert_equal_filecontent.rb,
lib/more_unit_test/assert_stdout.rb

Overview

Extend the class TestCase with additional methods/assertions

Instance Method Summary collapse

Instance Method Details

#assert_equal_filecontent(filename, actual, folder_for_failure = "failure#{Date.today}", message = nil) ⇒ Object

Takes the content of the file ‘filename’ and compares it with ‘actual’ like in assert_equal. If ‘filname’ doesn’t exist, the failure Reference file <#filename> missing is returned.

‘folder_for_failure’ will contain all results with differences.

Example of the usage:

assert_equal_filecontent( "expected/test_section.html", text.to_doc(:html))

What will happen:

  1. text.to_doc(:html) is the test. It creates some HTML-Text

  2. “expected/test_section.html” is read and compared to text.to_doc(:html)

  3. If the file is missing -> error

  4. If it is the same -> fine

  5. If there are differences:

  6. A message is given (like in assert_equal)

  7. A folder “failure#Date.today” is created if not already exist

  8. The file ‘test_section.html’ with the result is created in “failure#Date.today/”

  9. I can use a compare tool to compare the expected result and the real result.

Recommendation to build up your test.

  1. Create two folders: ‘expected’ and ‘failure’

  2. Define your assertion with a non-existing filename

  3. Run the test with folder_for_failure = ‘failure’

  4. You will get a failure (filename is missing).

  5. Copy the file in ‘failure’ to ‘expected’

  6. Rerun again, you have no failure (hopefully ;-) )



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/more_unit_test/assert_equal_filecontent.rb', line 51

def assert_equal_filecontent( filename, actual, folder_for_failure = "failure#{Date.today}", message = nil )
  #
  #Make the tests
  if File.exist?(filename)
    expected = File.read(filename) 
    full_message = build_message(message, "<?> expected (#{filename}) but was\n<?>.\n", expected, actual).to_s
  else
    full_message = "Reference file <#{filename}> missing"
  end

  #Write the real result to a file if a failure folder is given.
  if folder_for_failure and expected  != actual
    File.makedirs(folder_for_failure) if ! File.directory?(folder_for_failure)
    File.open( "#{folder_for_failure}/#{File.basename(filename)}", 'w'){|f|
      f << actual
    }
    full_message << "\n\t-> Build <#{folder_for_failure}/#{File.basename(filename)}>"
  end

  if File.exist?(filename)
    assert_block( full_message ){ expected  == actual }
  else
    assert_block( full_message ){ false }
  end
end

#assert_stderr(expected, actual, message = nil) ⇒ Object

Compare the stderr-output with your exception.

Actual must be a procedure. Another object (like a method call) would be executed first, before stderr could be redirected.

Example:

require 'assert_stdout'
def puts_err( arg )
  $stderr << arg
end

class MyTest < Test::Unit::TestCase  
  def test_hello_world()
    assert_stderr("Hello World", Proc.new{ puts_err 'Hello World'} )
  end
end


118
119
120
121
122
123
124
125
126
127
128
# File 'lib/more_unit_test/assert_stdout.rb', line 118

def assert_stderr( expected, actual, message = nil )
  if actual.respond_to?(:call)
    output = catch_stderr( actual )
    full_message = build_message(message, "<?> expected in stderr, but was\n<?>.\n", expected, output)
    assert_block( full_message ){ expected  == output }
  else
    raise 'no Proc-object given'
    full_message = build_message(message, "<?> expected in stderr, but no Proc-object is given to test.\n", expected )
    assert_block( full_message ){ false } #force error
  end
end

#assert_stderr_block(expected, message = nil) ⇒ Object

Compare the stderr-output with your exception.

Example:

require 'assert_stdout'
def puts_err( arg )
  $stderr << arg
end

class MyTest < Test::Unit::TestCase  
  def test_hello_world()
    assert_stderr_block("Hello World"){ puts_err 'Hello World'}
  end
end


144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/more_unit_test/assert_stdout.rb', line 144

def assert_stderr_block( expected, message = nil )
  if block_given? 
    $stderr = output = Catch_IO.new #temporary copy
    yield
    $stderr = STDERR
    full_message = build_message(message, "<?> expected in stderr, but was\n<?>.\n", expected, output)
    assert_block( full_message ){ expected  == output }
  else
    raise 'no block given'
    full_message = build_message(message, "<?> expected in stderr, but no block to test.\n", expected )
    assert_block( full_message ){ false }
  end
end

#assert_stdout(expected, actual, message = nil) ⇒ Object

Compare the stdout-output with your exception.

Actual must be a procedure.
Another object (like a method call) would be executed first, before stdout could be redirected.

Example:
    require 'assert_stdout'
    class MyTest < Test::Unit::TestCase  
      def test_hello_world()
        assert_stdout("Hello World\n", Proc.new{ puts 'Hello World'} )
      end
    end


56
57
58
59
60
61
62
63
64
65
66
# File 'lib/more_unit_test/assert_stdout.rb', line 56

def assert_stdout( expected, actual, message = nil )
  if actual.respond_to?(:call)
    output = catch_stdout( actual )
    full_message = build_message(message, "<?> expected in stdout, but was\n<?>.\n", expected, output)
    assert_block( full_message ){ expected  == output }
  else
    raise 'no Proc-object given'
    full_message = build_message(message, "<?> expected in stdout, but no Proc-object is given to test.\n", expected )
    assert_block( full_message ){ false } #force error
  end
end

#assert_stdout_block(expected, message = nil) ⇒ Object

Compare the stdout-output of a given block with your exception.

Example:

require 'assert_stdout'
class MyTest < Test::Unit::TestCase  
  def test_hello_world()
    assert_stdout_block("Hello World\n"){ puts 'Hello World'}
  end
end


78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/more_unit_test/assert_stdout.rb', line 78

def assert_stdout_block( expected, message = nil )
  if block_given? 
    $stdout = output = Catch_IO.new #temporary copy
    yield
    $stdout = STDOUT
    full_message = build_message(message, "<?> expected in stdout, but was\n<?>.\n", expected, output)
    assert_block( full_message ){ expected  == output }
  else
    raise 'no block given'
    full_message = build_message(message, "<?> expected in stdout, but no block to test.\n", expected )
    assert_block( full_message ){ false }
  end
end

#catch_stderr(procedure) ⇒ Object

Catch stderr for a given procedure (Proc-Element).



94
95
96
97
98
99
# File 'lib/more_unit_test/assert_stdout.rb', line 94

def catch_stderr( procedure )
  $stderr = output = Catch_IO.new #temporary copy
  procedure.call
  $stderr = STDERR
  output
end

#catch_stdout(procedure) ⇒ Object

Catch stdout for a given procedure (Proc-Element).



36
37
38
39
40
41
# File 'lib/more_unit_test/assert_stdout.rb', line 36

def catch_stdout( procedure )
  $stdout = output = Catch_IO.new #temporary copy
  procedure.call
  $stdout = STDOUT
  output
end