New Features of Test::Unit 2.0 Daniel Berger Ruby, C, Perl Win32Utils SysUtils Shards Sapphire
Test::Unit now a gem <ul><li>Maintained by Kouhei Sutou </li></ul><ul><li>gem install test-unit </li></ul>
I Do Not Know <ul><li>RSpec </li></ul><ul><li>Shoulda </li></ul><ul><li>If your favorite testing framework already support...
How to use Test::Unit 2.x <ul><li>require ‘rubygems’ </li></ul><ul><li>gem ‘test-unit’ </li></ul><ul><li>require ‘test/uni...
New Assertions <ul><li>assert_true </li></ul><ul><li>assert_false </li></ul><ul><li>assert_boolean </li></ul>
assert_true assert_true(condition) Same as: assert_equal(true, condition) assert(condition)
assert_false assert_false(condition) Same as: assert_equal(false, condition)
assert_boolean assert_boolean(result) Same as: assert_equal(true, [true, false].include?(result))
Wow Ooh, aah.
New stuff to use within tests <ul><li>omit() </li></ul><ul><li>omit_if() </li></ul><ul><li>omit_unless() </li></ul><ul><li...
omit, omit_if, omit_unless Used to skip tests entirely Or in specific circumstances
Omit Examples class TC_MyTest < Test::Unit::TestCase def test_alpha omit(&quot;Buggy library. Skip&quot;) # never reach pa...
Output 1) Omission: Buggy library. Skip test_alpha(TC_MyTest) omit_examples.rb:7:in `test_alpha' 2) Omission: Skipping tes...
pend <ul><li>The Test::Unit version of a TODO item </li></ul><ul><li>But better, because a TODO in code is easily forgotte...
pend example class TC_MyTest < Test::Unit::TestCase def setup @obj = MyClass.new end def test_alpha pend(&quot;The 'alpha'...
notify <ul><li>A notification within your test </li></ul><ul><li>Unlike typical stdout, tracked by Test::Unit as its own e...
notify example class TC_MyTest < Test::Unit::TestCase def test_alpha notify(&quot;starting the alpha test&quot;) assert_tr...
startup and shutdown <ul><li>Analogous to startup and teardown, except that they only run once per test case </li></ul><ul...
startup & shutdown example class TC_MyTest < Test::Unit::TestCase def self.startup @@file_name = File.join(Dir.pwd, 'my_te...
multiple setup & teardown <ul><li>You can have multiple setup and teardown methods with their own name </li></ul><ul><li>E...
multiple setups class TC_MyTest < Test::Unit::TestCase def setup  # first @standard = MyClass.new end setup # second def s...
multiple teardowns class TC_MyTest < Test::Unit::TestCase def test_stuff assert_true(1 == 1) end def teardown  # last @sta...
Setup Attributes <ul><li>setup :before => :prepend </li></ul><ul><li>Runs before default setup </li></ul><ul><li>setup :be...
Attribute ‘before’ class TC_MyTest < Test::Unit::TestCase def setup #  Third puts &quot;In setup&quot; end setup #  Fourth...
Quick Rules Summary <ul><li>setup :before => :prepend </li></ul><ul><li>setup :before => :append </li></ul><ul><li>setup (...
Better Diff Output 1) Failure: test_string_diff(TC_MyTest) [diff_example.rb:7]: <&quot;weird&quot;> expected but was <&quo...
Priority Mode <ul><li>Adds a –priority-mode command line option </li></ul><ul><li>If used, failed tests always repeat </li...
Sample Priority Output 1) Failure: test_one(TC_MyTest) [priority.rb:47]: <true> expected but was <false> 2) Failure: test_...
Forcing a test priority :must def test_blah assert_true(1 == 1) end This test will always run, regardless of the “–priorit...
Priority Modes <ul><li>must (i.e. always) </li></ul><ul><li>important </li></ul><ul><li>high </li></ul><ul><li>normal </li...
Colorized Output <ul><li>On by default (bad) </li></ul><ul><li>Disable with –no-use-color </li></ul><ul><li>Doesn’t work o...
Any questions?
Upcoming SlideShare
Loading in...5
×

New Features Of Test Unit 2.x

10,358

Published on

Review of the new features of Test::Unit 2.x for Ruby

Published in: Technology, Business
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
10,358
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
39
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

New Features Of Test Unit 2.x

  1. 1. New Features of Test::Unit 2.0 Daniel Berger Ruby, C, Perl Win32Utils SysUtils Shards Sapphire
  2. 2. Test::Unit now a gem <ul><li>Maintained by Kouhei Sutou </li></ul><ul><li>gem install test-unit </li></ul>
  3. 3. I Do Not Know <ul><li>RSpec </li></ul><ul><li>Shoulda </li></ul><ul><li>If your favorite testing framework already supports the features I discuss, great </li></ul><ul><li>If not, perhaps you’ll want to borrow them </li></ul>
  4. 4. How to use Test::Unit 2.x <ul><li>require ‘rubygems’ </li></ul><ul><li>gem ‘test-unit’ </li></ul><ul><li>require ‘test/unit’ </li></ul>
  5. 5. New Assertions <ul><li>assert_true </li></ul><ul><li>assert_false </li></ul><ul><li>assert_boolean </li></ul>
  6. 6. assert_true assert_true(condition) Same as: assert_equal(true, condition) assert(condition)
  7. 7. assert_false assert_false(condition) Same as: assert_equal(false, condition)
  8. 8. assert_boolean assert_boolean(result) Same as: assert_equal(true, [true, false].include?(result))
  9. 9. Wow Ooh, aah.
  10. 10. New stuff to use within tests <ul><li>omit() </li></ul><ul><li>omit_if() </li></ul><ul><li>omit_unless() </li></ul><ul><li>pend() </li></ul><ul><li>notify() </li></ul>
  11. 11. omit, omit_if, omit_unless Used to skip tests entirely Or in specific circumstances
  12. 12. Omit Examples class TC_MyTest < Test::Unit::TestCase def test_alpha omit(&quot;Buggy library. Skip&quot;) # never reach past here assert_true(1 == 1) end def test_beta omit_if(File::ALT_SEPARATOR, &quot;Skipping test on MS Windows&quot;) # Won’t run this test on MS Windows (or VMS) assert_true(1 == 1) end def test_gamma omit_unless(File::ALT_SEPARATOR, &quot;Only run on MS Windows&quot;) # Won’t run this test except on MS Windows assert_true(1 == 1) end end
  13. 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. 14. pend <ul><li>The Test::Unit version of a TODO item </li></ul><ul><li>But better, because a TODO in code is easily forgotten, but a pending test will annoy you into completing it. </li></ul>
  15. 15. pend example class TC_MyTest < Test::Unit::TestCase def setup @obj = MyClass.new end def test_alpha pend(&quot;The 'alpha' function hasn't been implemented yet&quot;) 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. 16. notify <ul><li>A notification within your test </li></ul><ul><li>Unlike typical stdout, tracked by Test::Unit as its own entity </li></ul><ul><li>Possibly useful as a test debugger (?) </li></ul>
  17. 17. notify example class TC_MyTest < Test::Unit::TestCase def test_alpha notify(&quot;starting the alpha test&quot;) assert_true(1 == 1) notify(&quot;finished the alpha test&quot;) 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. 18. startup and shutdown <ul><li>Analogous to startup and teardown, except that they only run once per test case </li></ul><ul><li>As opposed to once per test </li></ul><ul><li>Oh, and they’re singleton methods </li></ul><ul><li>Use for things you only need to do once, e.g. file generation, database connections, etc </li></ul>
  19. 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. 20. multiple setup & teardown <ul><li>You can have multiple setup and teardown methods with their own name </li></ul><ul><li>Each setup runs in the order declared, before any tests are run </li></ul><ul><li>Each teardown runs in reverse order declaration after all tests have run </li></ul><ul><li>Useful for breaking out big chunks of setup and teardown code into their own, manageable pieces </li></ul>
  21. 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. 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. 23. Setup Attributes <ul><li>setup :before => :prepend </li></ul><ul><li>Runs before default setup </li></ul><ul><li>setup :before => :append </li></ul><ul><li>Runs before default setup, but after before/prepend </li></ul><ul><li>setup :after => :prepend </li></ul><ul><li>Runs after default setup, but before after/append and custom setup methods </li></ul><ul><li>setup :after => :append </li></ul><ul><li>Runs after default setup and after custom setup methods </li></ul>
  24. 24. Attribute ‘before’ class TC_MyTest < Test::Unit::TestCase def setup # Third puts &quot;In setup&quot; end setup # Fourth def setup_alpha puts &quot;In setup_alpha&quot; end setup :before => :prepend # First def setup_beta puts &quot;In setup_beta&quot; end setup :before => :append # Second def setup_gamma puts &quot;In setup_gamma&quot; end end
  25. 25. Quick Rules Summary <ul><li>setup :before => :prepend </li></ul><ul><li>setup :before => :append </li></ul><ul><li>setup (default) </li></ul><ul><li>setup :after => :prepend </li></ul><ul><li>setup (custom) </li></ul><ul><li>setup :after => :append </li></ul><ul><li>Get it? Got it? Good. </li></ul>
  26. 26. Better Diff Output 1) Failure: test_string_diff(TC_MyTest) [diff_example.rb:7]: <&quot;weird&quot;> expected but was <&quot;wierd&quot;>. 1) Failure: test_string_diff(TC_MyTest) [diff_example.rb:7]: <&quot;weird&quot;> expected but was <&quot;wierd&quot;>. diff: - weird ? - + wierd ? + Old: New:
  27. 27. Priority Mode <ul><li>Adds a –priority-mode command line option </li></ul><ul><li>If used, failed tests always repeat </li></ul><ul><li>Tests that passed previously have a 50% chance of running </li></ul><ul><li>Designed to reduce your test time </li></ul><ul><li>Can be controlled with priority pragma </li></ul>
  28. 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. 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.
  30. 30. Priority Modes <ul><li>must (i.e. always) </li></ul><ul><li>important </li></ul><ul><li>high </li></ul><ul><li>normal </li></ul><ul><li>low </li></ul><ul><li>Never </li></ul><ul><li>Not sure what the probabilities are, and it doesn’t matter. </li></ul>
  31. 31. Colorized Output <ul><li>On by default (bad) </li></ul><ul><li>Disable with –no-use-color </li></ul><ul><li>Doesn’t work on Windows (yet) </li></ul>
  32. 32. Any questions?
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×