SlideShare a Scribd company logo
An Introduction To Software
Development Using Python
Spring Semester, 2014
Class #13:
Test Driven
Development, Part 1
3 Different Types Of Testing
• Unit Testing
– Performed by developers
– Goal is to ensure that their code works correctly
• System Testing
– Performed by professional testers
– Goal is to ensure that the parts work together
• User Testing
– Performed by professional testers
– Goal is to ensure that the expected functions work
Image Credit www.fotosearch.com
Python Has Two Testing Tools
unittest
• Automated testing framework
• Python’s unittest module, sometimes
referred to as PyUnit, is based on the
XUnit framework design by Kent Beck
and Erich Gamma.
• The same pattern is repeated in many
other languages, including C, perl,
Java, and Smalltalk.
• The framework implemented by
unittest supports fixtures, test suites,
and a test runner to enable
automated testing for your code.
Py.test
• A mature full-featured Python testing
tool
• Provides easy no-boilerplate testing
• Scales from simple unit to complex
functional testing
• Integrates with other testing
methods and tools:
• Extensive plugin and customization
system:
What Would A Py.Test Script Look
Like For Homework #1?
#
# Encrypt the social security number
encryptedSS = ""
encryptedSS += socialSecurityNum[10]
encryptedSS += socialSecurityNum[9]
encryptedSS += socialSecurityNum[8]
encryptedSS += socialSecurityNum[7]
encryptedSS += socialSecurityNum[6]
encryptedSS += socialSecurityNum[5]
encryptedSS += socialSecurityNum[4]
encryptedSS += socialSecurityNum[3]
encryptedSS += socialSecurityNum[2]
encryptedSS += socialSecurityNum[1]
encryptedSS += socialSecurityNum[0]
def test_encrypted_SS ():
assert encryptedSS == ‘6270-55-461’
Image Credit www.objective.no
What Is Continuous Integration?
• Version control keeps track of our code
• Now we have automated tests
• We need to tie these two things together
• Continuous Integration tools do the following:
– Compile code
– Run the automated tests
– Display and mail out reports when new code is committed
to the repository
Image Credit www.guruintechnocrats.com
Continuous Integration In Action
Bob
1. Bob checks code into
the server
2. Version control code
notifies continuous
integration tool that there
is new code
3. Continuous integration tool
checks out the new code, compiles
it and runs all of your tests on it.
Most build tools also create a
web page and email the team to
let them know how things are
going.
How Many Tests Do You Need?
• Trade off: how much of the code that you test vs. the
chances of finding a bug in the part that you haven’t
tested.
• Testing the same code several different ways won’t
do you much good.
• Don’t focus on the number of tests you have, instead
focus on the coverage of your tests: % of code tested.
• Most projects aim for 85% - 90% coverage
Image Credit www.clker.com
What If…?
• Good software needs to work.
• How do you know that your software works?
• Even with unit testing, there is the possibility that a portion of
your code is untested.
• What if testing was a fundamental part of your software
development process?
• What if everything was done with testing in mind?
• Version Control + Continuous Integration + Automated Testing
New Idea: Test First, Not Last!
• Don’t try to go back and work testing into a
completed project.
• Instead, build support for testing into the project
from the start.
• New idea: Test Driven Design (TDD) – create code
with testing in mind from the start.
Pay with
Visa
Pay with
MC
Pay with
Paypal
3
3
5
Pay with Visa / MC / Paypal
Test First…
• The “Pay With Visa / Mastercard / PayPal” user story
is going to be broken into tasks.
• If we are going to test first, then we need to look at
our first task
• If we jump right into creating code, then we’ll be
right back where we’ve been doing testing last.
Pay with
Visa
Pay with
MC
Pay with
Paypal
3
3
5
Pay with Visa / MC / Paypal
Analyze The Task
• First we have to break this task down. For this task
we’ll have to:
– Represent the Order Information: We’ll have to capture
the customer’s name, what they are ordering, and the cost
– Represent the Credit Card Information: We’ll need the
credit card info, and their secret card number.
– Represent Receipt Information: We’ll have to capture the
confirmation number, the final cost, as well as the date of
the transaction.
Pay with
Visa
3
Create The Test First!
• Write a test case first!
• Start with the order information part of the
task.
• Use your test framework to create a test for
the “Pay with Visa” functionality.
Welcome To TDD!
• When you are creating test cases before you write code and
then letting those test cases drive how you create your code,
you are using Test Driven Development (TDD).
• TDD is a formal term that is used to describe the process of
testing from the outset of development.
• This means that you write every line of code specifically as a
response to your tests.
How To Write A Test Case
• Your first step needs to be to determine just
exactly what needs to be tested.
• Since this is fine grained testing, testing at the
unit testing level, you should start with a small
test.
• Determine what the smallest test that you
could write would be that uses the order
information that you’ll be storing as a part of
the first task?
Test Case Creation Secrets
• You have no code! You are writing your tests
first.
• There is no way that this test should pass the
first time that you run it.
• The test probably won’t even compile.
However, that’s ok…
• Remember, at first your test case…fails
miserably.
TDD Rule #1
• TDD Rule #1: Your test should always fail
before you implement any code.
• You want your tests to fail when you first write
them.
• The point of the test is to establish a
measurable success.
• Because your test is failing, now it’s clear what
you have to do to make sure that test passes.
Your Next Step…
• Write the simplest code just to get this test to
pass.
• This is called “… getting your tests to green.”
• Green refers to a green bar that many
automated test case runners display when all
tests pass. If any test fails, a red bar is
displayed.
• TDD Rule #2: Implement the simplest code
possible to make your test cases pass.
The YAGNI Principal
• Test-Driven Development is about doing the simplest thing
that you can do in order to get your test case to pass.
• Do not add anything that you MIGHT NEED in the future.
• If you do need something in the future, you’ll write a test case
for it and then you’ll write code to pass that test case.
• Focusing on small bits of code is the key to test-driven
development.
• YAGNI: “Ya ain’t going to need it”
3 Steps To Test Driven Development
• Red: Your Test Fails
– Write a test to check whatever functionality you are going to write.
– It will fail because you have not yet implemented that functionality.
– This is the red stage because your testing GUI will show the test in red (failing).
• Green: Your Test Passes
– Implement the functionality to get that test to pass
– Write the simplest code possible to get the test to pass.
– This is the green stage.
• Refactor: Clean up – duplication, ugly code, old code, etc.
– After your test passes, go back in and clean up things that you noticed while
implementing your code.
– This is the refactor stage.
– Next, go on to create the next test.
Pay with
Visa
3
Pay with Visa / MC / Paypal
Exercise: Pay With Visa
• Represent the Order Information: We’ll have to capture the
customer’s name, what they are ordering, and the cost
– What tests can we create for the order
information task?
– What will be the minimal amount of code that we
can implement to pass these tests?
Image Credit: presstigeprinting.com
In TDD, Tests Drive Your
Implementation
• TDD drives your implementation all the way
through development.
• By writing tests before code, you have to focus
on the functionality right off the bat.
• What is the code that you are creating
supposed to do?
Good TDD Habits
• Each test should verify only one thing
– Make each test only test one thing
• Avoid duplicate test code
– Just like you avoid duplicate code, avoid duplicate tests.
• Keep your tests in a mirror directory of your source code
– You will be creating a lot of test cases
– Keep your tests in a separate subdirectory on the same level as your
source code and with the same directory structure.
– This will make life easier for your build scripts
Completing A Task
• You’ve got all the tests that you need and they
all pass.
• When your tests pass, move on!
• Different task, use the same process…
What We Covered Today
1. Test first, not last!
2. Test-Driven Development
3. TDD Rule #1: Your test
should always fail before
you implement any code.
4. TDD Rule #2: Implement
the simplest code possible
to make your test cases
pass.
Image Credit: http://www.tswdj.com/blog/2011/05/17/the-grooms-checklist/
What We’ll Be Covering Next Time
1. Test driven
development, Part 2
Image Credit: http://merchantblog.thefind.com/2011/01/merchant-newsletter/resolve-to-take-advantage-of-these-5-e-commerce-trends/attachment/crystal-ball-fullsize/

More Related Content

What's hot (17)

PDF
Code Review
Tu Hoang
 
PDF
Agile Mumbai 2020 Conference | How to get the best ROI on Your Test Automati...
AgileNetwork
 
PDF
TDD — Are you sure you properly test code?
Dmitriy Nesteryuk
 
PPTX
Test scenarios for sending & receiving emails
Morpheous Algan
 
PPTX
Code Review
R M Shahidul Islam Shahed
 
PDF
Code Review
Divante
 
ODP
Documenting Code - Patterns and Anti-patterns - NLPW 2016
Søren Lund
 
ODP
Beyond Unit Testing
Søren Lund
 
PDF
Code Review: How and When
Paul Gower
 
PPT
Code Review
rantav
 
ODP
Documenting code yapceu2016
Søren Lund
 
PDF
Adopting tdd in the workplace
Donny Wals
 
PPT
Code review
dqpi
 
PDF
ABAP Code Retreat Frankfurt 2016: TDD - Test Driven Development
Hendrik Neumann
 
DOCX
Code review guidelines
Lalit Kale
 
PPT
Code Review
Ravi Raj
 
PPTX
Lessons Learned in Test Automation From Zombieland
Matt Barbour
 
Code Review
Tu Hoang
 
Agile Mumbai 2020 Conference | How to get the best ROI on Your Test Automati...
AgileNetwork
 
TDD — Are you sure you properly test code?
Dmitriy Nesteryuk
 
Test scenarios for sending & receiving emails
Morpheous Algan
 
Code Review
Divante
 
Documenting Code - Patterns and Anti-patterns - NLPW 2016
Søren Lund
 
Beyond Unit Testing
Søren Lund
 
Code Review: How and When
Paul Gower
 
Code Review
rantav
 
Documenting code yapceu2016
Søren Lund
 
Adopting tdd in the workplace
Donny Wals
 
Code review
dqpi
 
ABAP Code Retreat Frankfurt 2016: TDD - Test Driven Development
Hendrik Neumann
 
Code review guidelines
Lalit Kale
 
Code Review
Ravi Raj
 
Lessons Learned in Test Automation From Zombieland
Matt Barbour
 

Viewers also liked (20)

PPT
Test Driven Development
nikhil sreeni
 
PPTX
Software architecture...Yes, on tests!
Codemotion
 
PDF
London SDET Meetup (April 2016) - M&S Digital Test Journey
Richard Chernanko
 
DOCX
Pratap Kumar Nallamothu SDET2
pratap kumar
 
PPTX
Testing without Testers
Alan Page
 
PDF
Appium & Robot Framework
Furkan Ertürk
 
PPTX
Agile Software Development and Test Driven Development: Agil8's Dave Putman 3...
agil8 Ltd
 
PDF
JavaOne 2016 :: Bringing Robot online with Robo4j Framework
Miro Wengner
 
PPTX
Belajar Postman test runner
Fachrul Choliluddin
 
PDF
Rf meetup 16.3.2017 tampere share
Mika Tavi
 
PDF
Robot framework
Prayoch Rujira
 
PDF
The Engines of Software Development: Testing and Test Driven Development
Lemi Orhan Ergin
 
PPTX
Robot Framework
Onur Baskirt
 
ODP
The automated tests inside Openshift
Oleg Popov
 
PDF
ATDD Using Robot Framework
Pekka Klärck
 
PDF
JavaCro'14 - Test Automation using RobotFramework Libraries – Stojan Peshov
HUJAK - Hrvatska udruga Java korisnika / Croatian Java User Association
 
PDF
Functional Tests Automation with Robot Framework
laurent bristiel
 
ZIP
Robot Framework Introduction
laurent bristiel
 
PPTX
Fit for Work – key questions from employers
Fit for Work
 
PPTX
ATAGTR2017 Expanding test horizons with Robot Framework
Agile Testing Alliance
 
Test Driven Development
nikhil sreeni
 
Software architecture...Yes, on tests!
Codemotion
 
London SDET Meetup (April 2016) - M&S Digital Test Journey
Richard Chernanko
 
Pratap Kumar Nallamothu SDET2
pratap kumar
 
Testing without Testers
Alan Page
 
Appium & Robot Framework
Furkan Ertürk
 
Agile Software Development and Test Driven Development: Agil8's Dave Putman 3...
agil8 Ltd
 
JavaOne 2016 :: Bringing Robot online with Robo4j Framework
Miro Wengner
 
Belajar Postman test runner
Fachrul Choliluddin
 
Rf meetup 16.3.2017 tampere share
Mika Tavi
 
Robot framework
Prayoch Rujira
 
The Engines of Software Development: Testing and Test Driven Development
Lemi Orhan Ergin
 
Robot Framework
Onur Baskirt
 
The automated tests inside Openshift
Oleg Popov
 
ATDD Using Robot Framework
Pekka Klärck
 
JavaCro'14 - Test Automation using RobotFramework Libraries – Stojan Peshov
HUJAK - Hrvatska udruga Java korisnika / Croatian Java User Association
 
Functional Tests Automation with Robot Framework
laurent bristiel
 
Robot Framework Introduction
laurent bristiel
 
Fit for Work – key questions from employers
Fit for Work
 
ATAGTR2017 Expanding test horizons with Robot Framework
Agile Testing Alliance
 
Ad

Similar to An Introduction To Software Development - Test Driven Development, Part 1 (20)

PPTX
TDD in Agile
Atish Narlawar
 
PDF
Test-Driven Developments are Inefficient; Behavior-Driven Developments are a ...
Abdelkrim Boujraf
 
PPTX
{10.0} Test Driven Development.pptx
AmalEldhose2
 
PPT
Test Driven Development – What Works And What Doesn’t
Synerzip
 
PDF
Test Driven Development (TDD)
David Ehringer
 
PDF
Test driven development : software process
Amin Taheri
 
PPTX
Software presentation
JennaPrengle
 
ODP
Effective TDD - Less is more
Ben Lau
 
PDF
What CS Class Didn't Teach About Testing
Camille Bell
 
PPTX
Test-Driven Development
Meilan Ou
 
PPT
Automated testing overview
Alex Pop
 
PDF
Driven to Tests
Kevlin Henney
 
PPTX
TeDevelopment Testing in Software Engineering
Karthik Rohan
 
PPTX
Bootstrapping Quality
Michael Roufa
 
PDF
Testing in Agile Development
Hariprakash Agrawal
 
PPTX
Beginners overview of automated testing with Rspec
jeffrey1ross
 
PDF
Test Driven Development
ZendCon
 
PPT
UNIT IV.ppt
Varshini62
 
TDD in Agile
Atish Narlawar
 
Test-Driven Developments are Inefficient; Behavior-Driven Developments are a ...
Abdelkrim Boujraf
 
{10.0} Test Driven Development.pptx
AmalEldhose2
 
Test Driven Development – What Works And What Doesn’t
Synerzip
 
Test Driven Development (TDD)
David Ehringer
 
Test driven development : software process
Amin Taheri
 
Software presentation
JennaPrengle
 
Effective TDD - Less is more
Ben Lau
 
What CS Class Didn't Teach About Testing
Camille Bell
 
Test-Driven Development
Meilan Ou
 
Automated testing overview
Alex Pop
 
Driven to Tests
Kevlin Henney
 
TeDevelopment Testing in Software Engineering
Karthik Rohan
 
Bootstrapping Quality
Michael Roufa
 
Testing in Agile Development
Hariprakash Agrawal
 
Beginners overview of automated testing with Rspec
jeffrey1ross
 
Test Driven Development
ZendCon
 
UNIT IV.ppt
Varshini62
 
Ad

Recently uploaded (20)

PPTX
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
PDF
AI-Powered-Visual-Storytelling-for-Nonprofits.pdf
TechSoup
 
PDF
Vani - The Voice of Excellence - Jul 2025 issue
Savipriya Raghavendra
 
PPTX
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PDF
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
PDF
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
PPTX
Introduction to Indian Writing in English
Trushali Dodiya
 
PDF
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
PPTX
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
PPTX
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
PPTX
DIGITAL CITIZENSHIP TOPIC TLE 8 MATATAG CURRICULUM
ROBERTAUGUSTINEFRANC
 
PPTX
Controller Request and Response in Odoo18
Celine George
 
PDF
Council of Chalcedon Re-Examined
Smiling Lungs
 
PDF
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
PPTX
infertility, types,causes, impact, and management
Ritu480198
 
PPTX
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
PDF
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
PPTX
How to Manage Allocation Report for Manufacturing Orders in Odoo 18
Celine George
 
PDF
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
AI-Powered-Visual-Storytelling-for-Nonprofits.pdf
TechSoup
 
Vani - The Voice of Excellence - Jul 2025 issue
Savipriya Raghavendra
 
Nitrogen rule, ring rule, mc lafferty.pptx
nbisen2001
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
Mahidol_Change_Agent_Note_2025-06-27-29_MUSEF
Tassanee Lerksuthirat
 
Women's Health: Essential Tips for Every Stage.pdf
Iftikhar Ahmed
 
Introduction to Indian Writing in English
Trushali Dodiya
 
The History of Phone Numbers in Stoke Newington by Billy Thomas
History of Stoke Newington
 
EDUCATIONAL MEDIA/ TEACHING AUDIO VISUAL AIDS
Sonali Gupta
 
grade 5 lesson matatag ENGLISH 5_Q1_PPT_WEEK4.pptx
SireQuinn
 
DIGITAL CITIZENSHIP TOPIC TLE 8 MATATAG CURRICULUM
ROBERTAUGUSTINEFRANC
 
Controller Request and Response in Odoo18
Celine George
 
Council of Chalcedon Re-Examined
Smiling Lungs
 
The Constitution Review Committee (CRC) has released an updated schedule for ...
nservice241
 
infertility, types,causes, impact, and management
Ritu480198
 
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
Android Programming - Basics of Mobile App, App tools and Android Basics
Kavitha P.V
 
How to Manage Allocation Report for Manufacturing Orders in Odoo 18
Celine George
 
Biological Bilingual Glossary Hindi and English Medium
World of Wisdom
 

An Introduction To Software Development - Test Driven Development, Part 1

  • 1. An Introduction To Software Development Using Python Spring Semester, 2014 Class #13: Test Driven Development, Part 1
  • 2. 3 Different Types Of Testing • Unit Testing – Performed by developers – Goal is to ensure that their code works correctly • System Testing – Performed by professional testers – Goal is to ensure that the parts work together • User Testing – Performed by professional testers – Goal is to ensure that the expected functions work Image Credit www.fotosearch.com
  • 3. Python Has Two Testing Tools unittest • Automated testing framework • Python’s unittest module, sometimes referred to as PyUnit, is based on the XUnit framework design by Kent Beck and Erich Gamma. • The same pattern is repeated in many other languages, including C, perl, Java, and Smalltalk. • The framework implemented by unittest supports fixtures, test suites, and a test runner to enable automated testing for your code. Py.test • A mature full-featured Python testing tool • Provides easy no-boilerplate testing • Scales from simple unit to complex functional testing • Integrates with other testing methods and tools: • Extensive plugin and customization system:
  • 4. What Would A Py.Test Script Look Like For Homework #1? # # Encrypt the social security number encryptedSS = "" encryptedSS += socialSecurityNum[10] encryptedSS += socialSecurityNum[9] encryptedSS += socialSecurityNum[8] encryptedSS += socialSecurityNum[7] encryptedSS += socialSecurityNum[6] encryptedSS += socialSecurityNum[5] encryptedSS += socialSecurityNum[4] encryptedSS += socialSecurityNum[3] encryptedSS += socialSecurityNum[2] encryptedSS += socialSecurityNum[1] encryptedSS += socialSecurityNum[0] def test_encrypted_SS (): assert encryptedSS == ‘6270-55-461’ Image Credit www.objective.no
  • 5. What Is Continuous Integration? • Version control keeps track of our code • Now we have automated tests • We need to tie these two things together • Continuous Integration tools do the following: – Compile code – Run the automated tests – Display and mail out reports when new code is committed to the repository Image Credit www.guruintechnocrats.com
  • 6. Continuous Integration In Action Bob 1. Bob checks code into the server 2. Version control code notifies continuous integration tool that there is new code 3. Continuous integration tool checks out the new code, compiles it and runs all of your tests on it. Most build tools also create a web page and email the team to let them know how things are going.
  • 7. How Many Tests Do You Need? • Trade off: how much of the code that you test vs. the chances of finding a bug in the part that you haven’t tested. • Testing the same code several different ways won’t do you much good. • Don’t focus on the number of tests you have, instead focus on the coverage of your tests: % of code tested. • Most projects aim for 85% - 90% coverage Image Credit www.clker.com
  • 8. What If…? • Good software needs to work. • How do you know that your software works? • Even with unit testing, there is the possibility that a portion of your code is untested. • What if testing was a fundamental part of your software development process? • What if everything was done with testing in mind? • Version Control + Continuous Integration + Automated Testing
  • 9. New Idea: Test First, Not Last! • Don’t try to go back and work testing into a completed project. • Instead, build support for testing into the project from the start. • New idea: Test Driven Design (TDD) – create code with testing in mind from the start. Pay with Visa Pay with MC Pay with Paypal 3 3 5 Pay with Visa / MC / Paypal
  • 10. Test First… • The “Pay With Visa / Mastercard / PayPal” user story is going to be broken into tasks. • If we are going to test first, then we need to look at our first task • If we jump right into creating code, then we’ll be right back where we’ve been doing testing last. Pay with Visa Pay with MC Pay with Paypal 3 3 5 Pay with Visa / MC / Paypal
  • 11. Analyze The Task • First we have to break this task down. For this task we’ll have to: – Represent the Order Information: We’ll have to capture the customer’s name, what they are ordering, and the cost – Represent the Credit Card Information: We’ll need the credit card info, and their secret card number. – Represent Receipt Information: We’ll have to capture the confirmation number, the final cost, as well as the date of the transaction. Pay with Visa 3
  • 12. Create The Test First! • Write a test case first! • Start with the order information part of the task. • Use your test framework to create a test for the “Pay with Visa” functionality.
  • 13. Welcome To TDD! • When you are creating test cases before you write code and then letting those test cases drive how you create your code, you are using Test Driven Development (TDD). • TDD is a formal term that is used to describe the process of testing from the outset of development. • This means that you write every line of code specifically as a response to your tests.
  • 14. How To Write A Test Case • Your first step needs to be to determine just exactly what needs to be tested. • Since this is fine grained testing, testing at the unit testing level, you should start with a small test. • Determine what the smallest test that you could write would be that uses the order information that you’ll be storing as a part of the first task?
  • 15. Test Case Creation Secrets • You have no code! You are writing your tests first. • There is no way that this test should pass the first time that you run it. • The test probably won’t even compile. However, that’s ok… • Remember, at first your test case…fails miserably.
  • 16. TDD Rule #1 • TDD Rule #1: Your test should always fail before you implement any code. • You want your tests to fail when you first write them. • The point of the test is to establish a measurable success. • Because your test is failing, now it’s clear what you have to do to make sure that test passes.
  • 17. Your Next Step… • Write the simplest code just to get this test to pass. • This is called “… getting your tests to green.” • Green refers to a green bar that many automated test case runners display when all tests pass. If any test fails, a red bar is displayed. • TDD Rule #2: Implement the simplest code possible to make your test cases pass.
  • 18. The YAGNI Principal • Test-Driven Development is about doing the simplest thing that you can do in order to get your test case to pass. • Do not add anything that you MIGHT NEED in the future. • If you do need something in the future, you’ll write a test case for it and then you’ll write code to pass that test case. • Focusing on small bits of code is the key to test-driven development. • YAGNI: “Ya ain’t going to need it”
  • 19. 3 Steps To Test Driven Development • Red: Your Test Fails – Write a test to check whatever functionality you are going to write. – It will fail because you have not yet implemented that functionality. – This is the red stage because your testing GUI will show the test in red (failing). • Green: Your Test Passes – Implement the functionality to get that test to pass – Write the simplest code possible to get the test to pass. – This is the green stage. • Refactor: Clean up – duplication, ugly code, old code, etc. – After your test passes, go back in and clean up things that you noticed while implementing your code. – This is the refactor stage. – Next, go on to create the next test. Pay with Visa 3 Pay with Visa / MC / Paypal
  • 20. Exercise: Pay With Visa • Represent the Order Information: We’ll have to capture the customer’s name, what they are ordering, and the cost – What tests can we create for the order information task? – What will be the minimal amount of code that we can implement to pass these tests? Image Credit: presstigeprinting.com
  • 21. In TDD, Tests Drive Your Implementation • TDD drives your implementation all the way through development. • By writing tests before code, you have to focus on the functionality right off the bat. • What is the code that you are creating supposed to do?
  • 22. Good TDD Habits • Each test should verify only one thing – Make each test only test one thing • Avoid duplicate test code – Just like you avoid duplicate code, avoid duplicate tests. • Keep your tests in a mirror directory of your source code – You will be creating a lot of test cases – Keep your tests in a separate subdirectory on the same level as your source code and with the same directory structure. – This will make life easier for your build scripts
  • 23. Completing A Task • You’ve got all the tests that you need and they all pass. • When your tests pass, move on! • Different task, use the same process…
  • 24. What We Covered Today 1. Test first, not last! 2. Test-Driven Development 3. TDD Rule #1: Your test should always fail before you implement any code. 4. TDD Rule #2: Implement the simplest code possible to make your test cases pass. Image Credit: http://www.tswdj.com/blog/2011/05/17/the-grooms-checklist/
  • 25. What We’ll Be Covering Next Time 1. Test driven development, Part 2 Image Credit: http://merchantblog.thefind.com/2011/01/merchant-newsletter/resolve-to-take-advantage-of-these-5-e-commerce-trends/attachment/crystal-ball-fullsize/

Editor's Notes

  • #2: New name for the class I know what this means Technical professionals are who get hired This means much more than just having a narrow vertical knowledge of some subject area. It means that you know how to produce an outcome that I value. I’m willing to pay you to do that.