SlideShare a Scribd company logo
Unit Testing

Programming 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::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
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
   end
end
Structuring Tests

● prefix methods with test_
● setup/teardown run before/after each test
   ○ use passed? in teardown to see if the test
     passed
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/
Organizing tests

roman/
  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 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
RSpec

before(: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 work
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
   end
end
Shoulda

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

More Related Content

What's hot (20)

PDF
Mocha, chai and sinon
Andrew Dixon
 
PDF
Unit Testing your React / Redux app (@BucharestJS)
Alin Pandichi
 
PPT
Zend Framework 2 - PHPUnit
Tarun Kumar Singhal
 
PPT
JavaScript Unit Testing
Christian Johansen
 
PPT
JavaScript Testing: Mocha + Chai
James Cryer
 
PDF
Minitest
Creditas
 
PDF
WebAssembly: A New World of Native Exploits on the Browser
Priyanka Aash
 
PDF
Testing with Express, Mocha & Chai
Joerg Henning
 
PPT
Testing – With Mock Objects
emmettwalsh
 
PPTX
Angular Unit Testing
Avi Engelshtein
 
PPTX
Automation testing
kamilkaide
 
PPT
Mocha Testing
Erick Aky
 
PDF
How to go about testing in React?
Lisa Gagarina
 
PPTX
Testing in Scala. Adform Research
Vasil Remeniuk
 
PDF
Drupal and testing (2010 - 2011 / 2)
Peter Arato
 
PPTX
In search of JavaScript code quality: unit testing
Anna Khabibullina
 
PDF
Modern Java Development
Peerapat Asoktummarungsri
 
PPT
Testing In Django
Daniel Greenfeld
 
PPT
How to test models using php unit testing framework?
satejsahu
 
PPTX
Java do-while Loop
Rhythm Suiwal
 
Mocha, chai and sinon
Andrew Dixon
 
Unit Testing your React / Redux app (@BucharestJS)
Alin Pandichi
 
Zend Framework 2 - PHPUnit
Tarun Kumar Singhal
 
JavaScript Unit Testing
Christian Johansen
 
JavaScript Testing: Mocha + Chai
James Cryer
 
Minitest
Creditas
 
WebAssembly: A New World of Native Exploits on the Browser
Priyanka Aash
 
Testing with Express, Mocha & Chai
Joerg Henning
 
Testing – With Mock Objects
emmettwalsh
 
Angular Unit Testing
Avi Engelshtein
 
Automation testing
kamilkaide
 
Mocha Testing
Erick Aky
 
How to go about testing in React?
Lisa Gagarina
 
Testing in Scala. Adform Research
Vasil Remeniuk
 
Drupal and testing (2010 - 2011 / 2)
Peter Arato
 
In search of JavaScript code quality: unit testing
Anna Khabibullina
 
Modern Java Development
Peerapat Asoktummarungsri
 
Testing In Django
Daniel Greenfeld
 
How to test models using php unit testing framework?
satejsahu
 
Java do-while Loop
Rhythm Suiwal
 

Viewers also liked (7)

PPTX
Intro to HTML 5
lvrubygroup
 
PPTX
Shoes
lvrubygroup
 
PPTX
Intro to Rails
lvrubygroup
 
PPTX
Intro to CoffeeScript
lvrubygroup
 
PPTX
Sinatra + Heroku
lvrubygroup
 
PDF
We present Bugscout
Jorge Martínez Taboada
 
PDF
Study: The Future of VR, AR and Self-Driving Cars
LinkedIn
 
Intro to HTML 5
lvrubygroup
 
Intro to Rails
lvrubygroup
 
Intro to CoffeeScript
lvrubygroup
 
Sinatra + Heroku
lvrubygroup
 
We present Bugscout
Jorge Martínez Taboada
 
Study: The Future of VR, AR and Self-Driving Cars
LinkedIn
 
Ad

Similar to Unit testing Ch. 13 of Programming Ruby (20)

PDF
RubyTesting
tutorialsruby
 
PDF
RubyTesting
tutorialsruby
 
ZIP
Rspec Tips
lionpeal
 
PPTX
presentation des google test dans un contexte de tdd
Thierry Gayet
 
PDF
How to Begin Developing Ruby Core
Hiroshi SHIBATA
 
PDF
Practical Testing of Ruby Core
Hiroshi SHIBATA
 
PDF
How to Begin to Develop Ruby Core
Hiroshi SHIBATA
 
PPT
Google test training
Thierry Gayet
 
PDF
A Big Look at MiniTest
Mark
 
ODP
Rtt preso
fdschoeneman
 
PDF
Beyond Testing: Specs and Behavior Driven Development
Rabble .
 
KEY
Intro to Ruby (and RSpec)
Blazing Cloud
 
PDF
Ruby on rails rspec
Bindesh Vijayan
 
PDF
IntroTestMore
tutorialsruby
 
PDF
IntroTestMore
tutorialsruby
 
PDF
Automated Testing with Ruby
Keith Pitty
 
PPT
New Features Of Test Unit 2.x
djberg96
 
PDF
A Big Look at MiniTest
Mark
 
PPT
Unit testing php-unit - phing - selenium_v2
Tricode (part of Dept)
 
RubyTesting
tutorialsruby
 
RubyTesting
tutorialsruby
 
Rspec Tips
lionpeal
 
presentation des google test dans un contexte de tdd
Thierry Gayet
 
How to Begin Developing Ruby Core
Hiroshi SHIBATA
 
Practical Testing of Ruby Core
Hiroshi SHIBATA
 
How to Begin to Develop Ruby Core
Hiroshi SHIBATA
 
Google test training
Thierry Gayet
 
A Big Look at MiniTest
Mark
 
Rtt preso
fdschoeneman
 
Beyond Testing: Specs and Behavior Driven Development
Rabble .
 
Intro to Ruby (and RSpec)
Blazing Cloud
 
Ruby on rails rspec
Bindesh Vijayan
 
IntroTestMore
tutorialsruby
 
IntroTestMore
tutorialsruby
 
Automated Testing with Ruby
Keith Pitty
 
New Features Of Test Unit 2.x
djberg96
 
A Big Look at MiniTest
Mark
 
Unit testing php-unit - phing - selenium_v2
Tricode (part of Dept)
 
Ad

Recently uploaded (20)

PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PDF
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
PPT
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
PDF
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
PDF
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
PDF
Predicting the unpredictable: re-engineering recommendation algorithms for fr...
Speck&Tech
 
PDF
Timothy Rottach - Ramp up on AI Use Cases, from Vector Search to AI Agents wi...
AWS Chicago
 
PDF
Windsurf Meetup Ottawa 2025-07-12 - Planning Mode at Reliza.pdf
Pavel Shukhman
 
PDF
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
PDF
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
PDF
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
PPTX
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
PDF
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
PDF
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
PDF
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
PDF
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
PDF
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
PDF
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
PDF
July Patch Tuesday
Ivanti
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
Jak MŚP w Europie Środkowo-Wschodniej odnajdują się w świecie AI
dominikamizerska1
 
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
SFWelly Summer 25 Release Highlights July 2025
Anna Loughnan Colquhoun
 
Exolore The Essential AI Tools in 2025.pdf
Srinivasan M
 
Predicting the unpredictable: re-engineering recommendation algorithms for fr...
Speck&Tech
 
Timothy Rottach - Ramp up on AI Use Cases, from Vector Search to AI Agents wi...
AWS Chicago
 
Windsurf Meetup Ottawa 2025-07-12 - Planning Mode at Reliza.pdf
Pavel Shukhman
 
"Beyond English: Navigating the Challenges of Building a Ukrainian-language R...
Fwdays
 
CIFDAQ Market Insights for July 7th 2025
CIFDAQ
 
Smart Trailers 2025 Update with History and Overview
Paul Menig
 
Building Search Using OpenSearch: Limitations and Workarounds
Sease
 
HubSpot Main Hub: A Unified Growth Platform
Jaswinder Singh
 
Transcript: New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
New from BookNet Canada for 2025: BNC BiblioShare - Tech Forum 2025
BookNet Canada
 
How Startups Are Growing Faster with App Developers in Australia.pdf
India App Developer
 
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
Empower Inclusion Through Accessible Java Applications
Ana-Maria Mihalceanu
 
Presentation - Vibe Coding The Future of Tech
yanuarsinggih1
 
July Patch Tuesday
Ivanti
 

Unit testing Ch. 13 of Programming Ruby