SlideShare a Scribd company logo
1 of 45
Download to read offline
Changing Your Mindset
Getting Started With Test-Driven Development
Patrick Reagan
Director, Application Development
patrick@viget.com
Overview
  Testing Process
  Test::Unit Introduction
  Code Example: GoogleRank
  Testing Pitfalls
  Coverage Analysis
  Next Steps
Changing Your Mindset
Getting Started With Test-Driven Development
September 7th, 2007
Test-Driven Development is the
          process of testing the behavior of
                non-existent objects




Changing Your Mindset
Getting Started With Test-Driven Development
September 7th, 2007
Testing Cycle

                                                  Write
                                               Failing Test


                                                              Write
                      Refactor
                                                              Code


                                                 Verify
                                                Success

Changing Your Mindset
Getting Started With Test-Driven Development
September 7th, 2007
Why Test First?

  No untested code is written
  Start with higher code coverage
  Rapid feedback
  Build your regressions as you go


Changing Your Mindset
Getting Started With Test-Driven Development
September 7th, 2007
Anatomy of a (Test::Unit) Test Case

                                               Test case

                                               Test setup
                                               (run every test)


                                               Test




                                               Assertion




Changing Your Mindset
Getting Started With Test-Driven Development
September 7th, 2007
Naming Conventions



        Method to test


                                Parameters / Input


                                                     Expected behavior
                                                        (should ...)




Changing Your Mindset
Getting Started With Test-Driven Development
September 7th, 2007
Basic Assertions
         assert


         assert_equal / assert_not_equal

         assert_nil / assert_not_nil


         assert_match / assert_no_match

         assert_raise / assert_nothing_raised




Changing Your Mindset
Getting Started With Test-Driven Development
September 7th, 2007
Code Example
 Class that retrieves Google results
               Retrieves only the top 10 URLs
          

               URL encodes provided search terms
          

               Makes an HTTP connection to Google
          

               Raises an exception when unavailable
          




Changing Your Mindset
Getting Started With Test-Driven Development
September 7th, 2007
Start Small




 test_google_rank_should_exist(GoogleRankTest):
 Exception raised: Class: <NameError>
 Message: <quot;uninitialized constant GoogleRankTest::GoogleRankquot;>

 1 tests, 0 assertions, 1 failures, 0 errors




 1 tests, 0 assertions, 0 failures, 0 errors




Changing Your Mindset
Getting Started With Test-Driven Development
September 7th, 2007
Simplest Thing That Works

                                                          Class method

 test_parse_with_empty_document_should_return_empty_array(GoogleRankTest):
 NoMethodError: undefined method `parse' for GoogleRank:Class

 1 tests, 0 assertions, 0 failures, 1 errors




                                               Instance method


 test_parse_with_empty_document_should_return_empty_array(GoogleRankTest):
 NoMethodError: undefined method `parse' for GoogleRank:Class

 1 tests, 0 assertions, 0 failures, 1 errors



Changing Your Mindset
Getting Started With Test-Driven Development
September 7th, 2007
Simplest Thing That Actually Works




  2 tests, 1 assertions, 0 failures, 0 errors




Changing Your Mindset
Getting Started With Test-Driven Development
September 7th, 2007
Use a Canned Data Set

                                                                Sample markup
                                                             from Google search




test_parse_with_sample_document_should_return_list_of_urls
<[quot;http://blog.viget.com/quot;, ..... > expected but was <[]>.

1 tests, 1 assertions, 1 failures, 0 errors




Changing Your Mindset
Getting Started With Test-Driven Development
September 7th, 2007
Use a Canned Data Set




1 tests, 1 assertions, 0 failures, 0 errors




Changing Your Mindset
Getting Started With Test-Driven Development
September 7th, 2007
Running Regressions
  test_parse_with_empty_document_should_return_empty_array(GoogleRankTest):
  NoMethodError: undefined method `[]' for nil:NilClass

  3 tests, 1 assertions, 0 failures, 1 errors




                                                     results.nil? == true




Changing Your Mindset
Getting Started With Test-Driven Development
September 7th, 2007
Fix Regressions




 3 tests, 2 assertions, 0 failures, 0 errors




Changing Your Mindset
Getting Started With Test-Driven Development
September 7th, 2007
Code Example
 Class that retrieves Google results
               Retrieves only the top 10 URLs
          

               URL encodes provided search terms
          

               Makes an HTTP connection to Google
          

               Raises an exception when unavailable
          




Changing Your Mindset
Getting Started With Test-Driven Development
September 7th, 2007
Retrieve Internal State




                                      Inspect instance variable

NameError: uninitialized constant GoogleRankTest::ERB




Changing Your Mindset
Getting Started With Test-Driven Development
September 7th, 2007
Retrieve Internal State




test_new_should_encode_supplied_keywords(GoogleRankTest):
ArgumentError: wrong number of arguments (1 for 0)

1 tests, 0 assertions, 0 failures, 1 errors




Changing Your Mindset
Getting Started With Test-Driven Development
September 7th, 2007
Retrieve Internal State




1 tests, 1 assertions, 0 failures, 0 errors


4 tests, 3 assertions, 0 failures, 0 errors




Changing Your Mindset
Getting Started With Test-Driven Development
September 7th, 2007
Refactor Test Duplication




4 tests, 3 assertions, 0 failures, 0 errors




Changing Your Mindset
Getting Started With Test-Driven Development
September 7th, 2007
Testing Encoding




test_new_should_assign_url_with_base_and_encoded_keywords(GoogleRankTest):
NameError: uninitialized constant GoogleRank::BASE_URL

1 tests, 0 assertions, 0 failures, 1 errors




Changing Your Mindset
Getting Started With Test-Driven Development
September 7th, 2007
Testing Encoding




1 tests, 1 assertions, 0 failures, 0 errors


5 tests, 4 assertions, 0 failures, 0 errors


Changing Your Mindset
Getting Started With Test-Driven Development
September 7th, 2007
Code Example
 Class that retrieves Google results
               Retrieves only the top 10 URLs
          

               URL encodes provided search terms
          

               Makes an HTTP connection to Google
          

               Raises an exception when unavailable
          




Changing Your Mindset
Getting Started With Test-Driven Development
September 7th, 2007
“Mock objects are simulated
       objects that mimic the behavior of
       real objects in controlled ways”
                       - Wikipedia




Changing Your Mindset
Getting Started With Test-Driven Development
September 7th, 2007
Mocks and Stubs

  Remove external dependencies
  Create known state
  Focus tests on specific code paths




Changing Your Mindset
Getting Started With Test-Driven Development
September 7th, 2007
Mocha to Stub Net::HTTP



                                                        Implementation will use
                                                             Net::HTTP#get




 test_retrieve_content_should_set_content(GoogleRankTest):
 NoMethodError: undefined method `retrieve_content' for #<GoogleRank:0x104053c>

 1 tests, 0 assertions, 0 failures, 1 errors



Changing Your Mindset
Getting Started With Test-Driven Development
September 7th, 2007
Mocha to Stub Net::HTTP




6 tests, 5 assertions, 0 failures, 0 errors



Changing Your Mindset
Getting Started With Test-Driven Development
September 7th, 2007
No Network? Oops!



  $ irb --prompt simple
  >> gr = GoogleRank.new('rails')
  => #<GoogleRank:0x54c89c @encoded_keywords=quot;railsquot;,
       @url=quot;http://www.google.com/search?q=railsquot;>
  >> gr.retrieve_content
  SocketError: getaddrinfo: No address associated with nodename




Changing Your Mindset
Getting Started With Test-Driven Development
September 7th, 2007
Replicate Real-Word Conditions


                                                         Expected failure




test_retrieve_content_when_connection_fails_should_set_content_to_nil(GoogleRankTest):
SocketError: SocketError

1 tests, 0 assertions, 0 failures, 1 errors




Changing Your Mindset
Getting Started With Test-Driven Development
September 7th, 2007
Dealing With Failure




7 tests, 6 assertions, 0 failures, 0 errors




Changing Your Mindset
Getting Started With Test-Driven Development
September 7th, 2007
Code Example
 Class that retrieves Google results
               Retrieves only the top 10 URLs
          

               URL encodes provided search terms
          

               Makes an HTTP connection to Google
          

               Raises an exception when unavailable
          




Changing Your Mindset
Getting Started With Test-Driven Development
September 7th, 2007
Dealing With Even More Failure




test_retrieve_content_when_connection_fails_should_raise_exception_and_set_content_to_nil(GoogleRankTest)
<GoogleRank::ConnectionError> exception expected but none was thrown.

test_retrieve_content_with_failure_should_raise_exception(GoogleRankTest)
<GoogleRank::ConnectionError> exception expected but was
Class: <NoMethodError> Message: <quot;undefined method `closed?' for nil:NilClassquot;>

7 tests, 6 assertions, 2 failures, 0 errors



  Changing Your Mindset
  Getting Started With Test-Driven Development
  September 7th, 2007
Handling Unsuccessful Requests




8 tests, 8 assertions, 0 failures, 0 errors


Changing Your Mindset
Getting Started With Test-Driven Development
September 7th, 2007
Testing Pitfalls

 Things to avoid
               Over-mocking
          

               Invalid mocks
          

               Testing library code
          

               “Assertion-heavy” tests
          

               Non-descriptive test names
          




Changing Your Mindset
Getting Started With Test-Driven Development
September 7th, 2007
Testing Pitfalls



                                                     Should test parsing




                                               HTTPResponse#content
                                               does not exist




Changing Your Mindset
Getting Started With Test-Driven Development
September 7th, 2007
Testing Pitfalls


                                               Use secondary test
                                               to verify


                                                  Expected behavior?




Changing Your Mindset
Getting Started With Test-Driven Development
September 7th, 2007
Coverage Analysis


  Shows ‘tested’ code
  Indication of when to stop
  Can present a false impression




Changing Your Mindset
Getting Started With Test-Driven Development
September 7th, 2007
Coverage With RCov




                                               Exception-prone




Changing Your Mindset
Getting Started With Test-Driven Development
September 7th, 2007
Next Steps?

  Rake - Rake::TestTask
  Mock expectations as tests
  Testing Macros




Changing Your Mindset
Getting Started With Test-Driven Development
September 7th, 2007
Rake::TestTask Example




 $ rake -T
 rake test:all           # Run tests for all




Changing Your Mindset
Getting Started With Test-Driven Development
September 7th, 2007
Expectations as Tests




                                               attr_accessor :content
                                                     called once




Changing Your Mindset
Getting Started With Test-Driven Development
September 7th, 2007
Testing Macros




Changing Your Mindset
Getting Started With Test-Driven Development
September 7th, 2007
Resources
 Test::Unit
           http://ruby-doc.org/stdlib
     

 Mocha
           http://mocha.rubyforge.org
     

 RCov
           http://eigenclass.org/hiki/rcov
     

 Rake
           http://rake.rubyforge.org
     

 How to Test Validations (Expectations as Tests)
           http://relevancellc.com/2007/7/16/how-to-test-validations-part-4
     

 Testing Macros
           http://www.extendviget.com
     



Changing Your Mindset
Getting Started With Test-Driven Development
September 7th, 2007
Slides
                         http://www.slideshare.net/viget

                                                Blog
                                     http://blog.viget.com
                                     http://www.sneaq.net

                                               Contact
                                         patrick@viget.com



Changing Your Mindset
Getting Started With Test-Driven Development
September 7th, 2007

More Related Content

Viewers also liked

Quality Mindset. Health & Care Radicals Inspiring Industrial Quality Improvement
Quality Mindset. Health & Care Radicals Inspiring Industrial Quality ImprovementQuality Mindset. Health & Care Radicals Inspiring Industrial Quality Improvement
Quality Mindset. Health & Care Radicals Inspiring Industrial Quality ImprovementCeline Schillinger
 
Quality Use of Medicines
Quality Use of MedicinesQuality Use of Medicines
Quality Use of Medicineskbaskett
 
Changing your perspective changes Everything!
Changing your perspective changes Everything!Changing your perspective changes Everything!
Changing your perspective changes Everything!Robert Alan McKenzie
 
How to Reduce Readmissions by Changing Patient Education
How to Reduce Readmissions by Changing Patient EducationHow to Reduce Readmissions by Changing Patient Education
How to Reduce Readmissions by Changing Patient EducationChuck Jones
 
Presentation on tqm
Presentation on tqmPresentation on tqm
Presentation on tqmMilton Kumar
 
Adapting to change-Emotional Intelligence
 Adapting to change-Emotional Intelligence  Adapting to change-Emotional Intelligence
Adapting to change-Emotional Intelligence Dr Funke Baffour
 
Enonic Content Repository built on elasticsearch
Enonic Content Repository built on elasticsearchEnonic Content Repository built on elasticsearch
Enonic Content Repository built on elasticsearchenonic
 
Elastic{on} - Tracking of events within ING
Elastic{on} - Tracking of events within INGElastic{on} - Tracking of events within ING
Elastic{on} - Tracking of events within INGING-IT
 
Mahindra & mahindra limited tqm and deming award
Mahindra & mahindra limited tqm and deming awardMahindra & mahindra limited tqm and deming award
Mahindra & mahindra limited tqm and deming awardsanjay p
 
the agile mindset, a learning lab
the agile mindset, a learning labthe agile mindset, a learning lab
the agile mindset, a learning labnikos batsios
 
Toxic Workplace: High School at the Office
Toxic Workplace: High School at the OfficeToxic Workplace: High School at the Office
Toxic Workplace: High School at the OfficeSusan Nicholas
 
Personal Mindset To Change
Personal Mindset To ChangePersonal Mindset To Change
Personal Mindset To Changenick_lim
 
Psychology and High Performance Organizations
Psychology and High Performance Organizations Psychology and High Performance Organizations
Psychology and High Performance Organizations John Willis
 
Getting Started with MongoDB and Node.js
Getting Started with MongoDB and Node.jsGetting Started with MongoDB and Node.js
Getting Started with MongoDB and Node.jsGrant Goodale
 
Book review on who moved my cheese?
Book review on who moved my cheese?Book review on who moved my cheese?
Book review on who moved my cheese?Shwetanshu Gupta
 
"Who moved my cheese" - Business Classics Presentation
"Who moved my cheese" - Business Classics Presentation"Who moved my cheese" - Business Classics Presentation
"Who moved my cheese" - Business Classics PresentationAegis Global Academy
 
Positive change = mindset x tools
Positive change = mindset x toolsPositive change = mindset x tools
Positive change = mindset x toolssrprs.me
 
ROOM 1 - How to Create a High-Performance Culture - By Gian Tu Trung
ROOM 1 - How to Create a High-Performance Culture - By Gian Tu TrungROOM 1 - How to Create a High-Performance Culture - By Gian Tu Trung
ROOM 1 - How to Create a High-Performance Culture - By Gian Tu TrungVietnam HR Summit
 

Viewers also liked (20)

Quality use of medicine
Quality use of medicineQuality use of medicine
Quality use of medicine
 
Quality Mindset. Health & Care Radicals Inspiring Industrial Quality Improvement
Quality Mindset. Health & Care Radicals Inspiring Industrial Quality ImprovementQuality Mindset. Health & Care Radicals Inspiring Industrial Quality Improvement
Quality Mindset. Health & Care Radicals Inspiring Industrial Quality Improvement
 
Quality Use of Medicines
Quality Use of MedicinesQuality Use of Medicines
Quality Use of Medicines
 
Changing your perspective changes Everything!
Changing your perspective changes Everything!Changing your perspective changes Everything!
Changing your perspective changes Everything!
 
How to Reduce Readmissions by Changing Patient Education
How to Reduce Readmissions by Changing Patient EducationHow to Reduce Readmissions by Changing Patient Education
How to Reduce Readmissions by Changing Patient Education
 
Dealing With Changes
Dealing With ChangesDealing With Changes
Dealing With Changes
 
Presentation on tqm
Presentation on tqmPresentation on tqm
Presentation on tqm
 
Adapting to change-Emotional Intelligence
 Adapting to change-Emotional Intelligence  Adapting to change-Emotional Intelligence
Adapting to change-Emotional Intelligence
 
Enonic Content Repository built on elasticsearch
Enonic Content Repository built on elasticsearchEnonic Content Repository built on elasticsearch
Enonic Content Repository built on elasticsearch
 
Elastic{on} - Tracking of events within ING
Elastic{on} - Tracking of events within INGElastic{on} - Tracking of events within ING
Elastic{on} - Tracking of events within ING
 
Mahindra & mahindra limited tqm and deming award
Mahindra & mahindra limited tqm and deming awardMahindra & mahindra limited tqm and deming award
Mahindra & mahindra limited tqm and deming award
 
the agile mindset, a learning lab
the agile mindset, a learning labthe agile mindset, a learning lab
the agile mindset, a learning lab
 
Toxic Workplace: High School at the Office
Toxic Workplace: High School at the OfficeToxic Workplace: High School at the Office
Toxic Workplace: High School at the Office
 
Personal Mindset To Change
Personal Mindset To ChangePersonal Mindset To Change
Personal Mindset To Change
 
Psychology and High Performance Organizations
Psychology and High Performance Organizations Psychology and High Performance Organizations
Psychology and High Performance Organizations
 
Getting Started with MongoDB and Node.js
Getting Started with MongoDB and Node.jsGetting Started with MongoDB and Node.js
Getting Started with MongoDB and Node.js
 
Book review on who moved my cheese?
Book review on who moved my cheese?Book review on who moved my cheese?
Book review on who moved my cheese?
 
"Who moved my cheese" - Business Classics Presentation
"Who moved my cheese" - Business Classics Presentation"Who moved my cheese" - Business Classics Presentation
"Who moved my cheese" - Business Classics Presentation
 
Positive change = mindset x tools
Positive change = mindset x toolsPositive change = mindset x tools
Positive change = mindset x tools
 
ROOM 1 - How to Create a High-Performance Culture - By Gian Tu Trung
ROOM 1 - How to Create a High-Performance Culture - By Gian Tu TrungROOM 1 - How to Create a High-Performance Culture - By Gian Tu Trung
ROOM 1 - How to Create a High-Performance Culture - By Gian Tu Trung
 

Similar to Getting Started TDD Guide Google Search Class

From 0 to 100: How we jump-started our frontend testing
From 0 to 100: How we jump-started our frontend testingFrom 0 to 100: How we jump-started our frontend testing
From 0 to 100: How we jump-started our frontend testingHenning Muszynski
 
Hands On with Selenium and WebDriver
Hands On with Selenium and WebDriverHands On with Selenium and WebDriver
Hands On with Selenium and WebDriverTechWell
 
Writing useful automated tests for the single page applications you build
Writing useful automated tests for the single page applications you buildWriting useful automated tests for the single page applications you build
Writing useful automated tests for the single page applications you buildAndrei Sebastian Cîmpean
 
Unit Testing and Behavior Driven Testing with AngularJS - Jesse Liberty | Fal...
Unit Testing and Behavior Driven Testing with AngularJS - Jesse Liberty | Fal...Unit Testing and Behavior Driven Testing with AngularJS - Jesse Liberty | Fal...
Unit Testing and Behavior Driven Testing with AngularJS - Jesse Liberty | Fal...FalafelSoftware
 
Selenium testing IDE 101
Selenium testing IDE 101Selenium testing IDE 101
Selenium testing IDE 101Adam Culp
 
10 Principles of Apex Testing
10 Principles of Apex Testing10 Principles of Apex Testing
10 Principles of Apex TestingKevin Poorman
 
Unit Test in Ruby on Rails by Minitest
Unit Test in Ruby on Rails by MinitestUnit Test in Ruby on Rails by Minitest
Unit Test in Ruby on Rails by MinitestHosang Jeon
 
Unit Test Your Database
Unit Test Your DatabaseUnit Test Your Database
Unit Test Your DatabaseDavid Wheeler
 
Approval Tests in Action: A LEGO Exercise and an Experience Report
Approval Tests in Action: A LEGO Exercise and an Experience ReportApproval Tests in Action: A LEGO Exercise and an Experience Report
Approval Tests in Action: A LEGO Exercise and an Experience Reporthouseofyin
 
Testing Sap: Modern Methodology
Testing Sap: Modern MethodologyTesting Sap: Modern Methodology
Testing Sap: Modern MethodologyEthan Jewett
 
Test Data Preparation: Tips and Tricks - SQA Days 22 - Saint Petersburg
Test Data Preparation: Tips and Tricks - SQA Days 22 - Saint PetersburgTest Data Preparation: Tips and Tricks - SQA Days 22 - Saint Petersburg
Test Data Preparation: Tips and Tricks - SQA Days 22 - Saint PetersburgSargis Sargsyan
 
Optimizing Your Agile Testing Processes
Optimizing Your Agile Testing ProcessesOptimizing Your Agile Testing Processes
Optimizing Your Agile Testing ProcessesStanton Champion
 
Dev labs alliance top 20 testng interview questions for sdet
Dev labs alliance top 20 testng interview questions for sdetDev labs alliance top 20 testng interview questions for sdet
Dev labs alliance top 20 testng interview questions for sdetdevlabsalliance
 
Developer Tests - Things to Know (Vilnius JUG)
Developer Tests - Things to Know (Vilnius JUG)Developer Tests - Things to Know (Vilnius JUG)
Developer Tests - Things to Know (Vilnius JUG)vilniusjug
 
Testing in AngularJS
Testing in AngularJSTesting in AngularJS
Testing in AngularJSPeter Drinnan
 

Similar to Getting Started TDD Guide Google Search Class (20)

From 0 to 100: How we jump-started our frontend testing
From 0 to 100: How we jump-started our frontend testingFrom 0 to 100: How we jump-started our frontend testing
From 0 to 100: How we jump-started our frontend testing
 
Hands On with Selenium and WebDriver
Hands On with Selenium and WebDriverHands On with Selenium and WebDriver
Hands On with Selenium and WebDriver
 
Writing useful automated tests for the single page applications you build
Writing useful automated tests for the single page applications you buildWriting useful automated tests for the single page applications you build
Writing useful automated tests for the single page applications you build
 
10 Principles of Apex Testing
10 Principles of Apex Testing10 Principles of Apex Testing
10 Principles of Apex Testing
 
Unit Testing and Behavior Driven Testing with AngularJS - Jesse Liberty | Fal...
Unit Testing and Behavior Driven Testing with AngularJS - Jesse Liberty | Fal...Unit Testing and Behavior Driven Testing with AngularJS - Jesse Liberty | Fal...
Unit Testing and Behavior Driven Testing with AngularJS - Jesse Liberty | Fal...
 
Selenium testing IDE 101
Selenium testing IDE 101Selenium testing IDE 101
Selenium testing IDE 101
 
10 Principles of Apex Testing
10 Principles of Apex Testing10 Principles of Apex Testing
10 Principles of Apex Testing
 
Unit Test in Ruby on Rails by Minitest
Unit Test in Ruby on Rails by MinitestUnit Test in Ruby on Rails by Minitest
Unit Test in Ruby on Rails by Minitest
 
Unit Test Your Database
Unit Test Your DatabaseUnit Test Your Database
Unit Test Your Database
 
Testacular
TestacularTestacular
Testacular
 
Approval Tests in Action: A LEGO Exercise and an Experience Report
Approval Tests in Action: A LEGO Exercise and an Experience ReportApproval Tests in Action: A LEGO Exercise and an Experience Report
Approval Tests in Action: A LEGO Exercise and an Experience Report
 
Testing Sap: Modern Methodology
Testing Sap: Modern MethodologyTesting Sap: Modern Methodology
Testing Sap: Modern Methodology
 
Test Data Preparation: Tips and Tricks - SQA Days 22 - Saint Petersburg
Test Data Preparation: Tips and Tricks - SQA Days 22 - Saint PetersburgTest Data Preparation: Tips and Tricks - SQA Days 22 - Saint Petersburg
Test Data Preparation: Tips and Tricks - SQA Days 22 - Saint Petersburg
 
Testing In Java
Testing In JavaTesting In Java
Testing In Java
 
Testing In Java4278
Testing In Java4278Testing In Java4278
Testing In Java4278
 
Optimizing Your Agile Testing Processes
Optimizing Your Agile Testing ProcessesOptimizing Your Agile Testing Processes
Optimizing Your Agile Testing Processes
 
Dev labs alliance top 20 testng interview questions for sdet
Dev labs alliance top 20 testng interview questions for sdetDev labs alliance top 20 testng interview questions for sdet
Dev labs alliance top 20 testng interview questions for sdet
 
Developer Tests - Things to Know (Vilnius JUG)
Developer Tests - Things to Know (Vilnius JUG)Developer Tests - Things to Know (Vilnius JUG)
Developer Tests - Things to Know (Vilnius JUG)
 
10 Principles of Apex Testing
10 Principles of Apex Testing10 Principles of Apex Testing
10 Principles of Apex Testing
 
Testing in AngularJS
Testing in AngularJSTesting in AngularJS
Testing in AngularJS
 

More from Viget Labs

Building a Brand as Consumers Take Control
Building a Brand as Consumers Take ControlBuilding a Brand as Consumers Take Control
Building a Brand as Consumers Take ControlViget Labs
 
Branded Utility By Josh Chambers
Branded Utility By Josh ChambersBranded Utility By Josh Chambers
Branded Utility By Josh ChambersViget Labs
 
Make Everyone a Tester: Natural Language Acceptance Testing
Make Everyone a Tester: Natural Language Acceptance TestingMake Everyone a Tester: Natural Language Acceptance Testing
Make Everyone a Tester: Natural Language Acceptance TestingViget Labs
 
Women In Technology
Women In TechnologyWomen In Technology
Women In TechnologyViget Labs
 
9 Tips to Profitability: How Squidoo Did It
9 Tips to Profitability: How Squidoo Did It9 Tips to Profitability: How Squidoo Did It
9 Tips to Profitability: How Squidoo Did ItViget Labs
 
Advanced RESTful Rails
Advanced RESTful RailsAdvanced RESTful Rails
Advanced RESTful RailsViget Labs
 
Cleanliness is Next to Domain-Specificity
Cleanliness is Next to Domain-SpecificityCleanliness is Next to Domain-Specificity
Cleanliness is Next to Domain-SpecificityViget Labs
 
Dealing With Legacy PHP Applications
Dealing With Legacy PHP ApplicationsDealing With Legacy PHP Applications
Dealing With Legacy PHP ApplicationsViget Labs
 
Mockfight! FlexMock vs. Mocha
Mockfight! FlexMock vs. MochaMockfight! FlexMock vs. Mocha
Mockfight! FlexMock vs. MochaViget Labs
 
Building and Working With Static Sites in Ruby on Rails
Building and Working With Static Sites in Ruby on RailsBuilding and Working With Static Sites in Ruby on Rails
Building and Working With Static Sites in Ruby on RailsViget Labs
 

More from Viget Labs (11)

Building a Brand as Consumers Take Control
Building a Brand as Consumers Take ControlBuilding a Brand as Consumers Take Control
Building a Brand as Consumers Take Control
 
Branded Utility By Josh Chambers
Branded Utility By Josh ChambersBranded Utility By Josh Chambers
Branded Utility By Josh Chambers
 
Make Everyone a Tester: Natural Language Acceptance Testing
Make Everyone a Tester: Natural Language Acceptance TestingMake Everyone a Tester: Natural Language Acceptance Testing
Make Everyone a Tester: Natural Language Acceptance Testing
 
Women In Technology
Women In TechnologyWomen In Technology
Women In Technology
 
9 Tips to Profitability: How Squidoo Did It
9 Tips to Profitability: How Squidoo Did It9 Tips to Profitability: How Squidoo Did It
9 Tips to Profitability: How Squidoo Did It
 
Advanced RESTful Rails
Advanced RESTful RailsAdvanced RESTful Rails
Advanced RESTful Rails
 
Hows Haml?
Hows Haml?Hows Haml?
Hows Haml?
 
Cleanliness is Next to Domain-Specificity
Cleanliness is Next to Domain-SpecificityCleanliness is Next to Domain-Specificity
Cleanliness is Next to Domain-Specificity
 
Dealing With Legacy PHP Applications
Dealing With Legacy PHP ApplicationsDealing With Legacy PHP Applications
Dealing With Legacy PHP Applications
 
Mockfight! FlexMock vs. Mocha
Mockfight! FlexMock vs. MochaMockfight! FlexMock vs. Mocha
Mockfight! FlexMock vs. Mocha
 
Building and Working With Static Sites in Ruby on Rails
Building and Working With Static Sites in Ruby on RailsBuilding and Working With Static Sites in Ruby on Rails
Building and Working With Static Sites in Ruby on Rails
 

Recently uploaded

CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DaySri Ambati
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024Stephanie Beckett
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 

Recently uploaded (20)

CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo DayH2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
H2O.ai CEO/Founder: Sri Ambati Keynote at Wells Fargo Day
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024What's New in Teams Calling, Meetings and Devices March 2024
What's New in Teams Calling, Meetings and Devices March 2024
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 

Getting Started TDD Guide Google Search Class

  • 1. Changing Your Mindset Getting Started With Test-Driven Development Patrick Reagan Director, Application Development patrick@viget.com
  • 2. Overview  Testing Process  Test::Unit Introduction  Code Example: GoogleRank  Testing Pitfalls  Coverage Analysis  Next Steps Changing Your Mindset Getting Started With Test-Driven Development September 7th, 2007
  • 3. Test-Driven Development is the process of testing the behavior of non-existent objects Changing Your Mindset Getting Started With Test-Driven Development September 7th, 2007
  • 4. Testing Cycle Write Failing Test Write Refactor Code Verify Success Changing Your Mindset Getting Started With Test-Driven Development September 7th, 2007
  • 5. Why Test First?  No untested code is written  Start with higher code coverage  Rapid feedback  Build your regressions as you go Changing Your Mindset Getting Started With Test-Driven Development September 7th, 2007
  • 6. Anatomy of a (Test::Unit) Test Case Test case Test setup (run every test) Test Assertion Changing Your Mindset Getting Started With Test-Driven Development September 7th, 2007
  • 7. Naming Conventions Method to test Parameters / Input Expected behavior (should ...) Changing Your Mindset Getting Started With Test-Driven Development September 7th, 2007
  • 8. Basic Assertions  assert  assert_equal / assert_not_equal  assert_nil / assert_not_nil  assert_match / assert_no_match  assert_raise / assert_nothing_raised Changing Your Mindset Getting Started With Test-Driven Development September 7th, 2007
  • 9. Code Example Class that retrieves Google results Retrieves only the top 10 URLs  URL encodes provided search terms  Makes an HTTP connection to Google  Raises an exception when unavailable  Changing Your Mindset Getting Started With Test-Driven Development September 7th, 2007
  • 10. Start Small test_google_rank_should_exist(GoogleRankTest): Exception raised: Class: <NameError> Message: <quot;uninitialized constant GoogleRankTest::GoogleRankquot;> 1 tests, 0 assertions, 1 failures, 0 errors 1 tests, 0 assertions, 0 failures, 0 errors Changing Your Mindset Getting Started With Test-Driven Development September 7th, 2007
  • 11. Simplest Thing That Works Class method test_parse_with_empty_document_should_return_empty_array(GoogleRankTest): NoMethodError: undefined method `parse' for GoogleRank:Class 1 tests, 0 assertions, 0 failures, 1 errors Instance method test_parse_with_empty_document_should_return_empty_array(GoogleRankTest): NoMethodError: undefined method `parse' for GoogleRank:Class 1 tests, 0 assertions, 0 failures, 1 errors Changing Your Mindset Getting Started With Test-Driven Development September 7th, 2007
  • 12. Simplest Thing That Actually Works 2 tests, 1 assertions, 0 failures, 0 errors Changing Your Mindset Getting Started With Test-Driven Development September 7th, 2007
  • 13. Use a Canned Data Set Sample markup from Google search test_parse_with_sample_document_should_return_list_of_urls <[quot;http://blog.viget.com/quot;, ..... > expected but was <[]>. 1 tests, 1 assertions, 1 failures, 0 errors Changing Your Mindset Getting Started With Test-Driven Development September 7th, 2007
  • 14. Use a Canned Data Set 1 tests, 1 assertions, 0 failures, 0 errors Changing Your Mindset Getting Started With Test-Driven Development September 7th, 2007
  • 15. Running Regressions test_parse_with_empty_document_should_return_empty_array(GoogleRankTest): NoMethodError: undefined method `[]' for nil:NilClass 3 tests, 1 assertions, 0 failures, 1 errors results.nil? == true Changing Your Mindset Getting Started With Test-Driven Development September 7th, 2007
  • 16. Fix Regressions 3 tests, 2 assertions, 0 failures, 0 errors Changing Your Mindset Getting Started With Test-Driven Development September 7th, 2007
  • 17. Code Example Class that retrieves Google results Retrieves only the top 10 URLs  URL encodes provided search terms  Makes an HTTP connection to Google  Raises an exception when unavailable  Changing Your Mindset Getting Started With Test-Driven Development September 7th, 2007
  • 18. Retrieve Internal State Inspect instance variable NameError: uninitialized constant GoogleRankTest::ERB Changing Your Mindset Getting Started With Test-Driven Development September 7th, 2007
  • 19. Retrieve Internal State test_new_should_encode_supplied_keywords(GoogleRankTest): ArgumentError: wrong number of arguments (1 for 0) 1 tests, 0 assertions, 0 failures, 1 errors Changing Your Mindset Getting Started With Test-Driven Development September 7th, 2007
  • 20. Retrieve Internal State 1 tests, 1 assertions, 0 failures, 0 errors 4 tests, 3 assertions, 0 failures, 0 errors Changing Your Mindset Getting Started With Test-Driven Development September 7th, 2007
  • 21. Refactor Test Duplication 4 tests, 3 assertions, 0 failures, 0 errors Changing Your Mindset Getting Started With Test-Driven Development September 7th, 2007
  • 22. Testing Encoding test_new_should_assign_url_with_base_and_encoded_keywords(GoogleRankTest): NameError: uninitialized constant GoogleRank::BASE_URL 1 tests, 0 assertions, 0 failures, 1 errors Changing Your Mindset Getting Started With Test-Driven Development September 7th, 2007
  • 23. Testing Encoding 1 tests, 1 assertions, 0 failures, 0 errors 5 tests, 4 assertions, 0 failures, 0 errors Changing Your Mindset Getting Started With Test-Driven Development September 7th, 2007
  • 24. Code Example Class that retrieves Google results Retrieves only the top 10 URLs  URL encodes provided search terms  Makes an HTTP connection to Google  Raises an exception when unavailable  Changing Your Mindset Getting Started With Test-Driven Development September 7th, 2007
  • 25. “Mock objects are simulated objects that mimic the behavior of real objects in controlled ways” - Wikipedia Changing Your Mindset Getting Started With Test-Driven Development September 7th, 2007
  • 26. Mocks and Stubs  Remove external dependencies  Create known state  Focus tests on specific code paths Changing Your Mindset Getting Started With Test-Driven Development September 7th, 2007
  • 27. Mocha to Stub Net::HTTP Implementation will use Net::HTTP#get test_retrieve_content_should_set_content(GoogleRankTest): NoMethodError: undefined method `retrieve_content' for #<GoogleRank:0x104053c> 1 tests, 0 assertions, 0 failures, 1 errors Changing Your Mindset Getting Started With Test-Driven Development September 7th, 2007
  • 28. Mocha to Stub Net::HTTP 6 tests, 5 assertions, 0 failures, 0 errors Changing Your Mindset Getting Started With Test-Driven Development September 7th, 2007
  • 29. No Network? Oops! $ irb --prompt simple >> gr = GoogleRank.new('rails') => #<GoogleRank:0x54c89c @encoded_keywords=quot;railsquot;, @url=quot;http://www.google.com/search?q=railsquot;> >> gr.retrieve_content SocketError: getaddrinfo: No address associated with nodename Changing Your Mindset Getting Started With Test-Driven Development September 7th, 2007
  • 30. Replicate Real-Word Conditions Expected failure test_retrieve_content_when_connection_fails_should_set_content_to_nil(GoogleRankTest): SocketError: SocketError 1 tests, 0 assertions, 0 failures, 1 errors Changing Your Mindset Getting Started With Test-Driven Development September 7th, 2007
  • 31. Dealing With Failure 7 tests, 6 assertions, 0 failures, 0 errors Changing Your Mindset Getting Started With Test-Driven Development September 7th, 2007
  • 32. Code Example Class that retrieves Google results Retrieves only the top 10 URLs  URL encodes provided search terms  Makes an HTTP connection to Google  Raises an exception when unavailable  Changing Your Mindset Getting Started With Test-Driven Development September 7th, 2007
  • 33. Dealing With Even More Failure test_retrieve_content_when_connection_fails_should_raise_exception_and_set_content_to_nil(GoogleRankTest) <GoogleRank::ConnectionError> exception expected but none was thrown. test_retrieve_content_with_failure_should_raise_exception(GoogleRankTest) <GoogleRank::ConnectionError> exception expected but was Class: <NoMethodError> Message: <quot;undefined method `closed?' for nil:NilClassquot;> 7 tests, 6 assertions, 2 failures, 0 errors Changing Your Mindset Getting Started With Test-Driven Development September 7th, 2007
  • 34. Handling Unsuccessful Requests 8 tests, 8 assertions, 0 failures, 0 errors Changing Your Mindset Getting Started With Test-Driven Development September 7th, 2007
  • 35. Testing Pitfalls Things to avoid Over-mocking  Invalid mocks  Testing library code  “Assertion-heavy” tests  Non-descriptive test names  Changing Your Mindset Getting Started With Test-Driven Development September 7th, 2007
  • 36. Testing Pitfalls Should test parsing HTTPResponse#content does not exist Changing Your Mindset Getting Started With Test-Driven Development September 7th, 2007
  • 37. Testing Pitfalls Use secondary test to verify Expected behavior? Changing Your Mindset Getting Started With Test-Driven Development September 7th, 2007
  • 38. Coverage Analysis  Shows ‘tested’ code  Indication of when to stop  Can present a false impression Changing Your Mindset Getting Started With Test-Driven Development September 7th, 2007
  • 39. Coverage With RCov Exception-prone Changing Your Mindset Getting Started With Test-Driven Development September 7th, 2007
  • 40. Next Steps?  Rake - Rake::TestTask  Mock expectations as tests  Testing Macros Changing Your Mindset Getting Started With Test-Driven Development September 7th, 2007
  • 41. Rake::TestTask Example $ rake -T rake test:all # Run tests for all Changing Your Mindset Getting Started With Test-Driven Development September 7th, 2007
  • 42. Expectations as Tests attr_accessor :content called once Changing Your Mindset Getting Started With Test-Driven Development September 7th, 2007
  • 43. Testing Macros Changing Your Mindset Getting Started With Test-Driven Development September 7th, 2007
  • 44. Resources Test::Unit http://ruby-doc.org/stdlib  Mocha http://mocha.rubyforge.org  RCov http://eigenclass.org/hiki/rcov  Rake http://rake.rubyforge.org  How to Test Validations (Expectations as Tests) http://relevancellc.com/2007/7/16/how-to-test-validations-part-4  Testing Macros http://www.extendviget.com  Changing Your Mindset Getting Started With Test-Driven Development September 7th, 2007
  • 45. Slides http://www.slideshare.net/viget Blog http://blog.viget.com http://www.sneaq.net Contact patrick@viget.com Changing Your Mindset Getting Started With Test-Driven Development September 7th, 2007