12. Omit Examples class TC_MyTest < Test::Unit::TestCase def test_alpha omit("Buggy library. Skip") # never reach past here assert_true(1 == 1) end def test_beta omit_if(File::ALT_SEPARATOR, "Skipping test on MS Windows") # Won’t run this test on MS Windows (or VMS) assert_true(1 == 1) end def test_gamma omit_unless(File::ALT_SEPARATOR, "Only run on MS Windows") # Won’t run this test except on MS Windows assert_true(1 == 1) end end
13. Output 1) Omission: Buggy library. Skip test_alpha(TC_MyTest) omit_examples.rb:7:in `test_alpha' 2) Omission: Skipping test on MS Windows test_beta(TC_MyTest) omit_examples.rb:12:in `test_beta' 3 tests, 1 assertions, 0 failures, 0 errors, 0 pendings, 2 omissions, 0 notifications Only 1 assertion ran, the other two were skipped
14.
15. pend example class TC_MyTest < Test::Unit::TestCase def setup @obj = MyClass.new end def test_alpha pend("The 'alpha' function hasn't been implemented yet") assert_true(@obj.alpha) end end 1) Pending: The 'alpha' function hasn't been implemented yet test_alpha(TC_MyTest) pend_example.rb:13:in `test_alpha' 1 tests, 0 assertions, 0 failures, 0 errors, 1 pendings, 0 omissions, 0 notifications
16.
17. notify example class TC_MyTest < Test::Unit::TestCase def test_alpha notify("starting the alpha test") assert_true(1 == 1) notify("finished the alpha test") end end 1) Notification: starting the alpha test test_alpha(TC_MyTest) notify_example.rb:7:in `test_alpha' 2) Notification: finished the alpha test test_alpha(TC_MyTest) notify_example.rb:9:in `test_alpha' 1 tests, 1 assertions, 0 failures, 0 errors, 0 pendings, 0 omissions, 2 notifications
18.
19. startup & shutdown example class TC_MyTest < Test::Unit::TestCase def self.startup @@file_name = File.join(Dir.pwd, 'my_test_file.txt') @@file_handle = File.open(@@file_name, 'w') end def test_file_path assert_respond_to(@@file_handle, :path) end def self.shutdown @@file_handle.close File.delete(@@file_name) end end Don’t forget the ‘self’
20.
21. multiple setups class TC_MyTest < Test::Unit::TestCase def setup # first @standard = MyClass.new end setup # second def setup_alpha @alpha = MyClass.new end setup # third def setup_beta @beta = MyClass.new end def test_stuff assert_true(1 == 1) end end
22. multiple teardowns class TC_MyTest < Test::Unit::TestCase def test_stuff assert_true(1 == 1) end def teardown # last @standard = nil end teardown # second def teardown_alpha @alpha = nil end teardown # first def teardown_beta @beta = nil end end
23.
24. Attribute ‘before’ class TC_MyTest < Test::Unit::TestCase def setup # Third puts "In setup" end setup # Fourth def setup_alpha puts "In setup_alpha" end setup :before => :prepend # First def setup_beta puts "In setup_beta" end setup :before => :append # Second def setup_gamma puts "In setup_gamma" end end
25.
26. Better Diff Output 1) Failure: test_string_diff(TC_MyTest) [diff_example.rb:7]: <"weird"> expected but was <"wierd">. 1) Failure: test_string_diff(TC_MyTest) [diff_example.rb:7]: <"weird"> expected but was <"wierd">. diff: - weird ? - + wierd ? + Old: New:
27.
28. Sample Priority Output 1) Failure: test_one(TC_MyTest) [priority.rb:47]: <true> expected but was <false> 2) Failure: test_two(TC_MyTest) [priority.rb:51]: <true> expected but was <false> 7 tests, 7 assertions, 2 failures , 0 errors, 0 pendings, 0 omissions, 0 notifications 1) Failure: test_one(TC_MyTest) [priority.rb:47]: <true> expected but was <false> 2) Failure: test_two(TC_MyTest) [priority.rb:51]: <true> expected but was <false> 6 tests, 6 assertions, 2 failures , 0 errors, 0 pendings, 0 omissions, 0 notifications
29. Forcing a test priority :must def test_blah assert_true(1 == 1) end This test will always run, regardless of the “–priority-mode” switch.