27 September 2021

Test Driven Development (TDD)

Test Driven Development (TDD) definition

  • TDD is one of the most important concepts in Agile, TDD is a test-first software development practice in which test cases are defined and created first, and executable code is created to make the test pass.
    • Test Driven Development is a technique where Developers develop a test case for each desired behavior of a unit of work and then extend the implementation to reflect this behavior.
    • Test Driven Development is a predictable, emergent, incremental and emergent Software development approach / technique which relies on Automated Test.

Test Driven Development (TDD) purpose

  • It can help to write cleaner code by emphasizing refactoring and it will decrease the risk of bugs.
    • But the practice itself will not guarantee these outcomes, it still has to be applied correctly and needs skilled developers to achieve good results.

Test Driven Development (TDD) benefits

  • It makes team collaboration easier and more efficient :
    • Makes collaboration easier and more efficient, team members can edit each other’s code with confidence because the tests will inform them if the changes are making the code behave in unexpected ways.
  • It helps to clarify requirements :
    • It helps to clarify requirements because you have to figure out concretely what inputs you have to feed and what outputs you expect.
  • It forces for good architecture :
    • TDD also forces good architecture.
    • In order to make your code unit‐testable, it must be properly modularized.
    • Writing the tests first, various architectural problems tend to surface earlier.
  • It improves the design :
    • It encourages small steps and improves the design because it makes you cut the unnecessary dependencies to facilitate the setup.
  • It promotes good design and separation of concerns :
    • It speeds the overall development process.
  • It causes you to construct a test harness that can be automated :
    • The Test exists before the code is written thus making it act as a requirement.
    • As soon as the Test is passed, the requirement is met.
    • Since the test exists before the code that makes it pass, the test acts as a requirement of the system under test.
  • It forces your code be more modular :
    • Because you are writing small tests at a time, it forces your code to be more modular (otherwise they’d be hard to test against).
    • TDD helps you learn, understand, and internalize the key principles of good modular design.
  • It forces you to try to make your interfaces clean enough to be tested :
    • Testing while writing also forces you to try to make your interfaces clean enough to be tested.
    • It’s sometimes hard to see the advantage of this until you work on a body of code where it wasn’t done, and the only way to exercise and focus on a given piece of code is to run the whole system and set a break‐point.
    • It helps prevent defects Helps prevent defects – well, at least it helps you find design or requirement issues right at the beginning.
    • TDD provides early warning to design problems (when they are easier to fix).
  • It improves quality and reduces bugs :
    • TDD helps reducing the defects in code.
    • However it does not reduce the defects completely.
  • It helps programmers really understand their code.
  • It forces for good code documentation :
    • Documents your code better than documentation (it doesn’t go out of date since you’re running it all the time).
  • It creates an automated regression test suite :
    • Creates an automated regression test suite, basically for free. i.e. you don’t need to spend time afterward writing unit tests to test the implementation code.
    • TDD is one of the most important concepts in Agile that creates an automated regression test suite, basically for free, i.e. you don’t need to spend time afterward writing unit tests to test the implemented code.
  • It helps find stupid mistakes earlier :
    • Stupid mistakes are caught almost immediately.
    • It helps developers find mistakes that would waste everyone’s time if they were found in Quality Assurance (QA).
  • Refactoring of code becomes easier and faster :
    • Makes code easier to maintain and refactor.
    • TDD helps to provide clarity during the implementation process and provides a safety net when you want to refactor the code you have just written.
    • Because TDD essentially forces you to write unit tests before writing implementation code, refactoring of code becomes easier and faster.
    • Refactoring code written two years ago is hard, if that code is backed up by a set of good unit tests, the process is made so much easier.
  • It facilitates the maintenance :
    • Unit tests are especially valuable as a safety net when the code needs to be changed to either add new features or fix an existing bug.
    • Since maintenance accounts for between 60 and 90% of the software life cycle, it’s hard to overstate how the time taken upfront to create a decent set of unit tests can pay for itself over and over again over the lifetime of the project.

Test Driven Development (TDD) practice

  • The simple concept of TDD is to write and correct the failed tests before writing new code (before development) :
    • This helps to avoid duplication of code as we write a small amount of code at a time in order to pass tests. (Tests are nothing but requirement conditions that we need to test to fulfill them).
    • TDD is a Developer approach between developer and tester to create well written unit of code (module, class, function).
  • TDD or Test-Driven Development or Test-First Development is a development process that has three steps :
    • “Test-Driven Development” refers to a style of programming in which three activities are tightly interwoven :
      • coding,
      • testing (in the form of writing unit tests)
      • and design (in the form of refactoring).
    • Step 1 – Write a test and run it to fail
      • Write a “single” unit test describing an aspect of the program, run the test, which should fail because the program lacks that feature
    • Step 2 – Write just enough code to pass the test
      • Write “just enough” code, the simplest possible, to make the test pass
    • Step 3 – Refactor the written code
      • Refactor the code until it conforms to the simplicity criteria
    • Then repeat, “accumulating” unit tests over time.
  • Another metaphor for TDD is :
    • Red – Green – Refactor.
  • Test Driven Development does not test the existing test cases / software before developing new functionality.
  • It only tests the test cases writing for the new functionality which needs to be developed.

More informations for Scrum Testing

More informations for the Scrum PSD certification here.

Leave a Reply

Your email address will not be published. Required fields are marked *