Unit TestingProgramming Ruby 1.9     Chapter 13
What is it?●testing at the lowest level●individual methods or lines within methods
Why?●less "archaeology"●decoupled designs●peace of mind when refactoring
MiniTest::Unit vs Test::Unitrequire minitest/unit  ● MiniTest new in 1.9 "a little leaner"  ● has a compatibility layer fo...
Example Testrequire_relative romanbugrequire test/unitclass TestRoman < Test::Unit::TestCase   def test_simple     assert_...
Structuring Tests● prefix methods with test_● setup/teardown run before/after each test   ○ use passed? in teardown to see...
Running Tests (files in the same folder)● ruby test_roman.rb   ○ Kernel::at_exit● ruby test_roman.rb -n test_simple● ruby ...
Organizing testsroman/  lib/     roman.rb     other files....  test/     test_roman.rb     other tests....  other files....
Running Tests (standard structure)● ruby -I path/to/app/lib  path/to/app/test/test_roman.rb● ruby -I lib test/test_roman.rb
RSpec● write story before writing tests● special .should semantics
RSpec Examplerequire_relative tennis_scorerdescribe TennisScorer do  it "should start with a score of 0-0" do     ts = Ten...
RSpecbefore(:each), before(:all), after(:each), after(:all)
Shoulda● less opinionated than RSpec● code lives inside Test::Unit::TestCases● makes test cases, so Test::Unit assertions ...
Shoulda Examplerequire test/unitrequire shouldarequire_relative tennis_scorer.rbclass TennisScorerTest < Test::Unit::TestC...
Shoulda● contexts may be nested● setup, should, should_eventually, teardown
End
Upcoming SlideShare
Loading in …5
×

Unit testing Ch. 13 of Programming Ruby

917 views

Published on

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

  • Be the first to like this

No Downloads
Views
Total views
917
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Unit testing Ch. 13 of Programming Ruby

  1. 1. Unit TestingProgramming Ruby 1.9 Chapter 13
  2. 2. What is it?●testing at the lowest level●individual methods or lines within methods
  3. 3. Why?●less "archaeology"●decoupled designs●peace of mind when refactoring
  4. 4. MiniTest::Unit vs Test::Unitrequire minitest/unit ● MiniTest new in 1.9 "a little leaner" ● has a compatibility layer for Test::Unit ● negative assertions renamed to refute ○ assert_not_nil -> refute_nil ● missing assertions ○ assert_not_raises ○ assert_not_throwsrequire test/unit ● most of the features people usedgem install test-unit ● little used features: test cases, GUI runners
  5. 5. Example Testrequire_relative romanbugrequire test/unitclass TestRoman < Test::Unit::TestCase def test_simple assert_equal "i", Roman.new(1).to_s assert_equal "ix", Roman.new(9).to_s endend
  6. 6. Structuring Tests● prefix methods with test_● setup/teardown run before/after each test ○ use passed? in teardown to see if the test passed
  7. 7. Running Tests (files in the same folder)● ruby test_roman.rb ○ Kernel::at_exit● ruby test_roman.rb -n test_simple● ruby test_roman.rb -n /simple/
  8. 8. Organizing testsroman/ lib/ roman.rb other files.... test/ test_roman.rb other tests.... other files....
  9. 9. Running Tests (standard structure)● ruby -I path/to/app/lib path/to/app/test/test_roman.rb● ruby -I lib test/test_roman.rb
  10. 10. RSpec● write story before writing tests● special .should semantics
  11. 11. RSpec Examplerequire_relative tennis_scorerdescribe TennisScorer do it "should start with a score of 0-0" do ts = TennisScorer.new ts.score.should == "0-0" end it "should be 15-0 if the server wins a point"end
  12. 12. RSpecbefore(:each), before(:all), after(:each), after(:all)
  13. 13. Shoulda● less opinionated than RSpec● code lives inside Test::Unit::TestCases● makes test cases, so Test::Unit assertions work
  14. 14. Shoulda Examplerequire test/unitrequire shouldarequire_relative tennis_scorer.rbclass TennisScorerTest < Test::Unit::TestCase context "Tennis scores" do setup do @ts = TennisScorer.new end should "start with a score of 0-0" do assert_equal("0-0", @ts.score) end endend
  15. 15. Shoulda● contexts may be nested● setup, should, should_eventually, teardown
  16. 16. End

×