SlideShare a Scribd company logo
Test Driven Development
      Introduction


           Issued date: 8/29/2007
           Author: Nguyen Phuc Hai
Agenda

    Introduction


    Unit test overview


    Refactoring


    Test Driven Development approach


    Q&A

Trainer’s profile

    Over seven years experienced in software

    industry on both product development and
    out-sourcing.
    Hand-over various positions from developers,

    technical lead, project manager, technical
    architecture and program/product managers.
Trainer’s profile (cont.)

    Be experienced in managing small, medium

    and large projects/program – Took oversight
    more than 100 associates in one of the
    biggest account in company
    Be experienced on technical design,

    technical management in various sizes of
    projects from small to enterprise applications
    on both desktop and web environment
Trainer’s profile

    Be recognized as an Agile expert that has

    experienced in managing Agile process,
    applying Agile practices.
    Maintain the blog that share experienced in

    software development on both technical and
    management areas (http://
    www.haiphucnguyen.net/blog/)
Unit test key ideas
Unit test - FAQs

    Check the link for some common concerns of

    applying unit test and its benefits:
    http://www.haiphucnguyen.net/blog/?p=40
Unit test best practices

    Keep test method as simple as possible:

        DRY principle (Don’t repeat yourself)
    –

        Re-use test set up across test cases: put non-
    –
        trivial test data in test set up method
        Clean test code as productive code
    –

    Evident data


    All non-trivial methods must be passed unit

    test with 100% coverage ratio.
Unit test best practices

    Unit test independent with:

        Other unit tests, order of unit test running
    –

        Environment (operating system, hardware
    –
        configuration, database …)
    Fix broken test as soon as possible (it should

    be immediately)
    After fixing defect, write unit test to prevent it

    re-occur
Unit test best practices

    All test cases must be passed at the end of

    working day.
    Test project code, not library code


    Minimal maintenance of test code


    If you can not write unit test for some

    functionality that means your code has
    problem! Refactor it immediately
How to make code becomes testable

    Using interface in communication among

    layers, modules.
    Dependency injection


    Dependency lookup


    Methods/Constructors has as less

    parameters as possible (4 is the best
    number)
How to make code becomes testable

    Class is not depended in many other classes

    -> it is hard to initiate instance of this class
    One method has only one purpose


    Be careful with singleton class or static

    methods!
    And the most important: refactoring your

    code if it is not testable (esp. legacy code)
Refactoring

    Refactoring is a disciplined technique for

    restructuring an existing body of code,
    altering its internal structure without changing
    its external behavior. The system is also kept
    fully working after each small refactoring,
    reducing the chances that a system can get
    seriously broken during the restructuring.
Refactoring (cont.)

    Whenever you think you can make more

    benefits with your new code:
        Increase the re-usability
    –

        Make the code becomes testable
    –

        Avoid the rigid, fragile issues
    –

        Better performance
    –

        …
    –
Refactoring – best practices

    Keep small change running before proceed

    another change.
    Unit test should be written to make sure

    refactoring does not change system’s
    behaviors.
    To legacy system: write test first, refactoring

    and verify result
Test Driven Development

    TDD is a technique to improve the quality of

    both code and design. Developers will write
    the test first, then writing the code to make it
    pass the test. They can refactor code to
    avoid issues (duplication or non-optimize
    code …)
TDD benefits

    Promoting high code coverage for productive

    code.
    Improving quality of architecture and code


    Prevent defects cost is less than detect

    defects cost
TDD Cycle

    Prepare a list of test cases


    Follow the TDD Rhythm:

        Pick a test to implement
    –

        Write a fail test
    –

        Quickly make test green
    –

        Refactor to eliminate duplication
    –
TDD – Things always do

    Write test-first


    Maintain to-do list


    Writing new code only if there is failing

    automated test
    Run all tests all the time not only single

    isolated test
TDD – Things always do

    Job is done whenever complete

    implementation and all test have green status
TDD – Things never do

    Write test for trivial methods (such as

    getters/setters)
    Commit code with failed test


    Big upfront design and code that makes test

    first do not cover all cases of writing code
TDD best practices

    Write the simple test first, then write code.

    After that write test code for more complexity
    functionality
    Run test immediately whenever complete

    writing code
    If spending much time for writing code, break

    the functionality into smaller ones and
    applying TDD for these pieces
TDD best practices

    Whenever test code or productive code

    smells, refactor it immediately
    After writing code, use coverage tool to make

    sure productive code is covered 100% by
    test method. If some piece of code is not
    covered, your implementation has problem!
    Use mock objects if needed

Tools support TDD

    Various tools help developers easier to do

    TDD:
        IDE: Eclipse, MS Visual Studio Team System
    –

        Build tools: Ant, Maven, Nant
    –

        xUnit tools: Junit, TestNG, DBUnit, Nunit, …
    –

        Coverage tools: JCoverage, NCoverage
    –
TDD examples

    A user story of login page:

        Presentation Layer: Login.html, Login.java
    –

        Service Layer: LoginService.java
    –

        Data Access Layer: LoginDAO.java
    –

    More complex user story: applying acegi for

    authentication – authorization process:
        Re-factor LoginService.java
    –

        Run test again
    –
TDD examples (cont.)

    Write the mock up html file: Login.html


    Write test class: LoginTest.java


    Write test method for case user login

    success:
    @Test
    Public void testLoginSuccess() {
      ….
    }
TDD examples (cont.)

    Implement login function that pass the test

    Void onSubmit() {
      if (username.equals(“VietNam”)) {
          setResponsePage(Helloworld.class);
      }
      else {
          form.error(“Login fail”);
      }
    }
TDD examples (cont.)

    Run LoginTest again and verify result (it has

    green color)
    Return the Login.java and add feature

    verifying user base on database
    Void onSubmit() {
       if (userService.getUser(userName) != null) {
           …
       }
    }
TDD examples (cont.)

    The enterprise application that maintains the

    suite of test cases and number of test cases
    is growing daily:
        Automation unit test
    –

        Continuous Integration
    –
TDD examples (cont.)
TDD examples (cont.)
Q&A

    Any question of unit test, refactoring and

    TDD is welcome 
Thank you

    I hope you enjoy with this course. Any further

    discussion, please contact me at
        Email: hainguyen@esofthead.com
    –

        My blog: http://blog.esofthead.com
    –

More Related Content

What's hot

ATDD in Practice
ATDD in PracticeATDD in Practice
ATDD in Practice
Steven Mak
 
TDD and BDD and ATDD
TDD and BDD and ATDDTDD and BDD and ATDD
TDD and BDD and ATDD
Anuar Nurmakanov
 
TDD (Test Driven Design)
TDD (Test Driven Design)TDD (Test Driven Design)
TDD (Test Driven Design)
nedirtv
 
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
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
Dhaval Dalal
 
TDD - Test Driven Development
TDD - Test Driven DevelopmentTDD - Test Driven Development
TDD - Test Driven Development
Tung Nguyen Thanh
 
TDD - Agile
TDD - Agile TDD - Agile
TDD - Agile
harinderpisces
 
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
 
Test Driven Development (TDD) Preso 360|Flex 2010
Test Driven Development (TDD) Preso 360|Flex 2010Test Driven Development (TDD) Preso 360|Flex 2010
Test Driven Development (TDD) Preso 360|Flex 2010
guest5639fa9
 
iOS Test-Driven Development
iOS Test-Driven DevelopmentiOS Test-Driven Development
iOS Test-Driven Development
Pablo Villar
 
ATDD in practice
ATDD in practiceATDD in practice
ATDD in practice
Andrei Marukovich
 
TDD vs. ATDD - What, Why, Which, When & Where
TDD vs. ATDD - What, Why, Which, When & WhereTDD vs. ATDD - What, Why, Which, When & Where
TDD vs. ATDD - What, Why, Which, When & Where
Daniel Davis
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
guestc8093a6
 
Behavior Driven Development with SpecFlow
Behavior Driven Development with SpecFlowBehavior Driven Development with SpecFlow
Behavior Driven Development with SpecFlow
Rachid Kherrazi
 
QA Fest 2017. Владимир Примаков. QA метрики. Взгляд на качество с разных стор...
QA Fest 2017. Владимир Примаков. QA метрики. Взгляд на качество с разных стор...QA Fest 2017. Владимир Примаков. QA метрики. Взгляд на качество с разных стор...
QA Fest 2017. Владимир Примаков. QA метрики. Взгляд на качество с разных стор...
QAFest
 
Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...
Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...
Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...
Zohirul Alam Tiemoon
 
TDD Flow: The Mantra in Action
TDD Flow: The Mantra in ActionTDD Flow: The Mantra in Action
TDD Flow: The Mantra in Action
Dionatan default
 
Software Quality via Unit Testing
Software Quality via Unit TestingSoftware Quality via Unit Testing
Software Quality via Unit Testing
Shaun Abram
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
ZendCon
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
Sergey Aganezov
 

What's hot (20)

ATDD in Practice
ATDD in PracticeATDD in Practice
ATDD in Practice
 
TDD and BDD and ATDD
TDD and BDD and ATDDTDD and BDD and ATDD
TDD and BDD and ATDD
 
TDD (Test Driven Design)
TDD (Test Driven Design)TDD (Test Driven Design)
TDD (Test Driven Design)
 
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)
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
TDD - Test Driven Development
TDD - Test Driven DevelopmentTDD - Test Driven Development
TDD - Test Driven Development
 
TDD - Agile
TDD - Agile TDD - Agile
TDD - Agile
 
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.
 
Test Driven Development (TDD) Preso 360|Flex 2010
Test Driven Development (TDD) Preso 360|Flex 2010Test Driven Development (TDD) Preso 360|Flex 2010
Test Driven Development (TDD) Preso 360|Flex 2010
 
iOS Test-Driven Development
iOS Test-Driven DevelopmentiOS Test-Driven Development
iOS Test-Driven Development
 
ATDD in practice
ATDD in practiceATDD in practice
ATDD in practice
 
TDD vs. ATDD - What, Why, Which, When & Where
TDD vs. ATDD - What, Why, Which, When & WhereTDD vs. ATDD - What, Why, Which, When & Where
TDD vs. ATDD - What, Why, Which, When & Where
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Behavior Driven Development with SpecFlow
Behavior Driven Development with SpecFlowBehavior Driven Development with SpecFlow
Behavior Driven Development with SpecFlow
 
QA Fest 2017. Владимир Примаков. QA метрики. Взгляд на качество с разных стор...
QA Fest 2017. Владимир Примаков. QA метрики. Взгляд на качество с разных стор...QA Fest 2017. Владимир Примаков. QA метрики. Взгляд на качество с разных стор...
QA Fest 2017. Владимир Примаков. QA метрики. Взгляд на качество с разных стор...
 
Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...
Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...
Overview on TDD (Test Driven Development) & ATDD (Acceptance Test Driven Deve...
 
TDD Flow: The Mantra in Action
TDD Flow: The Mantra in ActionTDD Flow: The Mantra in Action
TDD Flow: The Mantra in Action
 
Software Quality via Unit Testing
Software Quality via Unit TestingSoftware Quality via Unit Testing
Software Quality via Unit Testing
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 

Similar to Test Driven Development Introduction

Python and test
Python and testPython and test
Python and test
Micron Technology
 
Unit Testing and TDD 2017
Unit Testing and TDD 2017Unit Testing and TDD 2017
Unit Testing and TDD 2017
Xavi Hidalgo
 
Tdd
TddTdd
Test Driven Development with Sql Server
Test Driven Development with Sql ServerTest Driven Development with Sql Server
Test Driven Development with Sql Server
David P. Moore
 
Testing Sap: Modern Methodology
Testing Sap: Modern MethodologyTesting Sap: Modern Methodology
Testing Sap: Modern Methodology
Ethan Jewett
 
TDD Best Practices
TDD Best PracticesTDD Best Practices
TDD Best Practices
Attila Bertók
 
Test driven development
Test driven developmentTest driven development
Test driven development
lukaszkujawa
 
Agile testing
Agile testingAgile testing
Agile testing
Raj Indugula
 
www.tutorialsbook.com presents Manual testing
www.tutorialsbook.com presents Manual testingwww.tutorialsbook.com presents Manual testing
www.tutorialsbook.com presents Manual testing
Tutorials Book
 
Lecture 21
Lecture 21Lecture 21
Lecture 21
Skillspire LLC
 
TDD Workshop UTN 2012
TDD Workshop UTN 2012TDD Workshop UTN 2012
TDD Workshop UTN 2012
Facundo Farias
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
Md. Enamul Haque Chowdhury
 
Unit testing, UI testing and Test Driven Development in Visual Studio 2012
Unit testing, UI testing and Test Driven Development in Visual Studio 2012Unit testing, UI testing and Test Driven Development in Visual Studio 2012
Unit testing, UI testing and Test Driven Development in Visual Studio 2012
Jacinto Limjap
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
Anuj Arora
 
Introduction to Test Driven Development
Introduction to Test Driven DevelopmentIntroduction to Test Driven Development
Introduction to Test Driven Development
Michael Denomy
 
Mercury Testdirector8.0 using Slides
Mercury Testdirector8.0 using SlidesMercury Testdirector8.0 using Slides
Mercury Testdirector8.0 using Slides
telab
 
Unit tests and TDD
Unit tests and TDDUnit tests and TDD
Unit tests and TDD
Roman Okolovich
 
Automated Software Testing Framework Training by Quontra Solutions
Automated Software Testing Framework Training by Quontra SolutionsAutomated Software Testing Framework Training by Quontra Solutions
Automated Software Testing Framework Training by Quontra Solutions
Quontra Solutions
 
Test-Driven Development In Action
Test-Driven Development In ActionTest-Driven Development In Action
Test-Driven Development In Action
Jon Kruger
 
Behavior Driven Development—A Guide to Agile Practices by Josh Eastman
Behavior Driven Development—A Guide to Agile Practices by Josh EastmanBehavior Driven Development—A Guide to Agile Practices by Josh Eastman
Behavior Driven Development—A Guide to Agile Practices by Josh Eastman
QA or the Highway
 

Similar to Test Driven Development Introduction (20)

Python and test
Python and testPython and test
Python and test
 
Unit Testing and TDD 2017
Unit Testing and TDD 2017Unit Testing and TDD 2017
Unit Testing and TDD 2017
 
Tdd
TddTdd
Tdd
 
Test Driven Development with Sql Server
Test Driven Development with Sql ServerTest Driven Development with Sql Server
Test Driven Development with Sql Server
 
Testing Sap: Modern Methodology
Testing Sap: Modern MethodologyTesting Sap: Modern Methodology
Testing Sap: Modern Methodology
 
TDD Best Practices
TDD Best PracticesTDD Best Practices
TDD Best Practices
 
Test driven development
Test driven developmentTest driven development
Test driven development
 
Agile testing
Agile testingAgile testing
Agile testing
 
www.tutorialsbook.com presents Manual testing
www.tutorialsbook.com presents Manual testingwww.tutorialsbook.com presents Manual testing
www.tutorialsbook.com presents Manual testing
 
Lecture 21
Lecture 21Lecture 21
Lecture 21
 
TDD Workshop UTN 2012
TDD Workshop UTN 2012TDD Workshop UTN 2012
TDD Workshop UTN 2012
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Unit testing, UI testing and Test Driven Development in Visual Studio 2012
Unit testing, UI testing and Test Driven Development in Visual Studio 2012Unit testing, UI testing and Test Driven Development in Visual Studio 2012
Unit testing, UI testing and Test Driven Development in Visual Studio 2012
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Introduction to Test Driven Development
Introduction to Test Driven DevelopmentIntroduction to Test Driven Development
Introduction to Test Driven Development
 
Mercury Testdirector8.0 using Slides
Mercury Testdirector8.0 using SlidesMercury Testdirector8.0 using Slides
Mercury Testdirector8.0 using Slides
 
Unit tests and TDD
Unit tests and TDDUnit tests and TDD
Unit tests and TDD
 
Automated Software Testing Framework Training by Quontra Solutions
Automated Software Testing Framework Training by Quontra SolutionsAutomated Software Testing Framework Training by Quontra Solutions
Automated Software Testing Framework Training by Quontra Solutions
 
Test-Driven Development In Action
Test-Driven Development In ActionTest-Driven Development In Action
Test-Driven Development In Action
 
Behavior Driven Development—A Guide to Agile Practices by Josh Eastman
Behavior Driven Development—A Guide to Agile Practices by Josh EastmanBehavior Driven Development—A Guide to Agile Practices by Josh Eastman
Behavior Driven Development—A Guide to Agile Practices by Josh Eastman
 

Test Driven Development Introduction

  • 1. Test Driven Development Introduction Issued date: 8/29/2007 Author: Nguyen Phuc Hai
  • 2. Agenda Introduction  Unit test overview  Refactoring  Test Driven Development approach  Q&A 
  • 3. Trainer’s profile Over seven years experienced in software  industry on both product development and out-sourcing. Hand-over various positions from developers,  technical lead, project manager, technical architecture and program/product managers.
  • 4. Trainer’s profile (cont.) Be experienced in managing small, medium  and large projects/program – Took oversight more than 100 associates in one of the biggest account in company Be experienced on technical design,  technical management in various sizes of projects from small to enterprise applications on both desktop and web environment
  • 5. Trainer’s profile Be recognized as an Agile expert that has  experienced in managing Agile process, applying Agile practices. Maintain the blog that share experienced in  software development on both technical and management areas (http:// www.haiphucnguyen.net/blog/)
  • 7. Unit test - FAQs Check the link for some common concerns of  applying unit test and its benefits: http://www.haiphucnguyen.net/blog/?p=40
  • 8. Unit test best practices Keep test method as simple as possible:  DRY principle (Don’t repeat yourself) – Re-use test set up across test cases: put non- – trivial test data in test set up method Clean test code as productive code – Evident data  All non-trivial methods must be passed unit  test with 100% coverage ratio.
  • 9. Unit test best practices Unit test independent with:  Other unit tests, order of unit test running – Environment (operating system, hardware – configuration, database …) Fix broken test as soon as possible (it should  be immediately) After fixing defect, write unit test to prevent it  re-occur
  • 10. Unit test best practices All test cases must be passed at the end of  working day. Test project code, not library code  Minimal maintenance of test code  If you can not write unit test for some  functionality that means your code has problem! Refactor it immediately
  • 11. How to make code becomes testable Using interface in communication among  layers, modules. Dependency injection  Dependency lookup  Methods/Constructors has as less  parameters as possible (4 is the best number)
  • 12. How to make code becomes testable Class is not depended in many other classes  -> it is hard to initiate instance of this class One method has only one purpose  Be careful with singleton class or static  methods! And the most important: refactoring your  code if it is not testable (esp. legacy code)
  • 13. Refactoring Refactoring is a disciplined technique for  restructuring an existing body of code, altering its internal structure without changing its external behavior. The system is also kept fully working after each small refactoring, reducing the chances that a system can get seriously broken during the restructuring.
  • 14. Refactoring (cont.) Whenever you think you can make more  benefits with your new code: Increase the re-usability – Make the code becomes testable – Avoid the rigid, fragile issues – Better performance – … –
  • 15. Refactoring – best practices Keep small change running before proceed  another change. Unit test should be written to make sure  refactoring does not change system’s behaviors. To legacy system: write test first, refactoring  and verify result
  • 16. Test Driven Development TDD is a technique to improve the quality of  both code and design. Developers will write the test first, then writing the code to make it pass the test. They can refactor code to avoid issues (duplication or non-optimize code …)
  • 17. TDD benefits Promoting high code coverage for productive  code. Improving quality of architecture and code  Prevent defects cost is less than detect  defects cost
  • 18. TDD Cycle Prepare a list of test cases  Follow the TDD Rhythm:  Pick a test to implement – Write a fail test – Quickly make test green – Refactor to eliminate duplication –
  • 19. TDD – Things always do Write test-first  Maintain to-do list  Writing new code only if there is failing  automated test Run all tests all the time not only single  isolated test
  • 20. TDD – Things always do Job is done whenever complete  implementation and all test have green status
  • 21. TDD – Things never do Write test for trivial methods (such as  getters/setters) Commit code with failed test  Big upfront design and code that makes test  first do not cover all cases of writing code
  • 22. TDD best practices Write the simple test first, then write code.  After that write test code for more complexity functionality Run test immediately whenever complete  writing code If spending much time for writing code, break  the functionality into smaller ones and applying TDD for these pieces
  • 23. TDD best practices Whenever test code or productive code  smells, refactor it immediately After writing code, use coverage tool to make  sure productive code is covered 100% by test method. If some piece of code is not covered, your implementation has problem! Use mock objects if needed 
  • 24. Tools support TDD Various tools help developers easier to do  TDD: IDE: Eclipse, MS Visual Studio Team System – Build tools: Ant, Maven, Nant – xUnit tools: Junit, TestNG, DBUnit, Nunit, … – Coverage tools: JCoverage, NCoverage –
  • 25. TDD examples A user story of login page:  Presentation Layer: Login.html, Login.java – Service Layer: LoginService.java – Data Access Layer: LoginDAO.java – More complex user story: applying acegi for  authentication – authorization process: Re-factor LoginService.java – Run test again –
  • 26. TDD examples (cont.) Write the mock up html file: Login.html  Write test class: LoginTest.java  Write test method for case user login  success: @Test Public void testLoginSuccess() { …. }
  • 27. TDD examples (cont.) Implement login function that pass the test  Void onSubmit() { if (username.equals(“VietNam”)) { setResponsePage(Helloworld.class); } else { form.error(“Login fail”); } }
  • 28. TDD examples (cont.) Run LoginTest again and verify result (it has  green color) Return the Login.java and add feature  verifying user base on database Void onSubmit() { if (userService.getUser(userName) != null) { … } }
  • 29. TDD examples (cont.) The enterprise application that maintains the  suite of test cases and number of test cases is growing daily: Automation unit test – Continuous Integration –
  • 32. Q&A Any question of unit test, refactoring and  TDD is welcome 
  • 33. Thank you I hope you enjoy with this course. Any further  discussion, please contact me at Email: hainguyen@esofthead.com – My blog: http://blog.esofthead.com –