SlideShare a Scribd company logo
1 of 22
Download to read offline
Testing
Test Driven Development


• Implementation
• Red-Green-Refactor
• Regression Tests
Implementation


• Test what should happen
• Test what should not happen
Red-Green-Refactor
      Add a test

     Run all tests
   Write some code

  Run the tests again

    Refactor code
Regression Tests

Change environment

Change code

Is everything still working
Testing in php

$this->get("http://myserver/login.php");
$this->assertWantedPattern("Please login to continue");

$this->setField("username", "MyTestUser");
$this->setField("password", "t0ps3cr3t");
$this->clickSubmit("Login");

$this->assertWantedPattern("You are logged in");
Testing in Java

beginAt("login.jsp");
assertTextInElement("h1", "Please login to continue");

setFormElement("username", "MyTestUser");
setFormElement("password", "t0ps3cr3t");
submit();

assertTextInElement("h1", "You are logged in");
Testing in Rails

visit login_path
assert_contain "Please login to continue"

fill_in "username", :with => "MyTestUser"
fill_in "password", :with => "t0ps3cr3t"
click_button "Login"

assert_contain "You are logged in"
Test Types

• Unit
• Functional
• Integration
Unit


• Most basic level of testing
• Model tests in Rails
Unit Testing
                          class ShipTest
class Ship
                           def test_crew
 attr_accessor :captain
                             ship = Ship.new
 attr_accessor :maties
                             ship.captain = 1
                             ship.mateys = 20
 def crew
  captain + maties
                            assert_equal 21, ship.crew
 end
                           end
end
                          end
Functional


• Test lifecycle of objects
• Controller tests in Rails
Functional Testing
class ShipsController
 def enter_other_ship
  @ship = Ship.find_captain(params[:captain])
  @ship.gold += 1000
  @ship.save
  redirect_to :action => 'show_loot'
 end
end                          class ShipsControllerTest
                              def test_entering_other_ship
                               post 'enter_other_ship', :captain => "Sparrow"

                              assert_response :redirect
                              assert_equal "Black Pearl", assign(:ship).name
                              assert_equal 1000, assigns(:ship).gold
                             end
                            end
Integration

• Overall application functionalities
• Walk through a series of events
• View Tests / Stories
Integration Testing
                                    def test_attacking_other_ships
                                     Given "another ship" do |ship|
                                      @ship_to_enter = Ship.find(ship)
                                     end

                                     Given "my ship" do
Story: Attacking other ships          @my_ship = Ship.find_by_captain("Sparrow")
 As Captain Sparrow                  end
 I attack another ship
 So I can buy more rum               Given /my $number_of_mateys maties/ do |number_of_mateys|
                                      @ship_to_enter.attackers = number_of_mateys
 Scenario: Attack first ship          end
 Given another ship                  When "we attach the other ship" do
 And my ship                          @my_ship.attacks(@ship_to_enter)
 And my 20 mateys                    end
 When we attack the other ship
 Then 1 pirate dies                  Then /$pirates_lost pirate dies/ do |pirates_lost|
 And we steel 1000 pieces of gold     assert_equal @my_schip.crew - pirates_lost, @ship_to_enter.attackers
                                     end

                                     Then /we steel $pieces_of_gold pieces of gold/ do |pieces_of_gold|
                                      assert_equal pieces_of_gold, @my_ship.gold
                                     end
                                    end
Integration Testing
  Story: Attacking other ships
   As Captain Sparrow
   I attack another ship
   So I can buy more rum

   Scenario: Attack first ship
   Given another ship
   And my ship
   And my 20 mateys
   When we attack the other ship
   Then 1 pirate dies
   And we steel 1000 pieces of gold
def test_attacking_other_ships
 Given "another ship" do |ship|
  @ship_to_enter = Ship.find(ship)
 end

 Given "my ship" do
  @my_ship = Ship.find_by_captain("Sparrow")
 end

 Given /my $number_of_mateys maties/ do |number_of_mateys|
  @ship_to_enter.attackers = number_of_mateys
 end

 When "we attach the other ship" do
  @my_ship.attacks(@ship_to_enter)
 end

 Then /$pirates_lost pirate dies/ do |pirates_lost|
  assert_equal @my_schip.crew - pirates_lost, @ship_to_enter.attackers
 end

 Then /we steel $pieces_of_gold pieces of gold/ do |pieces_of_gold|
  assert_equal pieces_of_gold, @my_ship.gold
 end
end
Test Data


• Mock/stub
• Fixtures
• Factories
Mocking & Stubbing
   def test_getting_tweet_on_homepage
    response = mock
    response.stubs(:authorization).returns(true)
    response.stubs(:last_tweet).resturns("#arrrrcamp rocks")
    TwitterAPI.expects(:get_last_tweet).returns(response)

    get 'index'
    assert_equal "#arrrrcamp rocks", assigns(:tweet).body
   end



- Mocha
- Rspec
- Flex Mock
Fixtures
pirates.yml              ships.yml
 captain_jack_sparrow:   black_pearl:
  name: Jack Sparrow      name: The Black Pearl
  enemy: Royal Navy       max_crew: 85
                          captain: captain_jack_sparrow
                         interceptor:
                           name: The Interceptor
                           max_crew: 150
                           captain: captain_jack_sparrow
Factories
Factory.sequence :pirate do |n|
 "matey#{n}"
end

Factory.define :ship do |f|             should "only find big ships" do
 f.name     'Pirateship'                Factory(:ship, :max_crew => 500)
 f.max_crew 100                         Factory(:ship, :max_crew => 200)
 f.captain { Factory.next(:pirate) }
end                                     ships = Ship.big_ones
                                        assert_equal 1, ships.size
                                       end

- Factory Girl
- Machinist
- Object Daddy
- Foundry
- Fixjour
Questions

More Related Content

Viewers also liked

Aforismes i poemes
Aforismes i poemesAforismes i poemes
Aforismes i poemesannacasellas
 
Мягкое управление командой проекта
Мягкое управление командой проектаМягкое управление командой проекта
Мягкое управление командой проектаAnton Kuchumov
 
Control Pacients Fumadors
Control Pacients FumadorsControl Pacients Fumadors
Control Pacients Fumadorsjosepcuadrado
 
Am j physiol heart circ physiol 2000-matsubara-h1534-9
Am j physiol heart circ physiol 2000-matsubara-h1534-9Am j physiol heart circ physiol 2000-matsubara-h1534-9
Am j physiol heart circ physiol 2000-matsubara-h1534-9Natasha Xavier
 
Laserfiche document management solutions
Laserfiche document management solutionsLaserfiche document management solutions
Laserfiche document management solutionsrobnjoro
 
Cooperation needs on Field Operational Tests: FOT Methodology
Cooperation needs on Field Operational Tests: FOT MethodologyCooperation needs on Field Operational Tests: FOT Methodology
Cooperation needs on Field Operational Tests: FOT MethodologyeuroFOT
 
Bolyst - når gamlekjæresten prøver seg igjen
Bolyst - når gamlekjæresten prøver seg igjenBolyst - når gamlekjæresten prøver seg igjen
Bolyst - når gamlekjæresten prøver seg igjenArve Kvalsvik
 
Residents i digitals hospitalet
Residents i digitals hospitaletResidents i digitals hospitalet
Residents i digitals hospitaletArnau Cerdà
 
Shelley's Personal Brand Plan
Shelley's Personal Brand PlanShelley's Personal Brand Plan
Shelley's Personal Brand Planshelleyannethomas
 

Viewers also liked (20)

Guida all'Email Marketing
Guida all'Email MarketingGuida all'Email Marketing
Guida all'Email Marketing
 
Aforismes i poemes
Aforismes i poemesAforismes i poemes
Aforismes i poemes
 
Assessing Learning
Assessing LearningAssessing Learning
Assessing Learning
 
King arthur
King arthurKing arthur
King arthur
 
Artlandis' webinar Season 2 -Open day
Artlandis' webinar Season 2 -Open dayArtlandis' webinar Season 2 -Open day
Artlandis' webinar Season 2 -Open day
 
Unbelievable
UnbelievableUnbelievable
Unbelievable
 
Мягкое управление командой проекта
Мягкое управление командой проектаМягкое управление командой проекта
Мягкое управление командой проекта
 
Airtightphoto
AirtightphotoAirtightphoto
Airtightphoto
 
Tb XDR in South Africa
Tb XDR in South AfricaTb XDR in South Africa
Tb XDR in South Africa
 
Social Caring, il tassello mancante (free webinar)
Social Caring, il tassello mancante (free webinar)Social Caring, il tassello mancante (free webinar)
Social Caring, il tassello mancante (free webinar)
 
Control Pacients Fumadors
Control Pacients FumadorsControl Pacients Fumadors
Control Pacients Fumadors
 
Am j physiol heart circ physiol 2000-matsubara-h1534-9
Am j physiol heart circ physiol 2000-matsubara-h1534-9Am j physiol heart circ physiol 2000-matsubara-h1534-9
Am j physiol heart circ physiol 2000-matsubara-h1534-9
 
Laserfiche document management solutions
Laserfiche document management solutionsLaserfiche document management solutions
Laserfiche document management solutions
 
Filmin
FilminFilmin
Filmin
 
Cooperation needs on Field Operational Tests: FOT Methodology
Cooperation needs on Field Operational Tests: FOT MethodologyCooperation needs on Field Operational Tests: FOT Methodology
Cooperation needs on Field Operational Tests: FOT Methodology
 
Migration
MigrationMigration
Migration
 
Bolyst - når gamlekjæresten prøver seg igjen
Bolyst - når gamlekjæresten prøver seg igjenBolyst - når gamlekjæresten prøver seg igjen
Bolyst - når gamlekjæresten prøver seg igjen
 
Residents i digitals hospitalet
Residents i digitals hospitaletResidents i digitals hospitalet
Residents i digitals hospitalet
 
Shelley's Personal Brand Plan
Shelley's Personal Brand PlanShelley's Personal Brand Plan
Shelley's Personal Brand Plan
 
Creative loverz (workshop)
Creative loverz (workshop)Creative loverz (workshop)
Creative loverz (workshop)
 

More from ArrrrCamp

Arrrrcamp Radiant Intro
Arrrrcamp Radiant IntroArrrrcamp Radiant Intro
Arrrrcamp Radiant IntroArrrrCamp
 
Ruby 1.9 And Rails 3.0
Ruby 1.9 And Rails 3.0Ruby 1.9 And Rails 3.0
Ruby 1.9 And Rails 3.0ArrrrCamp
 
Metaprogramming + Ds Ls
Metaprogramming + Ds LsMetaprogramming + Ds Ls
Metaprogramming + Ds LsArrrrCamp
 
Rubyandrails
RubyandrailsRubyandrails
RubyandrailsArrrrCamp
 
Railsservers
RailsserversRailsservers
RailsserversArrrrCamp
 
Ruby and Rails Basics
Ruby and Rails BasicsRuby and Rails Basics
Ruby and Rails BasicsArrrrCamp
 
Caching your rails application
Caching your rails applicationCaching your rails application
Caching your rails applicationArrrrCamp
 
Advanced Radiant
Advanced RadiantAdvanced Radiant
Advanced RadiantArrrrCamp
 

More from ArrrrCamp (15)

Arrrrcamp Radiant Intro
Arrrrcamp Radiant IntroArrrrcamp Radiant Intro
Arrrrcamp Radiant Intro
 
Ruby 1.9 And Rails 3.0
Ruby 1.9 And Rails 3.0Ruby 1.9 And Rails 3.0
Ruby 1.9 And Rails 3.0
 
Metaprogramming + Ds Ls
Metaprogramming + Ds LsMetaprogramming + Ds Ls
Metaprogramming + Ds Ls
 
Rubyandrails
RubyandrailsRubyandrails
Rubyandrails
 
Nanoc
NanocNanoc
Nanoc
 
Git
GitGit
Git
 
Radiant
RadiantRadiant
Radiant
 
Mistakes
MistakesMistakes
Mistakes
 
Railsservers
RailsserversRailsservers
Railsservers
 
Prawn
PrawnPrawn
Prawn
 
Validation
ValidationValidation
Validation
 
Cucumber
CucumberCucumber
Cucumber
 
Ruby and Rails Basics
Ruby and Rails BasicsRuby and Rails Basics
Ruby and Rails Basics
 
Caching your rails application
Caching your rails applicationCaching your rails application
Caching your rails application
 
Advanced Radiant
Advanced RadiantAdvanced Radiant
Advanced Radiant
 

Recently uploaded

Dubai Call Girls Bikni O528786472 Call Girls Dubai Ebony
Dubai Call Girls Bikni O528786472 Call Girls Dubai EbonyDubai Call Girls Bikni O528786472 Call Girls Dubai Ebony
Dubai Call Girls Bikni O528786472 Call Girls Dubai Ebonyhf8803863
 
IPL Quiz ( weekly quiz) by SJU quizzers.
IPL Quiz ( weekly quiz) by SJU quizzers.IPL Quiz ( weekly quiz) by SJU quizzers.
IPL Quiz ( weekly quiz) by SJU quizzers.SJU Quizzers
 
ppt on Myself, Occupation and my Interest
ppt on Myself, Occupation and my Interestppt on Myself, Occupation and my Interest
ppt on Myself, Occupation and my InterestNagaissenValaydum
 
JORNADA 3 LIGA MURO 2024GHGHGHGHGHGH.pdf
JORNADA 3 LIGA MURO 2024GHGHGHGHGHGH.pdfJORNADA 3 LIGA MURO 2024GHGHGHGHGHGH.pdf
JORNADA 3 LIGA MURO 2024GHGHGHGHGHGH.pdfArturo Pacheco Alvarez
 
Expert Pool Table Refelting in Lee & Collier County, FL
Expert Pool Table Refelting in Lee & Collier County, FLExpert Pool Table Refelting in Lee & Collier County, FL
Expert Pool Table Refelting in Lee & Collier County, FLAll American Billiards
 
8377087607 ☎, Cash On Delivery Call Girls Service In Hauz Khas Delhi Enjoy 24/7
8377087607 ☎, Cash On Delivery Call Girls Service In Hauz Khas Delhi Enjoy 24/78377087607 ☎, Cash On Delivery Call Girls Service In Hauz Khas Delhi Enjoy 24/7
8377087607 ☎, Cash On Delivery Call Girls Service In Hauz Khas Delhi Enjoy 24/7dollysharma2066
 
Resultados del Campeonato mundial de Marcha por equipos Antalya 2024
Resultados del Campeonato mundial de Marcha por equipos Antalya 2024Resultados del Campeonato mundial de Marcha por equipos Antalya 2024
Resultados del Campeonato mundial de Marcha por equipos Antalya 2024Judith Chuquipul
 
JORNADA 4 LIGA MURO 2024TUXTEPEC1234.pdf
JORNADA 4 LIGA MURO 2024TUXTEPEC1234.pdfJORNADA 4 LIGA MURO 2024TUXTEPEC1234.pdf
JORNADA 4 LIGA MURO 2024TUXTEPEC1234.pdfArturo Pacheco Alvarez
 
Croatia vs Italy UEFA Euro 2024 Croatia's Checkered Legacy on Display in New ...
Croatia vs Italy UEFA Euro 2024 Croatia's Checkered Legacy on Display in New ...Croatia vs Italy UEFA Euro 2024 Croatia's Checkered Legacy on Display in New ...
Croatia vs Italy UEFA Euro 2024 Croatia's Checkered Legacy on Display in New ...Eticketing.co
 
办理学位证(KCL文凭证书)伦敦国王学院毕业证成绩单原版一模一样
办理学位证(KCL文凭证书)伦敦国王学院毕业证成绩单原版一模一样办理学位证(KCL文凭证书)伦敦国王学院毕业证成绩单原版一模一样
办理学位证(KCL文凭证书)伦敦国王学院毕业证成绩单原版一模一样7pn7zv3i
 
Technical Data | ThermTec Wild 650 | Optics Trade
Technical Data | ThermTec Wild 650 | Optics TradeTechnical Data | ThermTec Wild 650 | Optics Trade
Technical Data | ThermTec Wild 650 | Optics TradeOptics-Trade
 
France's UEFA Euro 2024 Ambitions Amid Coman's Injury.docx
France's UEFA Euro 2024 Ambitions Amid Coman's Injury.docxFrance's UEFA Euro 2024 Ambitions Amid Coman's Injury.docx
France's UEFA Euro 2024 Ambitions Amid Coman's Injury.docxEuro Cup 2024 Tickets
 
Call Girls in Dhaula Kuan 💯Call Us 🔝8264348440🔝
Call Girls in Dhaula Kuan 💯Call Us 🔝8264348440🔝Call Girls in Dhaula Kuan 💯Call Us 🔝8264348440🔝
Call Girls in Dhaula Kuan 💯Call Us 🔝8264348440🔝soniya singh
 
Real Moto 2 MOD APK v1.1.721 All Bikes, Unlimited Money
Real Moto 2 MOD APK v1.1.721 All Bikes, Unlimited MoneyReal Moto 2 MOD APK v1.1.721 All Bikes, Unlimited Money
Real Moto 2 MOD APK v1.1.721 All Bikes, Unlimited MoneyApk Toly
 
Mysore Call Girls 7001305949 WhatsApp Number 24x7 Best Services
Mysore Call Girls 7001305949 WhatsApp Number 24x7 Best ServicesMysore Call Girls 7001305949 WhatsApp Number 24x7 Best Services
Mysore Call Girls 7001305949 WhatsApp Number 24x7 Best Servicesnajka9823
 
Instruction Manual | ThermTec Wild Thermal Monoculars | Optics Trade
Instruction Manual | ThermTec Wild Thermal Monoculars | Optics TradeInstruction Manual | ThermTec Wild Thermal Monoculars | Optics Trade
Instruction Manual | ThermTec Wild Thermal Monoculars | Optics TradeOptics-Trade
 
Technical Data | ThermTec Wild 650L | Optics Trade
Technical Data | ThermTec Wild 650L | Optics TradeTechnical Data | ThermTec Wild 650L | Optics Trade
Technical Data | ThermTec Wild 650L | Optics TradeOptics-Trade
 
Technical Data | ThermTec Wild 335 | Optics Trade
Technical Data | ThermTec Wild 335 | Optics TradeTechnical Data | ThermTec Wild 335 | Optics Trade
Technical Data | ThermTec Wild 335 | Optics TradeOptics-Trade
 

Recently uploaded (20)

Dubai Call Girls Bikni O528786472 Call Girls Dubai Ebony
Dubai Call Girls Bikni O528786472 Call Girls Dubai EbonyDubai Call Girls Bikni O528786472 Call Girls Dubai Ebony
Dubai Call Girls Bikni O528786472 Call Girls Dubai Ebony
 
IPL Quiz ( weekly quiz) by SJU quizzers.
IPL Quiz ( weekly quiz) by SJU quizzers.IPL Quiz ( weekly quiz) by SJU quizzers.
IPL Quiz ( weekly quiz) by SJU quizzers.
 
ppt on Myself, Occupation and my Interest
ppt on Myself, Occupation and my Interestppt on Myself, Occupation and my Interest
ppt on Myself, Occupation and my Interest
 
young Call girls in Moolchand 🔝 9953056974 🔝 Delhi escort Service
young Call girls in Moolchand 🔝 9953056974 🔝 Delhi escort Serviceyoung Call girls in Moolchand 🔝 9953056974 🔝 Delhi escort Service
young Call girls in Moolchand 🔝 9953056974 🔝 Delhi escort Service
 
JORNADA 3 LIGA MURO 2024GHGHGHGHGHGH.pdf
JORNADA 3 LIGA MURO 2024GHGHGHGHGHGH.pdfJORNADA 3 LIGA MURO 2024GHGHGHGHGHGH.pdf
JORNADA 3 LIGA MURO 2024GHGHGHGHGHGH.pdf
 
Expert Pool Table Refelting in Lee & Collier County, FL
Expert Pool Table Refelting in Lee & Collier County, FLExpert Pool Table Refelting in Lee & Collier County, FL
Expert Pool Table Refelting in Lee & Collier County, FL
 
FULL ENJOY Call Girls In Savitri Nagar (Delhi) Call Us 9953056974
FULL ENJOY Call Girls In  Savitri Nagar (Delhi) Call Us 9953056974FULL ENJOY Call Girls In  Savitri Nagar (Delhi) Call Us 9953056974
FULL ENJOY Call Girls In Savitri Nagar (Delhi) Call Us 9953056974
 
8377087607 ☎, Cash On Delivery Call Girls Service In Hauz Khas Delhi Enjoy 24/7
8377087607 ☎, Cash On Delivery Call Girls Service In Hauz Khas Delhi Enjoy 24/78377087607 ☎, Cash On Delivery Call Girls Service In Hauz Khas Delhi Enjoy 24/7
8377087607 ☎, Cash On Delivery Call Girls Service In Hauz Khas Delhi Enjoy 24/7
 
Resultados del Campeonato mundial de Marcha por equipos Antalya 2024
Resultados del Campeonato mundial de Marcha por equipos Antalya 2024Resultados del Campeonato mundial de Marcha por equipos Antalya 2024
Resultados del Campeonato mundial de Marcha por equipos Antalya 2024
 
JORNADA 4 LIGA MURO 2024TUXTEPEC1234.pdf
JORNADA 4 LIGA MURO 2024TUXTEPEC1234.pdfJORNADA 4 LIGA MURO 2024TUXTEPEC1234.pdf
JORNADA 4 LIGA MURO 2024TUXTEPEC1234.pdf
 
Croatia vs Italy UEFA Euro 2024 Croatia's Checkered Legacy on Display in New ...
Croatia vs Italy UEFA Euro 2024 Croatia's Checkered Legacy on Display in New ...Croatia vs Italy UEFA Euro 2024 Croatia's Checkered Legacy on Display in New ...
Croatia vs Italy UEFA Euro 2024 Croatia's Checkered Legacy on Display in New ...
 
办理学位证(KCL文凭证书)伦敦国王学院毕业证成绩单原版一模一样
办理学位证(KCL文凭证书)伦敦国王学院毕业证成绩单原版一模一样办理学位证(KCL文凭证书)伦敦国王学院毕业证成绩单原版一模一样
办理学位证(KCL文凭证书)伦敦国王学院毕业证成绩单原版一模一样
 
Technical Data | ThermTec Wild 650 | Optics Trade
Technical Data | ThermTec Wild 650 | Optics TradeTechnical Data | ThermTec Wild 650 | Optics Trade
Technical Data | ThermTec Wild 650 | Optics Trade
 
France's UEFA Euro 2024 Ambitions Amid Coman's Injury.docx
France's UEFA Euro 2024 Ambitions Amid Coman's Injury.docxFrance's UEFA Euro 2024 Ambitions Amid Coman's Injury.docx
France's UEFA Euro 2024 Ambitions Amid Coman's Injury.docx
 
Call Girls in Dhaula Kuan 💯Call Us 🔝8264348440🔝
Call Girls in Dhaula Kuan 💯Call Us 🔝8264348440🔝Call Girls in Dhaula Kuan 💯Call Us 🔝8264348440🔝
Call Girls in Dhaula Kuan 💯Call Us 🔝8264348440🔝
 
Real Moto 2 MOD APK v1.1.721 All Bikes, Unlimited Money
Real Moto 2 MOD APK v1.1.721 All Bikes, Unlimited MoneyReal Moto 2 MOD APK v1.1.721 All Bikes, Unlimited Money
Real Moto 2 MOD APK v1.1.721 All Bikes, Unlimited Money
 
Mysore Call Girls 7001305949 WhatsApp Number 24x7 Best Services
Mysore Call Girls 7001305949 WhatsApp Number 24x7 Best ServicesMysore Call Girls 7001305949 WhatsApp Number 24x7 Best Services
Mysore Call Girls 7001305949 WhatsApp Number 24x7 Best Services
 
Instruction Manual | ThermTec Wild Thermal Monoculars | Optics Trade
Instruction Manual | ThermTec Wild Thermal Monoculars | Optics TradeInstruction Manual | ThermTec Wild Thermal Monoculars | Optics Trade
Instruction Manual | ThermTec Wild Thermal Monoculars | Optics Trade
 
Technical Data | ThermTec Wild 650L | Optics Trade
Technical Data | ThermTec Wild 650L | Optics TradeTechnical Data | ThermTec Wild 650L | Optics Trade
Technical Data | ThermTec Wild 650L | Optics Trade
 
Technical Data | ThermTec Wild 335 | Optics Trade
Technical Data | ThermTec Wild 335 | Optics TradeTechnical Data | ThermTec Wild 335 | Optics Trade
Technical Data | ThermTec Wild 335 | Optics Trade
 

Testing

  • 2. Test Driven Development • Implementation • Red-Green-Refactor • Regression Tests
  • 3. Implementation • Test what should happen • Test what should not happen
  • 4. Red-Green-Refactor Add a test Run all tests Write some code Run the tests again Refactor code
  • 5. Regression Tests Change environment Change code Is everything still working
  • 6. Testing in php $this->get("http://myserver/login.php"); $this->assertWantedPattern("Please login to continue"); $this->setField("username", "MyTestUser"); $this->setField("password", "t0ps3cr3t"); $this->clickSubmit("Login"); $this->assertWantedPattern("You are logged in");
  • 7. Testing in Java beginAt("login.jsp"); assertTextInElement("h1", "Please login to continue"); setFormElement("username", "MyTestUser"); setFormElement("password", "t0ps3cr3t"); submit(); assertTextInElement("h1", "You are logged in");
  • 8. Testing in Rails visit login_path assert_contain "Please login to continue" fill_in "username", :with => "MyTestUser" fill_in "password", :with => "t0ps3cr3t" click_button "Login" assert_contain "You are logged in"
  • 9. Test Types • Unit • Functional • Integration
  • 10. Unit • Most basic level of testing • Model tests in Rails
  • 11. Unit Testing class ShipTest class Ship def test_crew attr_accessor :captain ship = Ship.new attr_accessor :maties ship.captain = 1 ship.mateys = 20 def crew captain + maties assert_equal 21, ship.crew end end end end
  • 12. Functional • Test lifecycle of objects • Controller tests in Rails
  • 13. Functional Testing class ShipsController def enter_other_ship @ship = Ship.find_captain(params[:captain]) @ship.gold += 1000 @ship.save redirect_to :action => 'show_loot' end end class ShipsControllerTest def test_entering_other_ship post 'enter_other_ship', :captain => "Sparrow" assert_response :redirect assert_equal "Black Pearl", assign(:ship).name assert_equal 1000, assigns(:ship).gold end end
  • 14. Integration • Overall application functionalities • Walk through a series of events • View Tests / Stories
  • 15. Integration Testing def test_attacking_other_ships Given "another ship" do |ship| @ship_to_enter = Ship.find(ship) end Given "my ship" do Story: Attacking other ships @my_ship = Ship.find_by_captain("Sparrow") As Captain Sparrow end I attack another ship So I can buy more rum Given /my $number_of_mateys maties/ do |number_of_mateys| @ship_to_enter.attackers = number_of_mateys Scenario: Attack first ship end Given another ship When "we attach the other ship" do And my ship @my_ship.attacks(@ship_to_enter) And my 20 mateys end When we attack the other ship Then 1 pirate dies Then /$pirates_lost pirate dies/ do |pirates_lost| And we steel 1000 pieces of gold assert_equal @my_schip.crew - pirates_lost, @ship_to_enter.attackers end Then /we steel $pieces_of_gold pieces of gold/ do |pieces_of_gold| assert_equal pieces_of_gold, @my_ship.gold end end
  • 16. Integration Testing Story: Attacking other ships As Captain Sparrow I attack another ship So I can buy more rum Scenario: Attack first ship Given another ship And my ship And my 20 mateys When we attack the other ship Then 1 pirate dies And we steel 1000 pieces of gold
  • 17. def test_attacking_other_ships Given "another ship" do |ship| @ship_to_enter = Ship.find(ship) end Given "my ship" do @my_ship = Ship.find_by_captain("Sparrow") end Given /my $number_of_mateys maties/ do |number_of_mateys| @ship_to_enter.attackers = number_of_mateys end When "we attach the other ship" do @my_ship.attacks(@ship_to_enter) end Then /$pirates_lost pirate dies/ do |pirates_lost| assert_equal @my_schip.crew - pirates_lost, @ship_to_enter.attackers end Then /we steel $pieces_of_gold pieces of gold/ do |pieces_of_gold| assert_equal pieces_of_gold, @my_ship.gold end end
  • 18. Test Data • Mock/stub • Fixtures • Factories
  • 19. Mocking & Stubbing def test_getting_tweet_on_homepage response = mock response.stubs(:authorization).returns(true) response.stubs(:last_tweet).resturns("#arrrrcamp rocks") TwitterAPI.expects(:get_last_tweet).returns(response) get 'index' assert_equal "#arrrrcamp rocks", assigns(:tweet).body end - Mocha - Rspec - Flex Mock
  • 20. Fixtures pirates.yml ships.yml captain_jack_sparrow: black_pearl: name: Jack Sparrow name: The Black Pearl enemy: Royal Navy max_crew: 85 captain: captain_jack_sparrow interceptor: name: The Interceptor max_crew: 150 captain: captain_jack_sparrow
  • 21. Factories Factory.sequence :pirate do |n| "matey#{n}" end Factory.define :ship do |f| should "only find big ships" do f.name 'Pirateship' Factory(:ship, :max_crew => 500) f.max_crew 100 Factory(:ship, :max_crew => 200) f.captain { Factory.next(:pirate) } end ships = Ship.big_ones assert_equal 1, ships.size end - Factory Girl - Machinist - Object Daddy - Foundry - Fixjour