16

Click here to load reader

Unit testing Ch. 13 of Programming Ruby

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Unit testing Ch. 13 of Programming Ruby

Unit Testing

Programming Ruby 1.9Chapter 13

Page 2: Unit testing Ch. 13 of Programming Ruby

What is it?

●testing at the lowest level●individual methods or lines

within methods

Page 3: Unit testing Ch. 13 of Programming Ruby

Why?

●less "archaeology"●decoupled designs

●peace of mind when refactoring

Page 4: Unit testing Ch. 13 of Programming Ruby

MiniTest::Unit vs Test::Unit

require '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_throws

require 'test/unit'● most of the features people used

gem install test-unit● little used features: test cases, GUI runners

Page 5: Unit testing Ch. 13 of Programming Ruby

Example Test

require_relative 'romanbug'require 'test/unit'

class TestRoman < Test::Unit::TestCase def test_simple assert_equal "i", Roman.new(1).to_s assert_equal "ix", Roman.new(9).to_s endend

Page 6: Unit testing Ch. 13 of Programming Ruby

Structuring Tests

● prefix methods with test_● setup/teardown run before/after each test

○ use passed? in teardown to see if the test passed

Page 7: Unit testing Ch. 13 of Programming Ruby

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/

Page 8: Unit testing Ch. 13 of Programming Ruby

Organizing tests

roman/ lib/ roman.rb other files....

test/ test_roman.rb other tests.... other files....

Page 9: Unit testing Ch. 13 of Programming Ruby

Running Tests (standard structure)

● ruby -I path/to/app/lib path/to/app/test/test_roman.rb

● ruby -I lib test/test_roman.rb

Page 10: Unit testing Ch. 13 of Programming Ruby

RSpec

● write story before writing tests● special .should semantics

Page 11: Unit testing Ch. 13 of Programming Ruby

RSpec Example

require_relative 'tennis_scorer'

describe 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

Page 12: Unit testing Ch. 13 of Programming Ruby

RSpec

before(:each), before(:all), after(:each), after(:all)

Page 13: Unit testing Ch. 13 of Programming Ruby

Shoulda

● less opinionated than RSpec● code lives inside Test::Unit::TestCases● makes test cases, so Test::Unit assertions work

Page 14: Unit testing Ch. 13 of Programming Ruby

Shoulda Example

require 'test/unit'require 'shoulda'require_relative 'tennis_scorer.rb'

class 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

Page 15: Unit testing Ch. 13 of Programming Ruby

Shoulda

● contexts may be nested● setup, should, should_eventually, teardown

Page 16: Unit testing Ch. 13 of Programming Ruby

End