Class: Test::Unit::TestCase
- Inherits:
-
Object
- Object
- Test::Unit::TestCase
- 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
-
#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.
-
#assert_stderr(expected, actual, message = nil) ⇒ Object
Compare the stderr-output with your exception.
-
#assert_stderr_block(expected, message = nil) ⇒ Object
Compare the stderr-output with your exception.
-
#assert_stdout(expected, actual, message = nil) ⇒ Object
Compare the stdout-output with your exception.
-
#assert_stdout_block(expected, message = nil) ⇒ Object
Compare the stdout-output of a given block with your exception.
-
#catch_stderr(procedure) ⇒ Object
Catch stderr for a given procedure (Proc-Element).
-
#catch_stdout(procedure) ⇒ Object
Catch stdout for a given procedure (Proc-Element).
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:
-
text.to_doc(:html) is the test. It creates some HTML-Text
-
“expected/test_section.html” is read and compared to text.to_doc(:html)
-
If the file is missing -> error
-
If it is the same -> fine
-
If there are differences:
-
A message is given (like in assert_equal)
-
A folder “failure#Date.today” is created if not already exist
-
The file ‘test_section.html’ with the result is created in “failure#Date.today/”
-
I can use a compare tool to compare the expected result and the real result.
Recommendation to build up your test.
-
Create two folders: ‘expected’ and ‘failure’
-
Define your assertion with a non-existing filename
-
Run the test with folder_for_failure = ‘failure’
-
You will get a failure (filename is missing).
-
Copy the file in ‘failure’ to ‘expected’
-
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}", = nil ) # #Make the tests if File.exist?(filename) expected = File.read(filename) = (, "<?> expected (#{filename}) but was\n<?>.\n", expected, actual).to_s else = "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 } << "\n\t-> Build <#{folder_for_failure}/#{File.basename(filename)}>" end if File.exist?(filename) assert_block( ){ expected == actual } else assert_block( ){ 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, = nil ) if actual.respond_to?(:call) output = catch_stderr( actual ) = (, "<?> expected in stderr, but was\n<?>.\n", expected, output) assert_block( ){ expected == output } else raise 'no Proc-object given' = (, "<?> expected in stderr, but no Proc-object is given to test.\n", expected ) assert_block( ){ 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, = nil ) if block_given? $stderr = output = Catch_IO.new #temporary copy yield $stderr = STDERR = (, "<?> expected in stderr, but was\n<?>.\n", expected, output) assert_block( ){ expected == output } else raise 'no block given' = (, "<?> expected in stderr, but no block to test.\n", expected ) assert_block( ){ 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, = nil ) if actual.respond_to?(:call) output = catch_stdout( actual ) = (, "<?> expected in stdout, but was\n<?>.\n", expected, output) assert_block( ){ expected == output } else raise 'no Proc-object given' = (, "<?> expected in stdout, but no Proc-object is given to test.\n", expected ) assert_block( ){ 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, = nil ) if block_given? $stdout = output = Catch_IO.new #temporary copy yield $stdout = STDOUT = (, "<?> expected in stdout, but was\n<?>.\n", expected, output) assert_block( ){ expected == output } else raise 'no block given' = (, "<?> expected in stdout, but no block to test.\n", expected ) assert_block( ){ 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 |