SlideShare a Scribd company logo
Test Driven Development
Dmitry Savin
Introduction
• TDD is an advanced technique of using automated
unit tests to drive the design of software and force
decoupling of dependencies.
• The result of using this practice is a comprehensive
suite of unit tests that can be run at any time to
provide feedback that the software is still working.
• Was invented by Kent Beck at 1999.
TDD cycle
• Write a test that fails.
• Write just enough code to pass the test.
• Clean up code (remove duplication,
improve the design) and ensure that all
tests still pass.
Write a test that fails
• 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. By this you ensure that test is calling the
correct code and that the code is not working by accident.
Note
Now we have the goal - make this test pass.

This helps us concentrate our attention on one particular task.
Write just enough code to pass the test
• Write the production code to make the test pass. Keep it simple.
• You can hard-coding of the expected return value first to verify
that the test correctly detects success.
• If you've written the code so that the test passes as intended, you
are finished.
• The test is the objective definition of “done.”
• The phrase "You Ain't Gonna Need It" (YAGNI) is often used to
veto unnecessary work. If new functionality is still needed, then
another test is needed. Make this one test pass and continue.
Clean up code and ensure 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.
Process Example
• Follow these steps (slight variations exist among TDD practitioners):
• Understand the requirements of the story, work item, or feature that you are working
on.
• While working on task you can edit your test list - it is list of tests which you want to
create.
• Red: Create a test and make it fail.
• Green: Make the test pass by any means necessary.
• Refactor: Clean up code and ensure that all tests still pass.
• Repeat the cycle. Each cycle should be very short, and a typical hour should contain
many Red/Green/Refactor cycles.
Benefits of TDD
• 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 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.
• Reduced debugging time!
Characteristics of a Good Unit Test
• Runs fast.
• If the tests are slow, they will not be run often.
• Separates or simulates environment dependencies such as databases, file systems, networks,
queues, and so on.
• Otherwise they will not run fast, and a failure does not give meaningful feedback about what the
problem actually is.
• Use stubs and mock objects.
• 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 broken part of code is obvious.
• It's important to only test one thing in a single test.
• Tests should run and pass on any machine.
• Clearly reveals its intention.
• Another developer can look at the test and understand what is expected of the production code.
Demo
Game “Fifteen Puzzle"
References
• Microsoft: Guidelines for Test-Driven Development
• https://msdn.microsoft.com/en-us/library/aa730844%28v=vs.
80%29.aspx
• MCE 2014: Jon Reid - Test Driven Development for iOS (and anything)
• https://www.youtube.com/watch?v=Jzlz3Bx-NzM

More Related Content

PDF
Test Driven Development
PPTX
Empirically Detecting False Test Alarms Using Association Rules @ ICSE 2015
PPT
Understand regression testing
PPTX
Test Driven Development
PPT
4.4.2013 Software Quality - Regression Testing Automated and Manual - RFT/RQM
PPTX
Test Driven Development
PDF
Agile QA Automation process
PDF
A Concise QA Process
Test Driven Development
Empirically Detecting False Test Alarms Using Association Rules @ ICSE 2015
Understand regression testing
Test Driven Development
4.4.2013 Software Quality - Regression Testing Automated and Manual - RFT/RQM
Test Driven Development
Agile QA Automation process
A Concise QA Process

What's hot (20)

PPTX
Software Testing, Everyone's responsibility
PDF
TDD and Unit Testing in Golang
PDF
Becoming a better programmer - unit testing
PPTX
How google-tests-software
PPTX
Continous testing for grails
PPTX
Qa process 2012
PPT
Testing Attributes
PPTX
How QA engineers could affect quality?
PDF
CNUG TDD June 2014
PPT
01 software testing_introduction
PPTX
Topic production code
PDF
QA Process Overview for Firefox OS 2014
PPTX
Cost of defects
PDF
Testing automation in agile environment
PPT
Unit Testing, TDD and the Walking Skeleton
PPTX
Project Onion unit test environment
PPTX
Continuous delivery test strategies
PDF
Keynote AST 2016
PPTX
Case Study: Automated Code Reviews In A Grown SAP Application Landscape At EW...
PPTX
Regression testing
Software Testing, Everyone's responsibility
TDD and Unit Testing in Golang
Becoming a better programmer - unit testing
How google-tests-software
Continous testing for grails
Qa process 2012
Testing Attributes
How QA engineers could affect quality?
CNUG TDD June 2014
01 software testing_introduction
Topic production code
QA Process Overview for Firefox OS 2014
Cost of defects
Testing automation in agile environment
Unit Testing, TDD and the Walking Skeleton
Project Onion unit test environment
Continuous delivery test strategies
Keynote AST 2016
Case Study: Automated Code Reviews In A Grown SAP Application Landscape At EW...
Regression testing
Ad

Viewers also liked (15)

DOCX
Bmw tesis iv
PPS
Buen dia
PPT
Pauta numérica (b)
PPT
Legal Research Strategies
PPTX
Semi final presentation
PDF
Development of the Wi-Fi Offloading Business Concept within the African Marke...
PDF
01ข้อมูลและสารสนเทศ
PDF
136 Degrees Presentation
PDF
2014-08-18 - A Story
PPTX
Using puppets in MFL
PPTX
Coca-Cola TV transmitirá por primera vez para Latinoamérica: Lollapalooza Chile
PDF
Med-Line Singapore Social Media Marketing Plan in 2016
PPTX
Windows 10 A Guide to Secure Mobility in the Enterprise
Bmw tesis iv
Buen dia
Pauta numérica (b)
Legal Research Strategies
Semi final presentation
Development of the Wi-Fi Offloading Business Concept within the African Marke...
01ข้อมูลและสารสนเทศ
136 Degrees Presentation
2014-08-18 - A Story
Using puppets in MFL
Coca-Cola TV transmitirá por primera vez para Latinoamérica: Lollapalooza Chile
Med-Line Singapore Social Media Marketing Plan in 2016
Windows 10 A Guide to Secure Mobility in the Enterprise
Ad

Similar to Tdd (20)

PPTX
Week 14 Unit Testing.pptx
PPTX
Test driven development
PPTX
Test driven development
PPTX
Test driven development
PPTX
Test driven development
PPTX
Test driven development
PPTX
Test driven development
PPTX
Software Testing_A_mmmmmmmmmmmmmmmmmmmmm
PDF
TDD Workshop UTN 2012
PPTX
Test-Driven Development
PPTX
An Introduction to Unit Testing
PPTX
Unit Testing talk
PDF
PDF
Agile Testing - What is it?
PPT
Test Driven Development
PPTX
Istqb foundation level day 1
PDF
TDD and Related Techniques for Non Developers (2012)
PDF
Test-Driven Development Reference Card
PPT
Automated testing overview
PPTX
{10.0} Test Driven Development.pptx
Week 14 Unit Testing.pptx
Test driven development
Test driven development
Test driven development
Test driven development
Test driven development
Test driven development
Software Testing_A_mmmmmmmmmmmmmmmmmmmmm
TDD Workshop UTN 2012
Test-Driven Development
An Introduction to Unit Testing
Unit Testing talk
Agile Testing - What is it?
Test Driven Development
Istqb foundation level day 1
TDD and Related Techniques for Non Developers (2012)
Test-Driven Development Reference Card
Automated testing overview
{10.0} Test Driven Development.pptx

Tdd

  • 2. Introduction • TDD is an advanced technique of using automated unit tests to drive the design of software and force decoupling of dependencies. • The result of using this practice is a comprehensive suite of unit tests that can be run at any time to provide feedback that the software is still working. • Was invented by Kent Beck at 1999.
  • 3. TDD cycle • Write a test that fails. • Write just enough code to pass the test. • Clean up code (remove duplication, improve the design) and ensure that all tests still pass.
  • 4. Write a test that fails • 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. By this you ensure that test is calling the correct code and that the code is not working by accident. Note Now we have the goal - make this test pass.
 This helps us concentrate our attention on one particular task.
  • 5. Write just enough code to pass the test • Write the production code to make the test pass. Keep it simple. • You can hard-coding of the expected return value first to verify that the test correctly detects success. • If you've written the code so that the test passes as intended, you are finished. • The test is the objective definition of “done.” • The phrase "You Ain't Gonna Need It" (YAGNI) is often used to veto unnecessary work. If new functionality is still needed, then another test is needed. Make this one test pass and continue.
  • 6. Clean up code and ensure 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.
  • 7. Process Example • Follow these steps (slight variations exist among TDD practitioners): • Understand the requirements of the story, work item, or feature that you are working on. • While working on task you can edit your test list - it is list of tests which you want to create. • Red: Create a test and make it fail. • Green: Make the test pass by any means necessary. • Refactor: Clean up code and ensure that all tests still pass. • Repeat the cycle. Each cycle should be very short, and a typical hour should contain many Red/Green/Refactor cycles.
  • 8. Benefits of TDD • 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 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. • Reduced debugging time!
  • 9. Characteristics of a Good Unit Test • Runs fast. • If the tests are slow, they will not be run often. • Separates or simulates environment dependencies such as databases, file systems, networks, queues, and so on. • Otherwise they will not run fast, and a failure does not give meaningful feedback about what the problem actually is. • Use stubs and mock objects. • 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 broken part of code is obvious. • It's important to only test one thing in a single test. • Tests should run and pass on any machine. • Clearly reveals its intention. • Another developer can look at the test and understand what is expected of the production code.
  • 11. References • Microsoft: Guidelines for Test-Driven Development • https://msdn.microsoft.com/en-us/library/aa730844%28v=vs. 80%29.aspx • MCE 2014: Jon Reid - Test Driven Development for iOS (and anything) • https://www.youtube.com/watch?v=Jzlz3Bx-NzM