SlideShare ist ein Scribd-Unternehmen logo
1 von 16
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Was ist angesagt?

Introduction to test programming
Introduction to test programmingIntroduction to test programming
Introduction to test programmingopenfinanceDev
 
Project Onion unit test environment
Project Onion unit test environmentProject Onion unit test environment
Project Onion unit test environmentAbhinav 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 ReviewsAlan 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_integrationhaochenglee
 
Adopting Agile
Adopting AgileAdopting Agile
Adopting AgileCoverity
 
Mock driven development using .NET
Mock driven development using .NETMock driven development using .NET
Mock driven development using .NETPuneet Ghanshani
 
Test Driven Development - Overview and Adoption
Test Driven Development - Overview and AdoptionTest Driven Development - Overview and Adoption
Test Driven Development - Overview and AdoptionPyxis 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 MissingCoverity
 
Loopt unit test experiences
Loopt unit test experiencesLoopt unit test experiences
Loopt unit test experiencesHeine 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 developmentNascenia 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 landscapeMikalai Alimenkou
 
FluentSelenium Presentation Code Camp09
FluentSelenium Presentation Code Camp09FluentSelenium Presentation Code Camp09
FluentSelenium Presentation Code Camp09Pyxis Technologies
 
Unit Test + Functional Programming = Love
Unit Test + Functional Programming = LoveUnit Test + Functional Programming = Love
Unit Test + Functional Programming = LoveAlvaro Videla
 

Was ist angesagt? (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
 

Ähnlich wie 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 in .Net - 2010 + Eclipse
Test driven development in .Net - 2010 + EclipseTest driven development in .Net - 2010 + Eclipse
Test driven development in .Net - 2010 + EclipseUTC Fire & Security
 
Test-Driven Development In Action
Test-Driven Development In ActionTest-Driven Development In Action
Test-Driven Development In ActionJon Kruger
 
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
 
An introduction to unit testing
An introduction to unit testingAn introduction to unit testing
An introduction to unit testingAdam Stephensen
 
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 Engineeringijtsrd
 
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 ScenariosFlutter Agency
 
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
 

Ähnlich wie 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
 

Kürzlich hochgeladen

From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 

Kürzlich hochgeladen (20)

From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 

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