Test Driven Development
TDD (Test Driven Development) which is a software development technique has recently been of particular interest to developers. What is special about TDD is that at first the tests are written for a specific requirement before developing any code and at last after the code has been written it is refactored. In fact, in irritations we first make test cases which cover our defined requirement of the code and then we produce the code related to our tests and at the end we refactor the software.
As a matter of fact, the basis of TDD is related to TFD (Test First Design). But nowadays TDD itself is becoming an important point of view in developing software. Therefore TDD is TFD plus refactoring.
TDD has two main goals. Some believe that TDD helps us to focus more on the design stage before any code is written. Writing tests at first needs us to get familiar with our requirements through use cases and user stories. That’s why we pay more attention to the design section. Others believe that TDD is a program technique which results in a neat, good working code.
What is important is that we need specific unite tests so we could write our desired tests before producing any code. The unit tests should rapidly response to changes. Meaning, they should behave in such a way that we perceive their positive feedback to improved and refactored code. There are different frameworks we could use. For example, we could use NUnite or MBUnit for .NET testing or JUnit for Java testing.
TDD has five stages:
- At first we write a new test. This must fail because no code is written for it.
- Here we should run all the tests plus the new test. We want to make sure that it fails because we have no code.
- At this stage we write a code that passes the test. The code we write here is not perfect or clean, but it doesn’t matter because we will improve the code later on.
- We run the tests again. If it fails we need to update our code and repeat this stage. But if it passes we start the last stage.
- At last we refactor the code. As said before the code in stage 3 is not a clean code and it should only pass the test. Here we will clean it up. Remember to run the tests at this stage so we get sure that by refactoring the code it is still working correctly.
Links: