TDD - survival guide

V
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
1 of 19

Recommended

Unit testing legacy code by
Unit testing legacy codeUnit testing legacy code
Unit testing legacy codeLars Thorup
2K views31 slides
TDD by
TDDTDD
TDDDotan Patrich
914 views14 slides
Adding Unit Test To Legacy Code by
Adding Unit Test To Legacy CodeAdding Unit Test To Legacy Code
Adding Unit Test To Legacy CodeTerry Yin
2.3K views28 slides
Unit test by
Unit testUnit test
Unit testTran Duc
760 views43 slides
Refactoring legacy code driven by tests - ENG by
Refactoring legacy code driven by tests - ENGRefactoring legacy code driven by tests - ENG
Refactoring legacy code driven by tests - ENGLuca Minudel
2.5K views52 slides
Roy Osherove on Unit Testing Good Practices and Horrible Mistakes by
Roy Osherove on Unit Testing Good Practices and Horrible MistakesRoy Osherove on Unit Testing Good Practices and Horrible Mistakes
Roy Osherove on Unit Testing Good Practices and Horrible MistakesRoy Osherove
25.1K views53 slides

More Related Content

What's hot

Roy Osherove TDD From Scratch by
Roy Osherove TDD From ScratchRoy Osherove TDD From Scratch
Roy Osherove TDD From ScratchRoy Osherove
1.8K views25 slides
Unit Testing by
Unit TestingUnit Testing
Unit TestingFrançois Camus
1.2K views15 slides
AspectMock by
AspectMockAspectMock
AspectMockBryce Embry
491 views15 slides
Unit Testing Fundamentals by
Unit Testing FundamentalsUnit Testing Fundamentals
Unit Testing FundamentalsRichard Paul
4K views21 slides
Unit Testing by
Unit TestingUnit Testing
Unit TestingAvinash Kadam
71 views16 slides
PHPUnit - Unit testing by
PHPUnit - Unit testingPHPUnit - Unit testing
PHPUnit - Unit testingNguyễn Đào Thiên Thư
4K views54 slides

What's hot(20)

Roy Osherove TDD From Scratch by Roy Osherove
Roy Osherove TDD From ScratchRoy Osherove TDD From Scratch
Roy Osherove TDD From Scratch
Roy Osherove1.8K views
Unit Testing Fundamentals by Richard Paul
Unit Testing FundamentalsUnit Testing Fundamentals
Unit Testing Fundamentals
Richard Paul4K views
VT.NET 20160411: An Intro to Test Driven Development (TDD) by Rob Hale
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)
Rob Hale445 views
Unit testing by dubbu
Unit testing Unit testing
Unit testing
dubbu75 views
Understanding Unit Testing by ikhwanhayat
Understanding Unit TestingUnderstanding Unit Testing
Understanding Unit Testing
ikhwanhayat6.2K views
Unit Testing Done Right by Brian Fenton
Unit Testing Done RightUnit Testing Done Right
Unit Testing Done Right
Brian Fenton1.2K views
Getting Unstuck: Working with Legacy Code and Data by Cory Foy
Getting Unstuck: Working with Legacy Code and DataGetting Unstuck: Working with Legacy Code and Data
Getting Unstuck: Working with Legacy Code and Data
Cory Foy1.9K views
Unit Testing And Mocking by Joe Wilson
Unit Testing And MockingUnit Testing And Mocking
Unit Testing And Mocking
Joe Wilson7.1K views
Unit Tests And Automated Testing by Lee Englestone
Unit Tests And Automated TestingUnit Tests And Automated Testing
Unit Tests And Automated Testing
Lee Englestone7.7K views
Refactoring Legacy Code by Adam Culp
Refactoring Legacy CodeRefactoring Legacy Code
Refactoring Legacy Code
Adam Culp4.3K views
Unit Testing in Action - C#, NUnit, and Moq by XPDays
Unit Testing in Action - C#, NUnit, and MoqUnit Testing in Action - C#, NUnit, and Moq
Unit Testing in Action - C#, NUnit, and Moq
XPDays69 views
Software Quality via Unit Testing by Shaun Abram
Software Quality via Unit TestingSoftware Quality via Unit Testing
Software Quality via Unit Testing
Shaun Abram1.9K views
Tdd in php a brief example by Jeremy Kendall
Tdd in php   a brief exampleTdd in php   a brief example
Tdd in php a brief example
Jeremy Kendall2.1K views

Similar to TDD - survival guide

TDD Best Practices by
TDD Best PracticesTDD Best Practices
TDD Best PracticesAttila Bertók
517 views70 slides
How to complement TDD with static analysis by
How to complement TDD with static analysisHow to complement TDD with static analysis
How to complement TDD with static analysisPVS-Studio
290 views5 slides
TDD reloaded - JUGTAA 24 Ottobre 2012 by
TDD reloaded - JUGTAA 24 Ottobre 2012TDD reloaded - JUGTAA 24 Ottobre 2012
TDD reloaded - JUGTAA 24 Ottobre 2012Pietro Di Bello
483 views53 slides
Unit testing - An introduction by
Unit testing - An introductionUnit testing - An introduction
Unit testing - An introductionAlejandro Claro Mosqueda
967 views26 slides
Unit testing - A&BP CC by
Unit testing - A&BP CCUnit testing - A&BP CC
Unit testing - A&BP CCJWORKS powered by Ordina
722 views113 slides
JavaScript Unit Testing by
JavaScript Unit TestingJavaScript Unit Testing
JavaScript Unit TestingL&T Technology Services Limited
1.2K views16 slides

Similar to TDD - survival guide (20)

How to complement TDD with static analysis by PVS-Studio
How to complement TDD with static analysisHow to complement TDD with static analysis
How to complement TDD with static analysis
PVS-Studio290 views
TDD reloaded - JUGTAA 24 Ottobre 2012 by Pietro Di Bello
TDD reloaded - JUGTAA 24 Ottobre 2012TDD reloaded - JUGTAA 24 Ottobre 2012
TDD reloaded - JUGTAA 24 Ottobre 2012
Pietro Di Bello483 views
assertYourself - Breaking the Theories and Assumptions of Unit Testing in Flex by michael.labriola
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
michael.labriola839 views
Unit Testing and TDD 2017 by Xavi Hidalgo
Unit Testing and TDD 2017Unit Testing and TDD 2017
Unit Testing and TDD 2017
Xavi Hidalgo440 views
Getting started with Test Driven Development - Ferdous Mahmud Shaon by Cefalo
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
Cefalo14 views
Test Driven Development by Dhaval Dalal
Test Driven DevelopmentTest Driven Development
Test Driven Development
Dhaval Dalal2.5K views
Test driven development by namkha87
Test driven developmentTest driven development
Test driven development
namkha87528 views
Test-Driven Development In Action by Jon Kruger
Test-Driven Development In ActionTest-Driven Development In Action
Test-Driven Development In Action
Jon Kruger4.4K views
SELJE_Database_Unit_Testing.pdf by Eric Selje
SELJE_Database_Unit_Testing.pdfSELJE_Database_Unit_Testing.pdf
SELJE_Database_Unit_Testing.pdf
Eric Selje4 views
Test Driven Development by bhochhi
Test Driven DevelopmentTest Driven Development
Test Driven Development
bhochhi342 views
Unit testing by PiXeL16
Unit testingUnit testing
Unit testing
PiXeL16545 views
SELJE_Database_Unit_Testing_Slides.pdf by Eric Selje
SELJE_Database_Unit_Testing_Slides.pdfSELJE_Database_Unit_Testing_Slides.pdf
SELJE_Database_Unit_Testing_Slides.pdf
Eric Selje6 views

Recently uploaded

Programming Field by
Programming FieldProgramming Field
Programming Fieldthehardtechnology
6 views9 slides
Introduction to Gradle by
Introduction to GradleIntroduction to Gradle
Introduction to GradleJohn Valentino
5 views7 slides
MS PowerPoint.pptx by
MS PowerPoint.pptxMS PowerPoint.pptx
MS PowerPoint.pptxLitty Sylus
7 views14 slides
ADDO_2022_CICID_Tom_Halpin.pdf by
ADDO_2022_CICID_Tom_Halpin.pdfADDO_2022_CICID_Tom_Halpin.pdf
ADDO_2022_CICID_Tom_Halpin.pdfTomHalpin9
5 views33 slides
The Era of Large Language Models.pptx by
The Era of Large Language Models.pptxThe Era of Large Language Models.pptx
The Era of Large Language Models.pptxAbdulVahedShaik
7 views9 slides
Playwright Retries by
Playwright RetriesPlaywright Retries
Playwright Retriesartembondar5
5 views1 slide

Recently uploaded(20)

ADDO_2022_CICID_Tom_Halpin.pdf by TomHalpin9
ADDO_2022_CICID_Tom_Halpin.pdfADDO_2022_CICID_Tom_Halpin.pdf
ADDO_2022_CICID_Tom_Halpin.pdf
TomHalpin95 views
DRYiCE™ iAutomate: AI-enhanced Intelligent Runbook Automation by HCLSoftware
DRYiCE™ iAutomate: AI-enhanced Intelligent Runbook AutomationDRYiCE™ iAutomate: AI-enhanced Intelligent Runbook Automation
DRYiCE™ iAutomate: AI-enhanced Intelligent Runbook Automation
HCLSoftware6 views
Dapr Unleashed: Accelerating Microservice Development by Miroslav Janeski
Dapr Unleashed: Accelerating Microservice DevelopmentDapr Unleashed: Accelerating Microservice Development
Dapr Unleashed: Accelerating Microservice Development
Miroslav Janeski13 views
Electronic AWB - Electronic Air Waybill by Freightoscope
Electronic AWB - Electronic Air Waybill Electronic AWB - Electronic Air Waybill
Electronic AWB - Electronic Air Waybill
Freightoscope 5 views
JioEngage_Presentation.pptx by admin125455
JioEngage_Presentation.pptxJioEngage_Presentation.pptx
JioEngage_Presentation.pptx
admin1254558 views
Top-5-production-devconMunich-2023.pptx by Tier1 app
Top-5-production-devconMunich-2023.pptxTop-5-production-devconMunich-2023.pptx
Top-5-production-devconMunich-2023.pptx
Tier1 app9 views
Understanding HTML terminology by artembondar5
Understanding HTML terminologyUnderstanding HTML terminology
Understanding HTML terminology
artembondar57 views
Fleet Management Software in India by Fleetable
Fleet Management Software in India Fleet Management Software in India
Fleet Management Software in India
Fleetable12 views
360 graden fabriek by info33492
360 graden fabriek360 graden fabriek
360 graden fabriek
info33492162 views
AI and Ml presentation .pptx by FayazAli87
AI and Ml presentation .pptxAI and Ml presentation .pptx
AI and Ml presentation .pptx
FayazAli8714 views
Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium... by Lisi Hocke
Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium...Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium...
Team Transformation Tactics for Holistic Testing and Quality (Japan Symposium...
Lisi Hocke35 views
Top-5-production-devconMunich-2023-v2.pptx by Tier1 app
Top-5-production-devconMunich-2023-v2.pptxTop-5-production-devconMunich-2023-v2.pptx
Top-5-production-devconMunich-2023-v2.pptx
Tier1 app6 views
Navigating container technology for enhanced security by Niklas Saari by Metosin Oy
Navigating container technology for enhanced security by Niklas SaariNavigating container technology for enhanced security by Niklas Saari
Navigating container technology for enhanced security by Niklas Saari
Metosin Oy14 views

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