SlideShare a Scribd company logo
Test Driven Development
Ing. Facundo Farias
facundo.farias@gmail.com
November, 2012
Agenda
Introduction to Unit Testing.
 Introduction to TDD.
 How it works? Steps.
 Mock Objects.
 Dependency Injection
 Characteristics
 Benefits

What is it?


Is an advanced technique of using
automated unit tests to drive the
design of software and force
decoupling of dependencies.



Introduced by Kent Beck as part of XP
(Extreme Programming).
What is a Unit Test?


In computer programming, unit testing
is a method by which individual units
of source code, sets of one or more
computer program modules together
with associated control data, usage
procedures, and operating
procedures, are tested to determine if
they are fit for use.
What is a Unit Test? (Cont.)
/// <summary>
/// The sum should work using two integers.
/// </summary>
[TestMethod]
public void SumShouldWorkUsingTwoIntegers()
{
// Given
int numberA = 50;
int numberB = 100;
// When
int sum = numberA + numberB;

// Then
Assert.AreEqual(sum, 150);
}
How it works?
Steps (I)




Understand the requirements of the
story, work item, or feature that you are
working on.
RED: Create a test and make it fail.
◦ Imagine how the new code should be called
and write the test as if the code already
existed.
◦ Create the new production code stub. Write
just enough code so that it compiles.
◦ Run the test. It should fail.
Steps (II)


GREEN: Make the test pass by any
means necessary.
◦ Write the production code to make the test
pass. Keep it simple.
◦ If you've written the code so that the test
passes as intended, you are finished. You do
not have to write more code speculatively.
Make this one test pass and continue.
◦ When the test passes, you might want to run
all tests up to this point to build confidence
that everything else is still working.
Steps (III)


REFACTOR: Change the code to remove
duplication in your project and to improve the
design while ensuring that all tests still pass.
◦ Remove duplication caused by the addition of the
new functionality.
◦ Make design changes to improve the overall
solution.
◦ After each refactoring, rerun all the tests to
ensure that they all still pass.



Repeat the cycle. Each cycle should be very
short, and a typical hour should contain many
Red/Green/Refactor cycles.
Unit Testing Tools


Each programming language has
different tools to use TDD:
◦
◦
◦
◦
◦
◦
◦

Junit,
Mstest, Nunit,
PHPUnit,
Sunit,
PyUnit, Unittest,
CppUnit, CxxTest,
Etc
Mock Objects




Mock Objects are simulated objects that mimic the
behavior of real objects in controlled ways. A
programmer typically creates a mock object to test
the behavior of some other object, in much the
same way that a car designer uses a crash test
dummy to simulate the dynamic behavior of a
human in vehicle impacts.
Frameworks: Mockito, EasyMock, Jmockit,
RhinoMocks, Nmock, etc.
Dependency Injection
Dependency Injection is a software design
pattern that allows a choice of component to be
made at run-time rather than compile time. This
can be used, for example, as a simple way to load
plugins dynamically or to choose mock objects in
test environments vs. real objects in production
environments. This software design pattern injects
the dependent element (object or value etc.) to the
destination automatically by knowing the
requirement of the destination.
 Frameworks: Spring, Spring.net, Ninject, Unity,
etc.

Characteristics











Runs fast, runs fast, runs fast. If the tests are slow, they will not be
run often.
Separates or simulates environmental dependencies such as
databases, file systems, networks, queues, and so on. Tests that
exercise these will not run fast, and a failure does not give
meaningful feedback about what the problem actually is.
Is very limited in scope. If the test fails, it's obvious where to look for
the problem. Use few Assert calls so that the offending code is
obvious. It's important to only test one thing in a single test.
Runs and passes in isolation. If the tests require special
environmental setup or fail unexpectedly, then they are not good unit
tests. Change them for simplicity and reliability. Tests should run and
pass on any machine. The "works on my box" excuse doesn't work.
Often uses stubs and mock objects. If the code being tested typically
calls out to a database or file system, these dependencies must be
simulated, or mocked. These dependencies will ordinarily be
abstracted away by using interfaces.
Clearly reveals its intention. Another developer can look at the test
and understand what is expected of the production code.
Benefits


The suite of unit tests provides constant feedback that each component is
still working.



The unit tests act as documentation that cannot go out-of-date, unlike
separate documentation, which can and frequently does.



When the test passes and the production code is refactored to remove
duplication, it is clear that the code is finished, and the developer can move
on to a new test.



Test-driven development forces critical analysis and design because the
developer cannot create the production code without truly understanding
what the desired result should be and how to test it.



The software tends to be better designed, that is, loosely coupled and easily
maintainable, because the developer is free to make design decisions and
refactor at any time with confidence that the software is still working. This
confidence is gained by running the tests. The need for a design pattern may
emerge, and the code can be changed at that time.



The test suite acts as a regression safety net on bugs: If a bug is found, the
developer should create a test to reveal the bug and then modify the
production code so that the bug goes away and all other tests still pass. On
each successive test run, all previous bug fixes are verified.
References










Test Driven Development: By Example [Kent Beck]
Unit testing [Wikipedia]
Guidelines for Test-Driven Development
Using mock objects for complex unit tests
Mocks Aren't Stubs [Martin Fowler]
Dependency Injection [Martin Fowler]
Spring for Java
Spring for c#
Q&A

More Related Content

What's hot

Introduction to test programming
Introduction to test programmingIntroduction to test programming
Introduction to test programming
openfinanceDev
 
Project Onion unit test environment
Project Onion unit test environmentProject Onion unit test environment
Project Onion unit test environment
Abhinav Jha
 
Peering into the white box: A testers approach to Code Reviews
Peering into the white box: A testers approach to Code ReviewsPeering into the white box: A testers approach to Code Reviews
Peering into the white box: A testers approach to Code Reviews
Alan Page
 
TDD (Test Driven Design)
TDD (Test Driven Design)TDD (Test Driven Design)
TDD (Test Driven Design)
nedirtv
 
Unit Testing And Mocking
Unit Testing And MockingUnit Testing And Mocking
Unit Testing And MockingJoe Wilson
 
Are Agile Projects Doomed to Half-Baked Design?
Are Agile Projects Doomed to Half-Baked Design?Are Agile Projects Doomed to Half-Baked Design?
Are Agile Projects Doomed to Half-Baked Design?
theinfonaut
 
Test driven development_continuous_integration
Test driven development_continuous_integrationTest driven development_continuous_integration
Test driven development_continuous_integration
haochenglee
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
Sachithra Gayan
 
Adopting Agile
Adopting AgileAdopting Agile
Adopting Agile
Coverity
 
Mock driven development using .NET
Mock driven development using .NETMock driven development using .NET
Mock driven development using .NET
Puneet Ghanshani
 
TDD Basics with Angular.js and Jasmine
TDD Basics with Angular.js and JasmineTDD Basics with Angular.js and Jasmine
TDD Basics with Angular.js and Jasmine
Luis Sánchez Castellanos
 
Test Driven Development - Overview and Adoption
Test Driven Development - Overview and AdoptionTest Driven Development - Overview and Adoption
Test Driven Development - Overview and Adoption
Pyxis Technologies
 
A Not-So-Serious Introduction to Test Driven Development (TDD)
A Not-So-Serious Introduction to Test Driven Development (TDD) A Not-So-Serious Introduction to Test Driven Development (TDD)
A Not-So-Serious Introduction to Test Driven Development (TDD)
CodeOps Technologies LLP
 
OSS Java Analysis - What You Might Be Missing
OSS Java Analysis - What You Might Be MissingOSS Java Analysis - What You Might Be Missing
OSS Java Analysis - What You Might Be Missing
Coverity
 
Loopt unit test experiences
Loopt unit test experiencesLoopt unit test experiences
Loopt unit test experiences
Heine Frifeldt
 
Roy Osherove on Unit Testing Good Practices and Horrible Mistakes
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
 
Test driven development
Test driven developmentTest driven development
Test driven development
Nascenia IT
 
Funny stories and anti-patterns from DevOps landscape
Funny stories and anti-patterns from DevOps landscapeFunny stories and anti-patterns from DevOps landscape
Funny stories and anti-patterns from DevOps landscape
Mikalai Alimenkou
 
FluentSelenium Presentation Code Camp09
FluentSelenium Presentation Code Camp09FluentSelenium Presentation Code Camp09
FluentSelenium Presentation Code Camp09
Pyxis Technologies
 
Unit Test + Functional Programming = Love
Unit Test + Functional Programming = LoveUnit Test + Functional Programming = Love
Unit Test + Functional Programming = Love
Alvaro Videla
 

What's hot (20)

Introduction to test programming
Introduction to test programmingIntroduction to test programming
Introduction to test programming
 
Project Onion unit test environment
Project Onion unit test environmentProject Onion unit test environment
Project Onion unit test environment
 
Peering into the white box: A testers approach to Code Reviews
Peering into the white box: A testers approach to Code ReviewsPeering into the white box: A testers approach to Code Reviews
Peering into the white box: A testers approach to Code Reviews
 
TDD (Test Driven Design)
TDD (Test Driven Design)TDD (Test Driven Design)
TDD (Test Driven Design)
 
Unit Testing And Mocking
Unit Testing And MockingUnit Testing And Mocking
Unit Testing And Mocking
 
Are Agile Projects Doomed to Half-Baked Design?
Are Agile Projects Doomed to Half-Baked Design?Are Agile Projects Doomed to Half-Baked Design?
Are Agile Projects Doomed to Half-Baked Design?
 
Test driven development_continuous_integration
Test driven development_continuous_integrationTest driven development_continuous_integration
Test driven development_continuous_integration
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Adopting Agile
Adopting AgileAdopting Agile
Adopting Agile
 
Mock driven development using .NET
Mock driven development using .NETMock driven development using .NET
Mock driven development using .NET
 
TDD Basics with Angular.js and Jasmine
TDD Basics with Angular.js and JasmineTDD Basics with Angular.js and Jasmine
TDD Basics with Angular.js and Jasmine
 
Test Driven Development - Overview and Adoption
Test Driven Development - Overview and AdoptionTest Driven Development - Overview and Adoption
Test Driven Development - Overview and Adoption
 
A Not-So-Serious Introduction to Test Driven Development (TDD)
A Not-So-Serious Introduction to Test Driven Development (TDD) A Not-So-Serious Introduction to Test Driven Development (TDD)
A Not-So-Serious Introduction to Test Driven Development (TDD)
 
OSS Java Analysis - What You Might Be Missing
OSS Java Analysis - What You Might Be MissingOSS Java Analysis - What You Might Be Missing
OSS Java Analysis - What You Might Be Missing
 
Loopt unit test experiences
Loopt unit test experiencesLoopt unit test experiences
Loopt unit test experiences
 
Roy Osherove on Unit Testing Good Practices and Horrible Mistakes
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 Mistakes
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Funny stories and anti-patterns from DevOps landscape
Funny stories and anti-patterns from DevOps landscapeFunny stories and anti-patterns from DevOps landscape
Funny stories and anti-patterns from DevOps landscape
 
FluentSelenium Presentation Code Camp09
FluentSelenium Presentation Code Camp09FluentSelenium Presentation Code Camp09
FluentSelenium Presentation Code Camp09
 
Unit Test + Functional Programming = Love
Unit Test + Functional Programming = LoveUnit Test + Functional Programming = Love
Unit Test + Functional Programming = Love
 

Similar to TDD Workshop UTN 2012

Test driven development and unit testing with examples in C++
Test driven development and unit testing with examples in C++Test driven development and unit testing with examples in C++
Test driven development and unit testing with examples in C++
Hong Le Van
 
Test driven development(tdd)
Test driven development(tdd)Test driven development(tdd)
Test driven development(tdd)
Omar Youssef Shiha
 
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
UTC Fire & Security
 
Test-Driven Development In Action
Test-Driven Development In ActionTest-Driven Development In Action
Test-Driven Development In Action
Jon Kruger
 
Why Unit Testingl
Why Unit TestinglWhy Unit Testingl
Why Unit Testingl
priya_trivedi
 
Why unit testingl
Why unit testinglWhy unit testingl
Why unit testingl
Priya Sharma
 
Why Unit Testingl
Why Unit TestinglWhy Unit Testingl
Why Unit Testingl
priya_trivedi
 
Unit testing & TDD concepts with best practice guidelines.
Unit testing & TDD concepts with best practice guidelines.Unit testing & TDD concepts with best practice guidelines.
Unit testing & TDD concepts with best practice guidelines.
Mohamed Taman
 
TDD Best Practices
TDD Best PracticesTDD Best Practices
TDD Best Practices
Attila Bertók
 
TDD - Agile
TDD - Agile TDD - Agile
TDD - Agile
harinderpisces
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
Anuj Arora
 
Testing 101
Testing 101Testing 101
Testing 101
Noam Barkai
 
An introduction to unit testing
An introduction to unit testingAn introduction to unit testing
An introduction to unit testing
Adam Stephensen
 
Why test with flex unit
Why test with flex unitWhy test with flex unit
Why test with flex unit
michael.labriola
 
Unit Testing to Support Reusable for Component-Based Software Engineering
Unit Testing to Support Reusable for Component-Based Software EngineeringUnit Testing to Support Reusable for Component-Based Software Engineering
Unit Testing to Support Reusable for Component-Based Software Engineering
ijtsrd
 
Unit Testing in Flutter - From Workflow Essentials to Complex Scenarios
Unit Testing in Flutter - From Workflow Essentials to Complex ScenariosUnit Testing in Flutter - From Workflow Essentials to Complex Scenarios
Unit Testing in Flutter - From Workflow Essentials to Complex Scenarios
Flutter Agency
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
Sergey Aganezov
 
Unit testing
Unit testingUnit testing
Unit testing
Murugesan Nataraj
 
An Introduction to Test Driven Development
An Introduction to Test Driven Development An Introduction to Test Driven Development
An Introduction to Test Driven Development
CodeOps Technologies LLP
 

Similar to TDD Workshop UTN 2012 (20)

Test driven development and unit testing with examples in C++
Test driven development and unit testing with examples in C++Test driven development and unit testing with examples in C++
Test driven development and unit testing with examples in C++
 
Test driven development(tdd)
Test driven development(tdd)Test driven development(tdd)
Test driven development(tdd)
 
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
 
Test-Driven Development In Action
Test-Driven Development In ActionTest-Driven Development In Action
Test-Driven Development In Action
 
Why Unit Testingl
Why Unit TestinglWhy Unit Testingl
Why Unit Testingl
 
Why unit testingl
Why unit testinglWhy unit testingl
Why unit testingl
 
Why Unit Testingl
Why Unit TestinglWhy Unit Testingl
Why Unit Testingl
 
Unit testing & TDD concepts with best practice guidelines.
Unit testing & TDD concepts with best practice guidelines.Unit testing & TDD concepts with best practice guidelines.
Unit testing & TDD concepts with best practice guidelines.
 
TDD Best Practices
TDD Best PracticesTDD Best Practices
TDD Best Practices
 
TDD - Agile
TDD - Agile TDD - Agile
TDD - Agile
 
TestDrivenDeveloment
TestDrivenDevelomentTestDrivenDeveloment
TestDrivenDeveloment
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Testing 101
Testing 101Testing 101
Testing 101
 
An introduction to unit testing
An introduction to unit testingAn introduction to unit testing
An introduction to unit testing
 
Why test with flex unit
Why test with flex unitWhy test with flex unit
Why test with flex unit
 
Unit Testing to Support Reusable for Component-Based Software Engineering
Unit Testing to Support Reusable for Component-Based Software EngineeringUnit Testing to Support Reusable for Component-Based Software Engineering
Unit Testing to Support Reusable for Component-Based Software Engineering
 
Unit Testing in Flutter - From Workflow Essentials to Complex Scenarios
Unit Testing in Flutter - From Workflow Essentials to Complex ScenariosUnit Testing in Flutter - From Workflow Essentials to Complex Scenarios
Unit Testing in Flutter - From Workflow Essentials to Complex Scenarios
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Unit testing
Unit testingUnit testing
Unit testing
 
An Introduction to Test Driven Development
An Introduction to Test Driven Development An Introduction to Test Driven Development
An Introduction to Test Driven Development
 

Recently uploaded

Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Jeffrey Haguewood
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
Bhaskar Mitra
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
RTTS
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
CatarinaPereira64715
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
Abida Shariff
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
Cheryl Hung
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Product School
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
Frank van Harmelen
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 

Recently uploaded (20)

Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
ODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User GroupODC, Data Fabric and Architecture User Group
ODC, Data Fabric and Architecture User Group
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 

TDD Workshop UTN 2012

  • 1. Test Driven Development Ing. Facundo Farias facundo.farias@gmail.com November, 2012
  • 2. Agenda Introduction to Unit Testing.  Introduction to TDD.  How it works? Steps.  Mock Objects.  Dependency Injection  Characteristics  Benefits 
  • 3. What is it?  Is an advanced technique of using automated unit tests to drive the design of software and force decoupling of dependencies.  Introduced by Kent Beck as part of XP (Extreme Programming).
  • 4. What is a Unit Test?  In computer programming, unit testing is a method by which individual units of source code, sets of one or more computer program modules together with associated control data, usage procedures, and operating procedures, are tested to determine if they are fit for use.
  • 5. What is a Unit Test? (Cont.) /// <summary> /// The sum should work using two integers. /// </summary> [TestMethod] public void SumShouldWorkUsingTwoIntegers() { // Given int numberA = 50; int numberB = 100; // When int sum = numberA + numberB; // Then Assert.AreEqual(sum, 150); }
  • 7. Steps (I)   Understand the requirements of the story, work item, or feature that you are working on. RED: Create a test and make it fail. ◦ Imagine how the new code should be called and write the test as if the code already existed. ◦ Create the new production code stub. Write just enough code so that it compiles. ◦ Run the test. It should fail.
  • 8. Steps (II)  GREEN: Make the test pass by any means necessary. ◦ Write the production code to make the test pass. Keep it simple. ◦ If you've written the code so that the test passes as intended, you are finished. You do not have to write more code speculatively. Make this one test pass and continue. ◦ When the test passes, you might want to run all tests up to this point to build confidence that everything else is still working.
  • 9. Steps (III)  REFACTOR: Change the code to remove duplication in your project and to improve the design while ensuring that all tests still pass. ◦ Remove duplication caused by the addition of the new functionality. ◦ Make design changes to improve the overall solution. ◦ After each refactoring, rerun all the tests to ensure that they all still pass.  Repeat the cycle. Each cycle should be very short, and a typical hour should contain many Red/Green/Refactor cycles.
  • 10. Unit Testing Tools  Each programming language has different tools to use TDD: ◦ ◦ ◦ ◦ ◦ ◦ ◦ Junit, Mstest, Nunit, PHPUnit, Sunit, PyUnit, Unittest, CppUnit, CxxTest, Etc
  • 11. Mock Objects   Mock Objects are simulated objects that mimic the behavior of real objects in controlled ways. A programmer typically creates a mock object to test the behavior of some other object, in much the same way that a car designer uses a crash test dummy to simulate the dynamic behavior of a human in vehicle impacts. Frameworks: Mockito, EasyMock, Jmockit, RhinoMocks, Nmock, etc.
  • 12. Dependency Injection Dependency Injection is a software design pattern that allows a choice of component to be made at run-time rather than compile time. This can be used, for example, as a simple way to load plugins dynamically or to choose mock objects in test environments vs. real objects in production environments. This software design pattern injects the dependent element (object or value etc.) to the destination automatically by knowing the requirement of the destination.  Frameworks: Spring, Spring.net, Ninject, Unity, etc. 
  • 13. Characteristics       Runs fast, runs fast, runs fast. If the tests are slow, they will not be run often. Separates or simulates environmental dependencies such as databases, file systems, networks, queues, and so on. Tests that exercise these will not run fast, and a failure does not give meaningful feedback about what the problem actually is. Is very limited in scope. If the test fails, it's obvious where to look for the problem. Use few Assert calls so that the offending code is obvious. It's important to only test one thing in a single test. Runs and passes in isolation. If the tests require special environmental setup or fail unexpectedly, then they are not good unit tests. Change them for simplicity and reliability. Tests should run and pass on any machine. The "works on my box" excuse doesn't work. Often uses stubs and mock objects. If the code being tested typically calls out to a database or file system, these dependencies must be simulated, or mocked. These dependencies will ordinarily be abstracted away by using interfaces. Clearly reveals its intention. Another developer can look at the test and understand what is expected of the production code.
  • 14. Benefits  The suite of unit tests provides constant feedback that each component is still working.  The unit tests act as documentation that cannot go out-of-date, unlike separate documentation, which can and frequently does.  When the test passes and the production code is refactored to remove duplication, it is clear that the code is finished, and the developer can move on to a new test.  Test-driven development forces critical analysis and design because the developer cannot create the production code without truly understanding what the desired result should be and how to test it.  The software tends to be better designed, that is, loosely coupled and easily maintainable, because the developer is free to make design decisions and refactor at any time with confidence that the software is still working. This confidence is gained by running the tests. The need for a design pattern may emerge, and the code can be changed at that time.  The test suite acts as a regression safety net on bugs: If a bug is found, the developer should create a test to reveal the bug and then modify the production code so that the bug goes away and all other tests still pass. On each successive test run, all previous bug fixes are verified.
  • 15. References         Test Driven Development: By Example [Kent Beck] Unit testing [Wikipedia] Guidelines for Test-Driven Development Using mock objects for complex unit tests Mocks Aren't Stubs [Martin Fowler] Dependency Injection [Martin Fowler] Spring for Java Spring for c#
  • 16. Q&A