Tuesday, June 5, 2012

Understanding Test/Behaviour Driven Development


WIKIPEDIA :

TDD
http://en.wikipedia.org/wiki/Test-driven_development

BDD
http://en.wikipedia.org/wiki/Behavior_driven_development

What is TDD/BDD?

  • TDD means "Test Driven Development". It means that "Tests should drive your development"
  • Some people refer TDD as "Test Driven Design". They mean to say that "Tests should drive your design".
  • BDD means "Behaviour Driven Development". We should be thinking tests in terms of behaviors. These are specifications given to us by the business and provide value.
  • TDD or BDD is an approach to write your code. You would write tests first and then write your code.
  • Its a practice.
  • Its a discipline.
  • Its a philosophy.
  • Most people, including me, initially find it difficult to write tests first & then write their code. But once we get going, writing tests comes naturally.
  • Everyone fights the idea of writing tests, instead of writing code.
  • People working in a team can get into arguments & fights of what to test & what to not test & how to write such tests. Looking at how others are doing things can help in such cases.

Advantages of TDD/BDD

Personally, i have learnt the following through my experience.

  • TDD/BDD helps in writing clean, maintainable code
  • To better understand the businee problem/user story at hand
  • We start asking question when we start writing tests.
  • We try and write the simplest code possible and then think of ways of writing better code.
  • We do not have unnecessary code. We try and clean up as much as possible when we refactor.
  • There are no comments in the code. Why a particular line of code exists is evident by the tests that cover it.
  • It makes us more confident in our code-base. Better to have specifications than to have none.
  • It helps us in follow other practices such as Continous Integration & Automated deployments.
  • It helps us to refactor our code constantly.
  • If a bug is introduced in the system, it takes less time for a developer to see what's happening. An after effect would be to write a failing test to mock the bug and then fix it.
  • TDD can lead to more modularized, flexible, and extensible code. Always developers are trying to think of better ways to write code and use patterns & principles

The TDD Cycle

  • Red, Green, Refactor is the mantra of the industry.
  • Red Green Refactor means : Write a failing test, try and make it pass. Then, refactor your code to make the code readable, clean, and more efficient.

Saibaba        Vinaykaya Namaha!

No comments:

Post a Comment