SlideShare a Scribd company logo
An Introduction To Software
Development Using Python
Spring Semester, 2014
Class #16:
Test Driven
Development
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.
Image Credit: www.canstockphoto.com
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.
Image Credit: revistanautilus.ro
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?
Image Credit: antoni.w-w.pl
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.
Image Credit: www.clipartpanda.com
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.
Image Credit: www.fs.usda.gov
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.
Image Credit: www.clipartpanda.com
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”
Image Credit: www.fotosearch.com
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
Image Credit: cliparts.co
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?
Image Credit: www.clipartpanda.com
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
Image Credit: www.dreamstime.com
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…
Image Credit: cliparts.co
Dependencies Are A Bad Thing
When It Comes To Testing
• Simple code means that you can break things up
and test parts of your code one thing at a time.
• Dependencies: code that depends on something
that is external to your code (like a database or an
interface)
• Dependencies make it hard to test one thing at a
time
• Your job is to figure out a way to test your code
independent of the dependencies
Image Credit: blogs.msdn.com
First You Have To Find Your
DependenciesPay with
Visa
3
– Represent the Order Information:
• Get card owner’s name
• Get card owner’s address
– Represent the Credit Card
Information:
• Get credit card number
• Get secret confirmation number
– Represent Receipt Information:
• Provide confirmation number
• The final cost
• Date of the transaction.
Name
Address
CC #
Secret #
CC #
Secret #
Conf #
Cost
Date
Conf #
Cost
Date
Your
Program
Your
Program
Your
Program
Credit
Card
Paper
Need To Create Testing Interfaces
Name
Address
CC #
Secret #
Dummy
Input
Conf #
Cost
Date
Dummy
Output
Your
Program
Your
Program
Your
Program
• Need to hide how our application gets and
gives its data
• Change how this works depending on if we
are conducting testing or running in
production
• We want to have two different ways of
passing the information that we want
• Our program does not have to know which
one it is using
Dummy
Input
Code
Stub
Code
Stub
Image Credit: www.clker.com
What Would This Look Like
In Python?
#
# Constants used in this program
runningTest = 1 # 0 = in production, 1 = testing being done
#
# Collect personal information from credit card owner
if (runningTest == 0) :
lastName = input("Please enter last name: ")
firstName = input("Please enter first name: ")
middleInitial = input("Please enter the middle initial: ")
streetAddress = input("Please enter street address: ")
city = input("Please enter city: ")
state = input("Please enter state: ")
zip = input("Please enter zip: ")
else:
lastName = "Johnson"
firstName = "Fred“
middleInitial = "N"
streetAddress = “2763 Filibuster Drive”
city = "Lakeland"
state = “FL”
zip = "37643"
Image Credit: 4vector.com
What Does Testing First Buy Us?
• Testing produces better code
• Create well-organized code
– Simpler code
– Nothing that is not absolutely required
• Code that always does the same thing
– Production code does the same thing as testing code
– We are writing production code all the time
• Code is loosely coupled
– Tightly coupled code is brittle and breaks easily
– Our code will be broken into a loosely coupled flexible system
Image Credit: www.fotosearch.com
What Kind Of Tests Do We Have To
Write Now?
Name
Address
CC #
Secret #
CC #
Secret #
Conf #
Cost
Date
Conf #
Cost
Date
Your
Program
Your
Program
Your
Program
Credit
Card
Paper
Name too long
Name too short
Illegal name characters
Street too long
Street too short
Illegal street characters
No name
No street
No CC #
CC # too long
CC # too short
No secret #
Secret # too long
Secret # too short
Wrong secret #
Valid name
Valid street
Valid CC #
Valid secret #
Valid conf #
Valid cost
Valid date
22
Tests
Things NOT To Do When
Writing Code
Testing Nothing:
You can write a lot of test code that doesn’t really
test anything. Could place a credit card order and
then not test the cost printed on the final receipt.
Don’t Test Your Data:
It can be all too easy to create test cases that test
the fake data that you’ve created and not the code
that you’ve written.
Get Rid Of Scraps:
The system state that your testing starts in is very
important. You need to always clean up and start
in the same state each time.
Image Credit: alexrister1.wordpress.com
So Just Exactly What Have We
Learned About Testing?
1. Always write the test before you write the code.
2. Make the test fail first, then write the
simplest code that will make the test pass
3. Each test should only test one thing
4. Once your tests pass, you can then clean
up your code (refactor)
5. When you are out of tests to write, you’re done!
Image Credit: clipart-finder.com
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. Ending an iteration
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 (12)

PPTX
The Art of Gherkin Scripting - Matt Eakin
QA or the Highway
 
PDF
Best Practices in Software Development
André Pitombeira
 
PDF
Lessons learned with Bdd: a tutorial
Alan Richardson
 
PPTX
BDD Primer
Attila Bertók
 
PDF
Hey You Got Your TDD in my SQL DB by Jeff McKenzie
QA or the Highway
 
PDF
Growing Manual Testers into Automators
Camille Bell
 
PPTX
Clean Code
swaraj Patil
 
PDF
Inside Requirements
Kevlin Henney
 
PDF
Prefer Code to Comments
Kevlin Henney
 
PPTX
Clean code coding like a professional
Nhật Nguyễn Khắc
 
PDF
Sorted
Kevlin Henney
 
PDF
Adopting technical practices 2013
Steven Mak
 
The Art of Gherkin Scripting - Matt Eakin
QA or the Highway
 
Best Practices in Software Development
André Pitombeira
 
Lessons learned with Bdd: a tutorial
Alan Richardson
 
BDD Primer
Attila Bertók
 
Hey You Got Your TDD in my SQL DB by Jeff McKenzie
QA or the Highway
 
Growing Manual Testers into Automators
Camille Bell
 
Clean Code
swaraj Patil
 
Inside Requirements
Kevlin Henney
 
Prefer Code to Comments
Kevlin Henney
 
Clean code coding like a professional
Nhật Nguyễn Khắc
 
Adopting technical practices 2013
Steven Mak
 

Viewers also liked (13)

PPTX
An Introduction To Python - Modules & Solving Real World Problems
Blue Elephant Consulting
 
PPTX
An Introduction To Python - Tables, List Algorithms
Blue Elephant Consulting
 
PPTX
Intro To C++ - Class 03 - An Introduction To C++ Programming, Part II
Blue Elephant Consulting
 
PPTX
Intro To C++ - Class 10 - Control Statements: Part 2
Blue Elephant Consulting
 
PPTX
An Introduction To Python - Understanding Computers
Blue Elephant Consulting
 
PPTX
An Introduction To Python - Python, Print()
Blue Elephant Consulting
 
PPTX
Intro To C++ - Class 14 - Midterm Review
Blue Elephant Consulting
 
PPTX
Cloud Computing: How to Change The World In 1,825 Days…
Blue Elephant Consulting
 
PPTX
An Introduction To Software Development - Design Strategies
Blue Elephant Consulting
 
PPTX
An Introduction To Software Development - Implementation
Blue Elephant Consulting
 
PPTX
An Introduction To Python - Working With Data
Blue Elephant Consulting
 
PPTX
Intro To C++ - Class 11 - Converting between types, formatting floating point...
Blue Elephant Consulting
 
PPTX
Using A Balanced Scorecard For An IT Department -- The Secret To Knowing Wher...
Blue Elephant Consulting
 
An Introduction To Python - Modules & Solving Real World Problems
Blue Elephant Consulting
 
An Introduction To Python - Tables, List Algorithms
Blue Elephant Consulting
 
Intro To C++ - Class 03 - An Introduction To C++ Programming, Part II
Blue Elephant Consulting
 
Intro To C++ - Class 10 - Control Statements: Part 2
Blue Elephant Consulting
 
An Introduction To Python - Understanding Computers
Blue Elephant Consulting
 
An Introduction To Python - Python, Print()
Blue Elephant Consulting
 
Intro To C++ - Class 14 - Midterm Review
Blue Elephant Consulting
 
Cloud Computing: How to Change The World In 1,825 Days…
Blue Elephant Consulting
 
An Introduction To Software Development - Design Strategies
Blue Elephant Consulting
 
An Introduction To Software Development - Implementation
Blue Elephant Consulting
 
An Introduction To Python - Working With Data
Blue Elephant Consulting
 
Intro To C++ - Class 11 - Converting between types, formatting floating point...
Blue Elephant Consulting
 
Using A Balanced Scorecard For An IT Department -- The Secret To Knowing Wher...
Blue Elephant Consulting
 
Ad

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

PDF
An Introduction To Software Development - Final Review
Blue Elephant Consulting
 
PPTX
Unit testing
PiXeL16
 
PDF
Adopting tdd in the workplace
Donny Wals
 
PPTX
{10.0} Test Driven Development.pptx
AmalEldhose2
 
PPTX
A Brief Introduction to Test-Driven Development
Shawn Jones
 
PPTX
Building In Quality: The Beauty Of Behavior Driven Development (BDD)
Synerzip
 
PPTX
Gateway to Agile: XP and BDD
Gervais Johnson, Advisor
 
PPT
Test Driven Development and Automation
Mahesh Salaria
 
PPT
Chapter 6,7 Software coding and Testing.ppt
gadisaAdamu
 
PPTX
Test Driven Development on Android (Kotlin Kenya)
Danny Preussler
 
PDF
The Cowardly Test-o-Phobe's Guide To Testing
Tim Duckett
 
PDF
What CS Class Didn't Teach About Testing
Camille Bell
 
PDF
Joe Cisar - Everything I Know About TDD - Agile Midwest 2019
Jason Tice
 
PPTX
TDD: seriously, try it! 
Nacho Cougil
 
KEY
Driving application development through behavior driven development
Einar Ingebrigtsen
 
PPTX
BTD2015 - Your Place In DevTOps is Finding Solutions - Not Just Bugs!
Andreas Grabner
 
PPTX
An Introduction To Software Development - Testing, Continuous integration
Blue Elephant Consulting
 
PPTX
Finding a good development partner
Kevin Poorman
 
PPT
TDD_demo.ppt
DavidGracia27
 
An Introduction To Software Development - Final Review
Blue Elephant Consulting
 
Unit testing
PiXeL16
 
Adopting tdd in the workplace
Donny Wals
 
{10.0} Test Driven Development.pptx
AmalEldhose2
 
A Brief Introduction to Test-Driven Development
Shawn Jones
 
Building In Quality: The Beauty Of Behavior Driven Development (BDD)
Synerzip
 
Gateway to Agile: XP and BDD
Gervais Johnson, Advisor
 
Test Driven Development and Automation
Mahesh Salaria
 
Chapter 6,7 Software coding and Testing.ppt
gadisaAdamu
 
Test Driven Development on Android (Kotlin Kenya)
Danny Preussler
 
The Cowardly Test-o-Phobe's Guide To Testing
Tim Duckett
 
What CS Class Didn't Teach About Testing
Camille Bell
 
Joe Cisar - Everything I Know About TDD - Agile Midwest 2019
Jason Tice
 
TDD: seriously, try it! 
Nacho Cougil
 
Driving application development through behavior driven development
Einar Ingebrigtsen
 
BTD2015 - Your Place In DevTOps is Finding Solutions - Not Just Bugs!
Andreas Grabner
 
An Introduction To Software Development - Testing, Continuous integration
Blue Elephant Consulting
 
Finding a good development partner
Kevin Poorman
 
TDD_demo.ppt
DavidGracia27
 
Ad

Recently uploaded (20)

PPTX
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
PPTX
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
PPTX
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
PPTX
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
PPTX
QUARTER 1 WEEK 2 PLOT, POV AND CONFLICTS
KynaParas
 
PPT
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
PDF
Geographical Diversity of India 100 Mcq.pdf/ 7th class new ncert /Social/Samy...
Sandeep Swamy
 
PDF
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
PDF
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
PPTX
GRADE-3-PPT-EVE-2025-ENG-Q1-LESSON-1.pptx
EveOdrapngimapNarido
 
PPTX
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
PPTX
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
PDF
Dimensions of Societal Planning in Commonism
StefanMz
 
PPTX
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PPTX
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
PPT
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
PPTX
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
PPTX
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
PDF
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
PPTX
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 
Identifying elements in the story. Arrange the events in the story
geraldineamahido2
 
How to Create Odoo JS Dialog_Popup in Odoo 18
Celine George
 
PPT-Q1-WEEK-3-SCIENCE-ERevised Matatag Grade 3.pptx
reijhongidayawan02
 
I AM MALALA The Girl Who Stood Up for Education and was Shot by the Taliban...
Beena E S
 
QUARTER 1 WEEK 2 PLOT, POV AND CONFLICTS
KynaParas
 
Talk on Critical Theory, Part II, Philosophy of Social Sciences
Soraj Hongladarom
 
Geographical Diversity of India 100 Mcq.pdf/ 7th class new ncert /Social/Samy...
Sandeep Swamy
 
Chapter-V-DED-Entrepreneurship: Institutions Facilitating Entrepreneurship
Dayanand Huded
 
ARAL-Orientation_Morning-Session_Day-11.pdf
JoelVilloso1
 
GRADE-3-PPT-EVE-2025-ENG-Q1-LESSON-1.pptx
EveOdrapngimapNarido
 
Post Dated Cheque(PDC) Management in Odoo 18
Celine George
 
How to Create a PDF Report in Odoo 18 - Odoo Slides
Celine George
 
Dimensions of Societal Planning in Commonism
StefanMz
 
PATIENT ASSIGNMENTS AND NURSING CARE RESPONSIBILITIES.pptx
PRADEEP ABOTHU
 
PPT-Q1-WK-3-ENGLISH Revised Matatag Grade 3.pptx
reijhongidayawan02
 
Talk on Critical Theory, Part One, Philosophy of Social Sciences
Soraj Hongladarom
 
HUMAN RESOURCE MANAGEMENT: RECRUITMENT, SELECTION, PLACEMENT, DEPLOYMENT, TRA...
PRADEEP ABOTHU
 
Cultivation practice of Litchi in Nepal.pptx
UmeshTimilsina1
 
DIGESTION OF CARBOHYDRATES,PROTEINS,LIPIDS
raviralanaresh2
 
MENINGITIS: NURSING MANAGEMENT, BACTERIAL MENINGITIS, VIRAL MENINGITIS.pptx
PRADEEP ABOTHU
 

An Introduction To Software Development - Test Driven Development

  • 1. An Introduction To Software Development Using Python Spring Semester, 2014 Class #16: Test Driven Development
  • 2. 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
  • 3. 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
  • 4. 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
  • 5. 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. Image Credit: www.canstockphoto.com
  • 6. 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. Image Credit: revistanautilus.ro
  • 7. 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? Image Credit: antoni.w-w.pl
  • 8. 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. Image Credit: www.clipartpanda.com
  • 9. 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. Image Credit: www.fs.usda.gov
  • 10. 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. Image Credit: www.clipartpanda.com
  • 11. 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” Image Credit: www.fotosearch.com
  • 12. 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 Image Credit: cliparts.co
  • 13. 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
  • 14. 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? Image Credit: www.clipartpanda.com
  • 15. 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 Image Credit: www.dreamstime.com
  • 16. 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… Image Credit: cliparts.co
  • 17. Dependencies Are A Bad Thing When It Comes To Testing • Simple code means that you can break things up and test parts of your code one thing at a time. • Dependencies: code that depends on something that is external to your code (like a database or an interface) • Dependencies make it hard to test one thing at a time • Your job is to figure out a way to test your code independent of the dependencies Image Credit: blogs.msdn.com
  • 18. First You Have To Find Your DependenciesPay with Visa 3 – Represent the Order Information: • Get card owner’s name • Get card owner’s address – Represent the Credit Card Information: • Get credit card number • Get secret confirmation number – Represent Receipt Information: • Provide confirmation number • The final cost • Date of the transaction. Name Address CC # Secret # CC # Secret # Conf # Cost Date Conf # Cost Date Your Program Your Program Your Program Credit Card Paper
  • 19. Need To Create Testing Interfaces Name Address CC # Secret # Dummy Input Conf # Cost Date Dummy Output Your Program Your Program Your Program • Need to hide how our application gets and gives its data • Change how this works depending on if we are conducting testing or running in production • We want to have two different ways of passing the information that we want • Our program does not have to know which one it is using Dummy Input Code Stub Code Stub Image Credit: www.clker.com
  • 20. What Would This Look Like In Python? # # Constants used in this program runningTest = 1 # 0 = in production, 1 = testing being done # # Collect personal information from credit card owner if (runningTest == 0) : lastName = input("Please enter last name: ") firstName = input("Please enter first name: ") middleInitial = input("Please enter the middle initial: ") streetAddress = input("Please enter street address: ") city = input("Please enter city: ") state = input("Please enter state: ") zip = input("Please enter zip: ") else: lastName = "Johnson" firstName = "Fred“ middleInitial = "N" streetAddress = “2763 Filibuster Drive” city = "Lakeland" state = “FL” zip = "37643" Image Credit: 4vector.com
  • 21. What Does Testing First Buy Us? • Testing produces better code • Create well-organized code – Simpler code – Nothing that is not absolutely required • Code that always does the same thing – Production code does the same thing as testing code – We are writing production code all the time • Code is loosely coupled – Tightly coupled code is brittle and breaks easily – Our code will be broken into a loosely coupled flexible system Image Credit: www.fotosearch.com
  • 22. What Kind Of Tests Do We Have To Write Now? Name Address CC # Secret # CC # Secret # Conf # Cost Date Conf # Cost Date Your Program Your Program Your Program Credit Card Paper Name too long Name too short Illegal name characters Street too long Street too short Illegal street characters No name No street No CC # CC # too long CC # too short No secret # Secret # too long Secret # too short Wrong secret # Valid name Valid street Valid CC # Valid secret # Valid conf # Valid cost Valid date 22 Tests
  • 23. Things NOT To Do When Writing Code Testing Nothing: You can write a lot of test code that doesn’t really test anything. Could place a credit card order and then not test the cost printed on the final receipt. Don’t Test Your Data: It can be all too easy to create test cases that test the fake data that you’ve created and not the code that you’ve written. Get Rid Of Scraps: The system state that your testing starts in is very important. You need to always clean up and start in the same state each time. Image Credit: alexrister1.wordpress.com
  • 24. So Just Exactly What Have We Learned About Testing? 1. Always write the test before you write the code. 2. Make the test fail first, then write the simplest code that will make the test pass 3. Each test should only test one thing 4. Once your tests pass, you can then clean up your code (refactor) 5. When you are out of tests to write, you’re done! Image Credit: clipart-finder.com
  • 25. 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/
  • 26. What We’ll Be Covering Next Time 1. Ending an iteration 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.