SlideShare una empresa de Scribd logo
1 de 19
Descargar para leer sin conexión
TDD - Survival Guide
Vitali Perchonok [ vitali.pe@gmail.com ]
A very simple workflow, That focuses on
code correctness, flexibility and clarity.
TDD Is
Here’s The Basic Workflow
Write a test that fails
Start Here
Here’s The Basic Workflow
Write a test that fails Make it pass
Start Here
Here’s The Basic Workflow
Write a test that fails Make it pass
Refactor
Start Here
A replacement for a good high level design.
TDD Is NOT
TDD is bottom-up by nature which makes it hard to see the big picture.
NOTE: Many people will disagree
with me on this one!
Also sometimes by starting with a high level architecture
I can leverage existing knowledge, this is why frameworks like QT exist.
The same as unit testing
TDD Is NOT
TDD is a workflow, You use unit tests as a tool to drive your design
and ensure that you’re on the right track.
Unit tests can be (and are) used outside the scope of TDD.
It is true however that writing good unit tests on existing code is very hard.
A replacement for your QA
TDD Is NOT
Unit tests are simply not enough, There are also Usability Tests
Integration tests, Performance tests etc…
The 3 Rules Of TDD
1. You are not allowed to write any production code unless
it is to make a failing unit test pass.
The 3 Rules Of TDD
1. You are not allowed to write any production code unless
it is to make a failing unit test pass.
2. You are not allowed to write any more of a unit test than
is sufficient to fail.
The 3 Rules Of TDD
1. You are not allowed to write any production code unless
it is to make a failing unit test pass.
2. You are not allowed to write any more of a unit test than
is sufficient to fail.
3. You are not allowed to write any more production code
than is sufficient to pass the one failing unit test.
Wait, What’s a Unit Test?
Code that tests certain behavior in an isolated component.
def test_covert_red_from_RGB_to_HSL(self):
redHSL = [0, 100, 50]
self.assertListEqual(redHSL, rgb2hsl(255,0, 0))
def test_start_car_with_no_gas_should_throw_error(self):
emptyGasTank = FakeGasTank()
car = Car(emptyGasTank, FakeEngine())
emptyGasTank.gasLeft = MagicMock(return_value=0) # new in python 3.3
self.assertRaises(CustomError, car.start)
Remember, It’s NOT A Unit Test If
● It touches the file system or the DB (read || write)
● It communicates across the network
● It can’t run in parallel with other unit tests.
● It contains randomness, (i.e different each time you run it).
● It requires you to modify the environment (edit config etc…)
● It doesn't contain an assert statement.
Anatomy Of A Unit Test
class TestAssetLoader(unittest.TestCase):
...
def test_should_only_load_assets_once(self):
fakeFileReader = FakeFileReader()
assetManager = AssetManager(fakeFileReader)
assetManager.getImage("kozet_the_sheep")
assetManager.getImage("kozet_the_sheep")
self.assertEqual(1, fakeFileReader.load.call_count)
...
Anatomy Of A Unit Test
fakeFileReader = FakeFileReader()
assetManager = AssetManager(fakeFileReader)
assetManager.getImage("kozet_the_sheep")
assetManager.getImage("kozet_the_sheep")
self.assertEqual(1, fakeFileReader.load.call_count)
Arrange
Act
Assert
Arrange Act Assert (AAA) Pattern
Fakes (Mocks, Stubs, Spies)
Fakes Are Basically Used To:
1. Isolate the component under test from the rest of the system.
2. Inspect the effects on the outside world.
Fakes (Mocks, Stubs, Spies)
● Stub is just a dummy object to fill the role of a real object.
● Mock is a stub with an assert condition inside.
● Spy is just a wrapper that stores info like number of calls, parameters etc...
Here’s a simple way to think about them:
Most of the time when people say mock they actually mean stub.
But it’s not important as long as you don’t use no more than 1 real mock in a
test.
QA
Code

Más contenido relacionado

La actualidad más candente

Roy Osherove TDD From Scratch
Roy Osherove TDD From ScratchRoy Osherove TDD From Scratch
Roy Osherove TDD From Scratch
Roy Osherove
 
Unit Testing Fundamentals
Unit Testing FundamentalsUnit Testing Fundamentals
Unit Testing Fundamentals
Richard Paul
 
Unit Testing Best Practices
Unit Testing Best PracticesUnit Testing Best Practices
Unit Testing Best Practices
Tomaš Maconko
 
Unit Testing And Mocking
Unit Testing And MockingUnit Testing And Mocking
Unit Testing And Mocking
Joe Wilson
 
Tdd in php a brief example
Tdd in php   a brief exampleTdd in php   a brief example
Tdd in php a brief example
Jeremy Kendall
 

La actualidad más candente (20)

Roy Osherove TDD From Scratch
Roy Osherove TDD From ScratchRoy Osherove TDD From Scratch
Roy Osherove TDD From Scratch
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
AspectMock
AspectMockAspectMock
AspectMock
 
Unit Testing Fundamentals
Unit Testing FundamentalsUnit Testing Fundamentals
Unit Testing Fundamentals
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
PHPUnit - Unit testing
PHPUnit - Unit testingPHPUnit - Unit testing
PHPUnit - Unit testing
 
VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)VT.NET 20160411: An Intro to Test Driven Development (TDD)
VT.NET 20160411: An Intro to Test Driven Development (TDD)
 
Unit testing
Unit testing Unit testing
Unit testing
 
Understanding Unit Testing
Understanding Unit TestingUnderstanding Unit Testing
Understanding Unit Testing
 
Test driven development in .Net - 2010 + Eclipse
Test driven development in .Net - 2010 + EclipseTest driven development in .Net - 2010 + Eclipse
Test driven development in .Net - 2010 + Eclipse
 
Unit Testing Done Right
Unit Testing Done RightUnit Testing Done Right
Unit Testing Done Right
 
Getting Unstuck: Working with Legacy Code and Data
Getting Unstuck: Working with Legacy Code and DataGetting Unstuck: Working with Legacy Code and Data
Getting Unstuck: Working with Legacy Code and Data
 
Unit Testing Best Practices
Unit Testing Best PracticesUnit Testing Best Practices
Unit Testing Best Practices
 
Unit Testing And Mocking
Unit Testing And MockingUnit Testing And Mocking
Unit Testing And Mocking
 
Unit Tests And Automated Testing
Unit Tests And Automated TestingUnit Tests And Automated Testing
Unit Tests And Automated Testing
 
Refactoring Legacy Code
Refactoring Legacy CodeRefactoring Legacy Code
Refactoring Legacy Code
 
Intro to junit
Intro to junitIntro to junit
Intro to junit
 
Unit Testing in Action - C#, NUnit, and Moq
Unit Testing in Action - C#, NUnit, and MoqUnit Testing in Action - C#, NUnit, and Moq
Unit Testing in Action - C#, NUnit, and Moq
 
Software Quality via Unit Testing
Software Quality via Unit TestingSoftware Quality via Unit Testing
Software Quality via Unit Testing
 
Tdd in php a brief example
Tdd in php   a brief exampleTdd in php   a brief example
Tdd in php a brief example
 

Similar a TDD - survival guide

Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
Dhaval Dalal
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
bhochhi
 

Similar a TDD - survival guide (20)

TDD Best Practices
TDD Best PracticesTDD Best Practices
TDD Best Practices
 
How to complement TDD with static analysis
How to complement TDD with static analysisHow to complement TDD with static analysis
How to complement TDD with static analysis
 
TDD reloaded - JUGTAA 24 Ottobre 2012
TDD reloaded - JUGTAA 24 Ottobre 2012TDD reloaded - JUGTAA 24 Ottobre 2012
TDD reloaded - JUGTAA 24 Ottobre 2012
 
Unit testing - An introduction
Unit testing - An introductionUnit testing - An introduction
Unit testing - An introduction
 
Unit testing - A&BP CC
Unit testing - A&BP CCUnit testing - A&BP CC
Unit testing - A&BP CC
 
JavaScript Unit Testing
JavaScript Unit TestingJavaScript Unit Testing
JavaScript Unit Testing
 
TDD Flow: The Mantra in Action
TDD Flow: The Mantra in ActionTDD Flow: The Mantra in Action
TDD Flow: The Mantra in Action
 
assertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
assertYourself - Breaking the Theories and Assumptions of Unit Testing in FlexassertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
assertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex
 
Unit Testing and TDD 2017
Unit Testing and TDD 2017Unit Testing and TDD 2017
Unit Testing and TDD 2017
 
Getting started with Test Driven Development - Ferdous Mahmud Shaon
Getting started with Test Driven Development - Ferdous Mahmud ShaonGetting started with Test Driven Development - Ferdous Mahmud Shaon
Getting started with Test Driven Development - Ferdous Mahmud Shaon
 
Getting started with Test Driven Development
Getting started with Test Driven DevelopmentGetting started with Test Driven Development
Getting started with Test Driven Development
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Test-Driven Development In Action
Test-Driven Development In ActionTest-Driven Development In Action
Test-Driven Development In Action
 
SELJE_Database_Unit_Testing.pdf
SELJE_Database_Unit_Testing.pdfSELJE_Database_Unit_Testing.pdf
SELJE_Database_Unit_Testing.pdf
 
Unit testing
Unit testingUnit testing
Unit testing
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Unit testing
Unit testingUnit testing
Unit testing
 
Test Driven
Test DrivenTest Driven
Test Driven
 
SELJE_Database_Unit_Testing_Slides.pdf
SELJE_Database_Unit_Testing_Slides.pdfSELJE_Database_Unit_Testing_Slides.pdf
SELJE_Database_Unit_Testing_Slides.pdf
 

Último

CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
Health
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
anilsa9823
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
anilsa9823
 

Último (20)

The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS LiveVip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
Vip Call Girls Noida ➡️ Delhi ➡️ 9999965857 No Advance 24HRS Live
 
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AISyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
SyndBuddy AI 2k Review 2024: Revolutionizing Content Syndication with AI
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
Short Story: Unveiling the Reasoning Abilities of Large Language Models by Ke...
 
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female serviceCALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
CALL ON ➥8923113531 🔝Call Girls Badshah Nagar Lucknow best Female service
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
Tech Tuesday-Harness the Power of Effective Resource Planning with OnePlan’s ...
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online  ☂️
CALL ON ➥8923113531 🔝Call Girls Kakori Lucknow best sexual service Online ☂️
 

TDD - survival guide

  • 1. TDD - Survival Guide Vitali Perchonok [ vitali.pe@gmail.com ]
  • 2. A very simple workflow, That focuses on code correctness, flexibility and clarity. TDD Is
  • 3. Here’s The Basic Workflow Write a test that fails Start Here
  • 4. Here’s The Basic Workflow Write a test that fails Make it pass Start Here
  • 5. Here’s The Basic Workflow Write a test that fails Make it pass Refactor Start Here
  • 6. A replacement for a good high level design. TDD Is NOT TDD is bottom-up by nature which makes it hard to see the big picture. NOTE: Many people will disagree with me on this one! Also sometimes by starting with a high level architecture I can leverage existing knowledge, this is why frameworks like QT exist.
  • 7. The same as unit testing TDD Is NOT TDD is a workflow, You use unit tests as a tool to drive your design and ensure that you’re on the right track. Unit tests can be (and are) used outside the scope of TDD. It is true however that writing good unit tests on existing code is very hard.
  • 8. A replacement for your QA TDD Is NOT Unit tests are simply not enough, There are also Usability Tests Integration tests, Performance tests etc…
  • 9. The 3 Rules Of TDD 1. You are not allowed to write any production code unless it is to make a failing unit test pass.
  • 10. The 3 Rules Of TDD 1. You are not allowed to write any production code unless it is to make a failing unit test pass. 2. You are not allowed to write any more of a unit test than is sufficient to fail.
  • 11. The 3 Rules Of TDD 1. You are not allowed to write any production code unless it is to make a failing unit test pass. 2. You are not allowed to write any more of a unit test than is sufficient to fail. 3. You are not allowed to write any more production code than is sufficient to pass the one failing unit test.
  • 12. Wait, What’s a Unit Test? Code that tests certain behavior in an isolated component. def test_covert_red_from_RGB_to_HSL(self): redHSL = [0, 100, 50] self.assertListEqual(redHSL, rgb2hsl(255,0, 0)) def test_start_car_with_no_gas_should_throw_error(self): emptyGasTank = FakeGasTank() car = Car(emptyGasTank, FakeEngine()) emptyGasTank.gasLeft = MagicMock(return_value=0) # new in python 3.3 self.assertRaises(CustomError, car.start)
  • 13. Remember, It’s NOT A Unit Test If ● It touches the file system or the DB (read || write) ● It communicates across the network ● It can’t run in parallel with other unit tests. ● It contains randomness, (i.e different each time you run it). ● It requires you to modify the environment (edit config etc…) ● It doesn't contain an assert statement.
  • 14. Anatomy Of A Unit Test class TestAssetLoader(unittest.TestCase): ... def test_should_only_load_assets_once(self): fakeFileReader = FakeFileReader() assetManager = AssetManager(fakeFileReader) assetManager.getImage("kozet_the_sheep") assetManager.getImage("kozet_the_sheep") self.assertEqual(1, fakeFileReader.load.call_count) ...
  • 15. Anatomy Of A Unit Test fakeFileReader = FakeFileReader() assetManager = AssetManager(fakeFileReader) assetManager.getImage("kozet_the_sheep") assetManager.getImage("kozet_the_sheep") self.assertEqual(1, fakeFileReader.load.call_count) Arrange Act Assert Arrange Act Assert (AAA) Pattern
  • 16. Fakes (Mocks, Stubs, Spies) Fakes Are Basically Used To: 1. Isolate the component under test from the rest of the system. 2. Inspect the effects on the outside world.
  • 17. Fakes (Mocks, Stubs, Spies) ● Stub is just a dummy object to fill the role of a real object. ● Mock is a stub with an assert condition inside. ● Spy is just a wrapper that stores info like number of calls, parameters etc... Here’s a simple way to think about them: Most of the time when people say mock they actually mean stub. But it’s not important as long as you don’t use no more than 1 real mock in a test.
  • 18. QA
  • 19. Code